My Thoughts: Checking the Web Page load time using WebDriver

Sunday, June 3

Checking the Web Page load time using WebDriver

Most of the time we would like to check the performance of site like the page load time. But in WebDriver we don't have any direct method to do this. So here is the sample code to achieve this using TestNG framework:
import java.util.concurrent.TimeUnit;
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.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CheckPageLoadTime{
 
 WebDriver driver;
 
 @BeforeTest
 public void start(){ 
  driver = new FirefoxDriver();
 }
 
 @Test
 public void LoadTime(){
  
 long startTime = System.currentTimeMillis()/1000;
 System.out.println("The startTime is "+startTime);
 //Set the acceptable Page load time to 60 sec
 driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); 
 driver.get("http://google.com/");  
 WebElement search = driver.findElement(By.name("q"));
 //Iterate through the loop as long as time(60sec) is with in the acceptable Page load time
 while(((System.currentTimeMillis()/1000)-startTime)<60){
         if(search.isDisplayed()){
  long endTime = System.currentTimeMillis()/1000;
  System.out.println("The endTime is "+endTime);
  long loadTime = endTime - startTime;
  System.out.println("Totaltime: " +loadTime + " seconds"); 
      break;
  }  
   }     
     }
    
}
Here is the output of above program(It may vary for you)
The startTime is 1338712185
The endTime is 1338712215
Totaltime: 30 seconds

No comments:

Post a Comment