Friday, June 29, 2012

Webdriver - Calculate Page Load Time

Webdriver - Calculate Page Load Time

I have a similar post for Selenium1.
Selenium2 automatically wait during page load this is an awesome feature not required to write explicit wait or sync statement, but for AJAX calls need to write extra line of code.
How to calculate the page load time? Using java timers.

You can implement the following code in 3 different ways.

long start;
start = System.currentTimeMillis();
driver.get(url); or click();
Print (driver.getTitle() + " - " + (System.currentTimeMillis() - start) + " MilliSec");

1. Copy the above code where page response calculation is required, but it create redundancy.

2. Implement the code in page object. Below code contain 3 classes, 1 test class and 2 page objects. This code will help you to understand page object implementation and calculate the page download time.

package MercuryTours;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import MercuryTours.PageObjects.UtilityScript;
import MercuryTours.PageObjects._01_Initilize;
import MercuryTours.PageObjects._02_Login;

public class MercuryTours_TC_05 extends UtilityScript {

private static WebDriver driver;
@BeforeMethod
public static void StartDriver() {
      driver = new InternetExplorerDriver();
}

@AfterMethod(alwaysRun = true)
public void StopDriver() {
driver.quit();
}

@Test(groups = { "MercuryToursTestCases" }, enabled = true,timeOut = 90000)
public void Test_TC_01() throws Exception {
_01_Initilize Initilize = PageFactory.initElements(driver,_01_Initilize.class);
Initilize.zOpen("http://newtours.demoaut.com/");
_02_Login Login = PageFactory.initElements(driver,_02_Login.class);
Login.zEnterCrediantials("qtp123", "qtp123");
}
}


package MercuryTours.PageObjects;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;

public class _01_Initilize extends UtilityScript {

private WebDriver driver;

public _01_Initilize(WebDriver driver) throws InterruptedException {
this.driver = driver;
}

public _01_Initilize zOpen(String url) throws Exception {
driver.manage().timeouts().implicitlyWait(ImplicitWait, TimeUnit.SECONDS);
driver.manage().window().maximize();
long start;
start = System.currentTimeMillis();
driver.get(url);
Print (driver.getTitle() + " - " + (System.currentTimeMillis() - start) + " MilliSec");
return this;
}

} 


package MercuryTours.PageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class _02_Login extends UtilityScript {
private WebDriver driver;

public _02_Login(WebDriver driver) throws InterruptedException {
this.driver = driver;
}

public _02_Login zEnterCrediantials(String UserNameTxt, String PasswordTxt)
throws InterruptedException {
Wait(3000);
driver.findElement(By.name("userName")).sendKeys(UserNameTxt);
driver.findElement(By.name("password")).sendKeys(UserNameTxt);
//driver.findElement(By.name("login")).click();
click(driver.findElement(By.name("login")));
Print("UserName:" + UserNameTxt);
Print("---Login");
Wait(3000);
return this;
}
public void click(WebElement element)  {
long start;
start = System.currentTimeMillis();
element.click();
Print (driver.getTitle() + " - " + (System.currentTimeMillis() - start) + " MilliSec");
}

}

Note: Print() is a custom function written in Utility class, replace it with System.out.Println();

3. By extending the click() method using Webdriver interface.

--- 
   

No comments:

Post a Comment