Closed myitcv closed 7 years ago
Last time I looked at this, only PhantomJS had support for reporting any type of JS logs. The integration test is here: https://github.com/sclevine/agouti/blob/master/internal/integration/page_test.go#L95
I would check LogTypes to see what logging channels headless Chrome supports, and then try each of those to see if any of them will report console.log
messages.
If ChromeDriver has some special endpoint for retrieving these types of logs, you can always use page.Session().Send(...)
to hit the endpoint manually. (I think this is unlikely though.)
If that doesn't work, you could do something terrible with RunScript
.
Thanks @sclevine.
Turns out this is possible:
Here's a complete, standalone example:
package main
import (
"fmt"
"github.com/sclevine/agouti"
)
func main() {
driver := agouti.ChromeDriver(
agouti.ChromeOptions(
"args", []string{
"headless",
"disable-gpu",
"no-default-browser-check",
"no-sandbox",
"no-first-run",
"disable-default-apps",
"disable-popup-blocking",
"disable-translate",
"disable-background-timer-throttling",
"disable-renderer-backgrounding",
"disable-device-discovery-notifications",
},
),
agouti.ChromeOptions(
"binary", "/usr/bin/google-chrome",
),
agouti.Desired(
agouti.Capabilities{
"loggingPrefs": map[string]string{
"browser": "INFO",
},
},
),
)
if err := driver.Start(); err != nil {
panic(err)
}
p, err := driver.NewPage()
if err != nil {
panic(err)
}
err = p.RunScript("console.log('hello world');", nil, nil)
if err != nil {
panic(err)
}
printNewLogs(p, "browser")
if err := driver.Stop(); err != nil {
panic(err)
}
}
func printNewLogs(p *agouti.Page, typ string) {
logs, err := p.ReadNewLogs(typ)
if err != nil {
panic(err)
}
for _, l := range logs {
fmt.Println(l.Message)
}
}
Outputs:
console-api 371:61 "hello world"
Firstly, thank you for this package - great work!
I've searched various places, probably with the wrong terms, for an answer to the following question. Would appreciate any pointers.
Given the following:
I get the following output:
But if I change the
console.log
toconsole.error
I get the output:So my question is, how can I configure Chrome so that the
console.log
messages are also returned byReadNewLogs
?For what it's worth:
Thanks in advance.