Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll discuss the steps to run Selenium scripts with JMeter.

2. Selenium Scripts with JMeter

JMeter provides an open-source solution for performance and load testing. It can also be used for functional testing. But with the advancement of technologies like CSS, JS, and HTML5, we push more and more logic and behaviour down the client. Thus many more things add to the browser execution time. These things include:

  • Client-side Javascript execution – AJAX, JS templates, etc.
  • CSS transforms – 3D matrix transforms, animations, etc.
  • 3rd party plugins – Facebook likes, Double click ads, etc.

In turn, this might affect the overall performance of a website or web application. But JMeter does not have such a matrix to measure these perceived performances. JMeter also cannot measure the user experience at client renderings like load time or page rendition, as JMeter is not a real browser.

Web drivers like Selenium can automate the execution and collection of performance metrics discussed above on the client-side (browser in this case). Thus while the JMeter Load Test will put enough load on the system, the JMeter WebDriver plan will get the response times and other behaviour from the user experience point of view.

Thus apart from measuring performance, we can also measure other behaviours when we use a WebDriver set with JMeter. So let’s discuss this further.

3. Prerequisites

The following prerequisites should be fulfilled before running a Selenium script with JMeter:

Now we can create a sample JMeter project to run the Selenium script.

4. JMeter Project

At this point, we have the environment installed to run the Selenium script in JMeter. Now, let’s create a JMeter Project to configure and test it. We’ll create a Thread Group that will have a Selenium Web Driver Sampler instance. We’ll include a Selenium script in this sampler and then execute it.

A detailed description is given below:

First, we launch our JMeter GUI:

JMeterStart

Then we can add a simple “Thread Group” by clicking “Edit-> Add” and selecting the Thread Group:

createTG

Then, we need to add the Chrome driver config. Now, we click on the Chrome driver config in the left pane and specify the “Path to chrome driver”:

chromeConfigSetting

Please note that the version of Chrome browser should match the “Chromedrive.exe” version for the script to run successfully.

Next, we need to add the web driver sampler to the thread group:

WDSampler

We can add the script  given below to the thread group:

WDS.sampleResult.sampleStart()
WDS.browser.get('http://baeldung.com')
WDS.sampleResult.sampleEnd()
WDS.log.info("successfully navigated to http://baeldung.com/");

Finally, let’s add a ‘View Results in Table’  and/or “View Results Tree” listener so that we can view the results of the script execution.

The thread group we created above looks as shown in the following image:

threadGroup1

5. Running the Selenium Script

We have created the thread group with the Selenium script we want to execute. Next, we “Run the Thread Group”.

The instance of Selenium Web Driver is created, and a new Chrome driver window opens up that opens the homepage of Baeldung:

output1

As we can see from the JMeter Results table above, we have successfully executed the Thread Group that contained a simple Selenium script that opened a new Chrome Browser window and then opened the specified webpage. This way, we can execute any Selenium script by adding a WebDriver Sample in the Thread Group and then executing it.

6. Conclusion

In this tutorial, we have illustrated running a Selenium script using JMeter. We executed a Selenium script in JMeter by creating a Thread Group containing the Selenium Web Driver instance.

The full code for the implementation is available on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!