Performance Testing using Selenium Webdriver

Yes we know that Selenium Webdriver is a functional testing tool and is not a recommended option to do performance! But then there are times that you want to just do a quick performance check and find the bottlenecks in your application, here is how:

The solutions here are organised based on the complexity and detail you want to achieve:

Solution 1:  Measuring performance using a Timer in Selenium WebDriver

We can use the the StopWatch class for measuring the time taken for the page to load or any inbuilt date time calendar feature:

StopWatch pageLoad = new StopWatch();
//Perform your test
System.out.println("Total Page Load Time: " + pageLoad.getTime() + "milliseconds");


Calendar cal = Calendar.getInstance();
Date d1 = cal.getTime();
//Perform your test
Calendar cal2 = Calendar.getInstance();
Date d2 = cal2.getTime();
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;  
long diffMinutes = diff / (60 * 1000) % 60;   
System.out.println("Execution Time:",diffMinutes+" minute and "+diffSeconds+"seconds"););

Solution 2:  Measuring performance with the Navigation Timing API

JavascriptExecutor js = (JavascriptExecutor) driver;
long navigationStart = (Long) js.executeScript("return window.performance.timing.navigationStart;");
//Perform your test
long loadEventEnd = (Long) js.executeScript("return window.performance.timing.loadEventEnd;");
System.out.println("Page Load Time is " + (loadEventEnd - navigationStart)/1000 + " seconds.");

Solution 3:  Measuring performance with JMeter

Solution 4:  Measuring performance with Firebug's Net panel

Hope that helps someone playing with Selenium WebDriver and trying to achieve some basic performance testing!

No comments:

Post a Comment