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.

--- 
   

Wednesday, June 13, 2012

Webdriver - How to start different browsers.

Webdriver - How to start different browsers.

Following example will help you understand how to invoke different browsers using Webdriver, you can also implemented case structure so that browser selection is automatic instead of comment and uncomment the code.


package MercuryTours;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

//@Listeners({ MercuryTours.TestNG.TestNGCustom.class, MercuryTours.TestNG.TestNGCustomeListener.class })
public class MercuryTours_TC_04 {

private static WebDriver driver;
@BeforeMethod
public static void StartDriver() {

      driver = new InternetExplorerDriver();

/* //Download chromedriver.exe from http://code.google.com/p/chromedriver/downloads/list and place in following location
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe");            
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        driver = new ChromeDriver(options);*/
     
/*    FirefoxProfile Profile = new FirefoxProfile();
        Profile.setPreference("browser.safebrowsing.malware.enabled", false);
        driver = new FirefoxDriver(Profile);*/

//Safari driver still in experimental stage and run only on Mac
//For Windows need to relay on Selenium1 or RC
//Selenium browser = new DefaultSelenium("localhost", 4444, "*safari", "http://www.google.com");

 //driver = new OperaDriver();


}

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

@Test(groups = { "MercuryToursTestCases" }, enabled = true,timeOut = 90000)
public void Test_TC_01() throws Exception {
driver.get("http://newtours.demoaut.com/");
Thread.sleep(7000);

}


}

-----------------