Implement web.Timings

Problem

A new embedded feature in some browsers is the webtimings. This feature works only in Internet Explorer 9 and Google Chrome, by the time this book is published. We can also combine it with predefined Javascript variables. We can use that if we want to start the timings after a specific element is present in the HTML DOM. The disadvantage of web.Timings is that it does not give timings per item downloaded. In this recipe we will see how we can make use of the new standard.

Solution

We have to execute some JavaScript through Selenium, before we get performance related values. The script, including the JavaScript calls, will look like this:

<code lang="java">
@Test
public void measureWebTimings() throws InterruptedException {
    driver.get("/");
    JavascriptExecutor js = (JavascriptExecutor) driver;
    Double loadEventEnt = (Double) js.executeScript("return window.performance.timing.loadEventEnd;");
    Double navigationStart = (Double) js.executeScript("return window.performance.timing.navigationStart;");
    System.out.println("Page Load Time = " + (loadEventEnt - navigationStart));
    WebElement search = driver.findElement(By.id("q"));
    search.sendKeys("Selenium");
    search.submit();
    assertTrue(driver.findElement(By.tagName("body")).getText().contains("Selenium"));
    WebElement result = driver.findElement(By.linkText("Selenium"));
    result.click();
    assertTrue(driver.findElement(By.tagName("body")).getText().contains("Selenium"));
}
</code>

What has been done

The script is taking some values from the browser and pass them through back to the test.

One thought on “Implement web.Timings”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.