Measuring performance using a Timer in Selenium WebDriver

10 April, 2014
Measuring page load or the response time is one of the basic parameters that we can capture in the Selenium WebDriver tests. We can use timers in the test code to capture the time taken for page load, rendering of the elements,and JavaScript code execution. This can be implemented using the Date/Time classes in programming languages. We can also use the Stopwatch class to measure the time taken. The disadvantage of this approach will be testing with a lot of timers added.

In the example test below, we shall use the StopWatch class for measuring the time taken for the page to load and render the required element. The StopWatch class provides a clean way to calculate the elapsed time between the events. It also provides a method to suspend and resume the Stopwatch class.


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 test {
	public static void main(String[] args) {
		WebDriver driver = new FirefoxDriver();
		// Get the StopWatch Object and start the StopWatch
		StopWatch pageLoad = new StopWatch();
		pageLoad.start();
		// Open the Application
		driver.get("http://money.rediff.com/");
		// Wait for the required button
		new WebDriverWait(driver, 10).until(ExpectedConditions.	presenceOfElementLocated(By.id("sensTab2")));
		// Stop the StopWatch
		pageLoad.stop();
		System.out.println("Total Page Load Time: " + pageLoad.getTime() + "milliseconds");
	}
}

No comments:

Post a Comment