Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 617
Jasmine.Olivra Avatar asked Jun 28 '12 12:06

Jasmine.Olivra


2 Answers

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); 
like image 100
Hari Reddy Avatar answered Nov 15 '22 19:11

Hari Reddy


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();
    }
}
like image 45
Ripon Al Wasim Avatar answered Nov 15 '22 20:11

Ripon Al Wasim