Wednesday 3 July 2013

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:
  1. import java.util.concurrent.TimeUnit;  
  2. import org.openqa.selenium.By;  
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.firefox.FirefoxDriver;  
  6. import org.openqa.selenium.firefox.FirefoxProfile;  
  7. import org.testng.annotations.BeforeTest;  
  8. import org.testng.annotations.Test;  
  9.   
  10. public class CheckPageLoadTime{  
  11.    
  12.  WebDriver driver;  
  13.    
  14.  @BeforeTest  
  15.  public void start(){   
  16.   driver = new FirefoxDriver();  
  17.  }  
  18.    
  19.  @Test  
  20.  public void LoadTime(){  
  21.     
  22.  long startTime = System.currentTimeMillis()/1000;  
  23.  System.out.println("The startTime is "+startTime);  
  24.  //Set the acceptable Page load time to 60 sec  
  25.  driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);   
  26.  driver.get("http://google.com/");    
  27.  WebElement search = driver.findElement(By.name("q"));  
  28.  //Iterate through the loop as long as time(60sec) is with in the acceptable Page load time  
  29.  while(((System.currentTimeMillis()/1000)-startTime)<60){  
  30.          if(search.isDisplayed()){  
  31.   long endTime = System.currentTimeMillis()/1000;  
  32.   System.out.println("The endTime is "+endTime);  
  33.   long loadTime = endTime - startTime;  
  34.   System.out.println("Totaltime: " +loadTime + " seconds");   
  35.       break;  
  36.   }    
  37.    }       
  38.      }  
  39.       
  40. }  
Here is the output of above program(It may vary for you)
  1. The startTime is 1338712185  
  2. The endTime is 1338712215  
  3. Totaltime: 30 seconds  

2 comments:

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...