webdriver icon indicating copy to clipboard operation
webdriver copied to clipboard

Detect when the page is loaded

Open diegobernardes opened this issue 10 years ago • 3 comments

There is any way to detect when the page is loaded?

diegobernardes avatar Jun 23 '15 02:06 diegobernardes

I would also like to know this. I'm personally looking for some performance metrics, which I see performance logging is available via webdriver, but I don't see any particular support for it utilizing this package. I'm sure it works and I'm just too new to webdriver/golang to figure it out.

Anyway, if anyone has any insight into how to detect if a page is loaded and/or calculate the total load time, please let me know!

EDIT: Just an update, you can see ChromeDriver's performance log options here: https://sites.google.com/a/chromium.org/chromedriver/logging/performance-log

An example of the performance logging being utilized in Python (with a mention of Java utilizing the same functionality) : http://stackoverflow.com/questions/27596229/browser-performance-tests-through-selenium

Unfortunately, though, I can't quite place the syntax that I'd need to pass in LoggingPreferences of "Performance" and "All". I do see that this golang package has support for custom capabilities, I'm just not certain how they'd be triggered or what sort of syntax I should be using to tell the LoggingPreferences that I'd like ALL of the performance metrics.

mbitson avatar Jun 23 '15 19:06 mbitson

the Capabilities are a map[string]interface{} type. Below is how to set the performance logging to level INFO, which is the level needed to retrieve the performance logs.

chromeDriver := webdriver.NewChromeDriver("/path/to/chromedriver")
err := chromeDriver.Start()
if err != nil {
    log.Println(err)
}
var loggingPrefs = map[string]webdriver.LogLevel{
    "performance": webdriver.LogInfo,
}
desired := webdriver.Capabilities{"Platform": "Linux", "loggingPrefs": loggingPrefs }
required := webdriver.Capabilities{}
session, err := chromeDriver.NewSession(desired, required)

To retrieve the performance logs after requesting a page, use:

err = session.Url("http://golang.org")
if err != nil {
    log.Fatal(err)
}
logs, err := session.Log("performance")
if err != nil {
    log.Fatal(err)
}

The performance logs will include the events listed at: https://developer.chrome.com/devtools/docs/protocol/1.1/index

To detect when the page is loaded, look for the "Page.loadEventFired" event.

I am currently working on a library that will take the performance logs and create a HTTP archive (HAR) file which can be analysed by a number of different tools. Feel free to use it as inspiration for what you are working on. https://github.com/woodsaj/chromedriver_har

woodsaj avatar Aug 31 '15 07:08 woodsaj

Thank you woodsaj! I really only need a HAR file after my request, anyway, so I may end up using your project. Thanks for updating this thread with such valuable information :)

mbitson avatar Aug 31 '15 12:08 mbitson