New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use chromedp to drive an Electron application #628
Comments
Bump... I've determined that the ws://localhost:8315/devtools/page/396803C460A3CEF97A38A5E67EACBAE2 form of the URL is in fact "correct" -- this sort of URL works when I drive an instance of Chrome proper with the debugging url enabled. I derive that URL from http://localhost:8315/json/version -- which returns the value on the "webSocketDebuggerUrl" attribute. So the Electron app is in fact listening on 8315 and serving up the debug API. But when I try to debug my electron app, I still get the "Not supported (-32000)" error from even a "raw" Run() command without any Navigate, etc.: package main
import (
"context"
"flag"
"log"
"github.com/chromedp/chromedp"
)
var flagDevToolWsUrl = flag.String("devtools-ws-url", "", "DevTools WebSsocket URL")
func main() {
flag.Parse()
if *flagDevToolWsUrl == "" {
log.Fatal("must specify -devtools-ws-url")
}
// create allocator context for use with creating a browser context later
allocatorContext, cancel := chromedp.NewRemoteAllocator(context.Background(), *flagDevToolWsUrl)
defer cancel()
log.Printf("allocatorContext: %#v", allocatorContext)
// create context
ctxt, cancel := chromedp.NewContext(allocatorContext)
defer cancel()
log.Printf("ctxt: %#v", ctxt)
// run task list
var body string
if err := chromedp.Run(ctxt); err != nil {
log.Fatalf("raw Run failed: %v", err)
}
} |
Could you provide a way to reproduce that error? I don't have an Electron app I can test with. |
My electron app is based on go-astilectron. I've managed to find a way to test it with Spectron (via nodejs) and can send you a sample application (based on the astilectron demo). If you want to build it yourself, see my pull request at asticode/go-astilectron-demo#62. If that's not convenient, I can create a tarball (but it will be very large...). Let me know. |
I believe the URL should be Lines 458 to 461 in 9191ea2
@chinenual Are you still working on this? Do you want to give it a shot? |
I have tested with Electron (Sine The app I used is https://github.com/electron/electron-quick-start. First, start it with the following command: $ yarn start --remote-debugging-port=9222 And here is the output (we need the WebSocket debug URL later):
Here is a simple demo to automate the app: package main
import (
"context"
"log"
"github.com/chromedp/chromedp"
)
func main() {
ctx, cancel := chromedp.NewRemoteAllocator(context.Background(), "ws://127.0.0.1:9222/devtools/browser/f2129e98-5476-435d-9803-de8ba99e2032")
defer cancel()
ctx, cancel = chromedp.NewContext(ctx,
chromedp.WithDebugf(log.Printf),
)
defer cancel()
// go get the current targets in the Electron app.
ts, err := chromedp.Targets(ctx)
if err != nil {
log.Fatal(err)
}
if len(ts) == 0 {
log.Fatal("targets not found.")
}
ctx, cancel = chromedp.NewContext(ctx,
// use the first target
chromedp.WithTargetID(ts[0].TargetID),
)
// if we cancel the target, it will close the Electron app.
//defer cancel()
var body string
if err := chromedp.Run(ctx,
chromedp.OuterHTML("body", &body, chromedp.ByQuery),
chromedp.Navigate("https://www.google.com"),
); err != nil {
log.Fatal(err)
}
log.Println(body)
} Notes:
|
allocatorContext, cancel := chromedp.NewRemoteAllocator(context.Background(), "ws://localhost:8202/")
defer cancel()
// create context
ctx, cancel := chromedp.NewContext(allocatorContext)
defer cancel()
var body string
err := chromedp.Run(ctx,
chromedp.OuterHTML("html", &body),
)
fmt.Println(err)
fmt.Println(body) Output:
|
but this library works |
|
What versions are you running?
What did you do? Include clear steps.
I've enabled dev-tools on my application via
I've manually started the application; it's running and listening on the debug port --
I can browse to https://localhost:8315 and see interactive dev tools. However, if I try to drive the app via chromedp (e.g. with the "remote" example), I get a 404 error. Eg.:
I've debugged this and determined that the first call to Run(ctxt) is what is failing.
If I change the URL to include a page id that I can see in the /json response (http://localhost:8315/json), the error changes to "Not Supported (-32000)". Not sure if that's an improvement, or just that I am doing something even more wrong.
Can anyone advise how to connect chromedp to an Electron application? Perhaps the WS URL needs more context (not just host and port?)
The text was updated successfully, but these errors were encountered: