How can we get the exact time to load a page using Selenium WebDriver?

How can we get the exact time to load a page using Selenium WebDriver?

We use Thread.sleep

We use implicitlyWait

we use WebDriverWait

but how can we get the exact time to load the page using Selenium WebDriver?

+6
source share
3 answers

If you are trying to figure out how long it takes to fully load the page using Selenium WebDriver (aka Selenium 2).

Usually WebDriver should return control to your code only after the page has fully loaded.

So the following Selenium Java code can help you find the page load time -

long start = System.currentTimeMillis(); driver.get("Some url"); long finish = System.currentTimeMillis(); long totalTime = finish - start; System.out.println("Total Time for page load - "+totalTime); 

If this does not work, you have to wait until any element appears on the page -

  long start = System.currentTimeMillis(); driver.get("Some url"); WebElement ele = driver.findElement(By.id("ID of some element on the page which will load")); long finish = System.currentTimeMillis(); long totalTime = finish - start; System.out.println("Total Time for page load - "+totalTime); 
+12
source

You can use the StopWatch object of the org.apache.commons.lang3.time package. Below is the complete Selenium WebDriver code using Java:

 import org.apache.commons.lang3.time.StopWatch; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class TimerInSeleniumWebDriver { public static void main(String[] args) { WebDriver driver; driver = new FirefoxDriver(); StopWatch pageLoad = new StopWatch(); pageLoad.start(); //Open your web app (In my case, I opened facebook) driver.get("https://www.facebook.com/"); // Wait for the required any element (I am waiting for Login button in fb) WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.presenceOfElementLocated(By.id("u_0_l"))); pageLoad.stop(); //Get the time long pageLoadTime_ms = pageLoad.getTime(); long pageLoadTime_Seconds = pageLoadTime_ms / 1000; System.out.println("Total Page Load Time: " + pageLoadTime_ms + " milliseconds"); System.out.println("Total Page Load Time: " + pageLoadTime_Seconds + " seconds"); driver.close(); } } 
+3
source

driver.manage (). timeouts (). pageLoadTimeout (60, TimeUnit.SECONDS);

-8
source

Source: https://habr.com/ru/post/919240/


All Articles