How can we get exact time to load a page using Selenium WebDriver?
We use Thread.sleep
We use implicitlyWait
we use WebDriverWait
but How can we get exact time to load a page using Selenium WebDriver?
Join Stack Overflow to learn, share knowledge, and build your career.
How can we get exact time to load a page using Selenium WebDriver?
We use Thread.sleep
We use implicitlyWait
we use WebDriverWait
but How can we get exact time to load a page using Selenium WebDriver?
If you are trying to find out how much time does it take to load a page completely using Selenium WebDriver (a.k.a Selenium 2).
Normally WebDriver should return control to your code only after the page has loaded completely.
So the following Selenium Java code might help you to find the time for a page load -
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 then you will have to wait till some element is displayed 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);
You can use StopWatch object of org.apache.commons.lang3.time package. The following is the complete code of Selenium WebDriver 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();
}
}
I also had this requirement and I used the performance.timeOrigin object provided by the browser. I used it in C# but for this question, I have modified @Ripon Al Wasim answer, to make it work for 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();
//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")));
//Get the time
long pageLoadTime_ms = getPageLoadTime();
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();
}
public static long getPageLoadTime(WebDriver driver){
//Creating the JavascriptExecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
//This will get you the time passed since you page navigation started
return (Long)((JavascriptExecutor)driver).executeScript("return Date.now() - performance.timeOrigin;"");
}
}
Just call the following method where you need to get the page load time.
public static long getPageLoadTime(WebDriver driver){
//Creating the JavascriptExecutor interface object by Type casting
JavascriptExecutor js = (JavascriptExecutor)driver;
//This will get you the time passed since you page navigation started
return (Long)((JavascriptExecutor)driver).executeScript("return Date.now() - performance.timeOrigin;"");
}
You don't need to worry about where page load started but just like in the other answers you have to put it immediately after your page load is finished.
This works like StopWatch but it is more clean(no start, stop, reset required).
Use TestListners for best experience :
@AfterMethod
public void getResult(ITestResult result) throws Exception {
String sMethodName = "";
Long lExecutionTime;
sMethodName = result.getMethod().getMethodName().toString();
lExecutionTime = (result.getEndMillis() - result.getStartMillis()) / 1000;
System.out.println(sMethodName + " execution Time is : " + lExecutionTime + "sec.");
}