How to know the exact time to load a page using Selenium WebDriver?

SeleniumAutomation TestingTesting Tools

We can determine the exact time to load a page using Selenium webdriver. We can capture the time before the page load with help of the System.currentTimeMillis method.

After the URL for the application is launched, we have to wait for the page to be loaded completely with the help of the explicit wait condition. Once the expected criteria for the element is met, we shall again record the current time.

The difference between the time recorded prior and post the page load will measure the exact time to load a page.

Syntax

long s = System.currentTimeMillis();

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class PageLoadTime{
   public static void main(String[] args) {
      System.setProperty("webdriver.gecko.driver",
         "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //capture time before page load
      long s = System.currentTimeMillis();
      //URL launch
      driver.get("https://www.tutorialspoint.com/videotutorials/subscription.php");
      //verify page is loaded
      WebDriverWait wt = new WebDriverWait(driver,6);
      wt.until(ExpectedConditions.elementToBeClickable (By.className("s-buy")));
      //capture time after page load
      long e = System.currentTimeMillis();
      //compute time
      long r = e – s;
      System.out.println("Page load time in milliseconds: " + r);
      driver.close();
   }
}

Output

raja
Published on 06-Apr-2021 12:02:20
Advertisements