Parallel Testing with Selenium Webdriver
Cara Suarez
The combination of using JMeter and some simple scripting to invoke Selenium code allows you to measure how fast HTML pages take to load. And with Headless browser execution, the test execution time can be reduced up to 30% and lets you continue to use your computer while the tests execute in the background.
Find out why performance testing is essential in establishing an exceptional customer experience. Contact Kobiton today.
As Web driver restricts our ability to scale due to its high usage of CPU, combining it with a well-organized JMeter load Test helps.
One of the most significant advantages we have here is achieving the performance matrix by avoiding multiple browsers opening up and minimizing the overall CPU usage and speed.
There are two primary ways to achieve this integration:
Open JMeter after successful installation.
As we all know, JMeter is an open-source java application. There are custom plugins which one needs to install separately after JMeter installation, so one can use additional features using the Plugins Manager.
With the help of WebDriver sampler, every browser consumes a significant amount of resources and every thread will have a single browser instance.
Right-click on Thread group, click on Add>Sampler>jp@gc – WebDriver Sampler
Add “jp@gc – Chrome Driver Config” under thread group
Sample Selenium code in java for searching Automation in web browser
driver.browser.get('https://google.com')driver.sampleResult.sampleEnd() driver.sampleResult.sampleStart() driver.browser.get("https://www.google.com/"); var searchBox = driver.browser.findElement(By.name("q")); searchBox.click(); searchBox.sendKeys('Test Automation'); searchBox.sendKeys(Keys.ENTER);
So while introducing your scripts in jp@gc - WebDriver Sampler one has to make the following changes:
Replace driver keyword with WDS
Replace By class with org.openqa.selenium
SAMPLE Script for jp@gc – WebDriver Sample
WDS.sampleResult.sampleStart() WDS.browser.get('https://google.com') WDS.sampleResult.sampleEnd() WDS.sampleResult.sampleStart() WDS.browser.get("https://www.google.com/"); var searchBox = WDS.browser.findElement(org.openqa.selenium.By.name("q")); searchBox.click(); searchBox.sendKeys('Test'); searchBox.sendKeys(org.openqa.selenium.Keys.ENTER); WDS.sampleResult.sampleEnd()
With Selenium we can write functional automation test cases, and we can put load on those scripts which are already written and validate the response time and other performance parameters
Prerequisites
SAMPLE JUNIT SCRIPT
import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
public class sample{
@Test
public void testing()
{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Driver\\driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://google.com");
System.out.println(driver.getTitle());
}
}
Under java folder, click on Jar file and click on Next.
Select jar file download location and provide the file name of the jar.
Now copy the jar and place it under junit folder where JMeter is installed
ApacheJmeter
Example : apache-jmeter-5.2.1\lib\junit\Sample.Jar
More thread leads to more browser sessions, which ultimately deteriorate the performance to quite an extent.
HtmlUnitDriver helps to run the test case without opening the web browser i.e., in Headless mode. Html Unit driver is a better choice as it increases the execution speed and CPU usage while maintaining Selenium API. Headless testing is simply running your Selenium tests using a headless browser. It operates as your typical browser would, but without a user interface, making it excellent for automated testing
When we integrate Selenium- Junit scripts in JMeter, we need to choose the browser driver as htmlUnitDriver in the Selenium- Junit scripts.
Download the htmlunit-driver and in sample script change the
SAMPLE JUNIT SCRIPT using htmlunitDriver
import org.junit.Test;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.WebDriver;
public class sample{
@Test
public void testing()
{
System.setProperty("webdriver.HtmlUnitDriver.driver", "C:\\Users\\Driver\\driver\\htmlunitdriver.exe");
WebDriver driver = new HtmlUnitDriver();
driver.get("https://google.com");
System.out.println(driver.getTitle());
}
}
Export this class file as jar file and label it with class name and replace this in location
Example: apache-jmeter-5.2.1\lib\junit\Sample.Jar
Restart JMeter and reopen the Junit Selenium .jmx file and run with headless mode with the updated ‘sample’ file which has headless browser – HtmlUnitDriver.
With an increasing focus on the user experience, performance testing is essential.
It is important to know the overall page load time during load testing to establish speed and scalability of software applications. As shown, the integration of JMeter with Selenium allows for headless testing and improved scalability of your tests.
Looking for a robust performance testing solution?