= Web Driver Tutorial
Web Driver Sampler automates the execution and collection of Performance metrics on the Browser (client-side). A large part of performance testing, up to this point, has been on the server side of things. However, with the advancement of technology, HTML5, JS and CSS improvements, more and more logic and behaviour have been pushed down to the client. This adds to the overall perceived performance of website/webapp, but this metric is not available in JMeter. Things that add to the overall browser execution time may include:
# Client-side Javascript execution - eg. AJAX, JS templates # CSS transforms - eg. 3D matrix transforms, animations # 3rd party plugins - eg. Facebook like, Double click ads, site analytics, etc
All these things add to the overall browser execution time, and this project aims to measure the time it takes to complete rendering all this content.
- Note: It is NOT the intention of this project to replace the HTTP Samplers included in JMeter. Rather it is meant to compliment them by measuring the end user load time.*
To get started, first follow the [PluginInstall] and then create a basic Test Plan with a Thread Group containing 1 thread. The next steps will cover adding the following elements to your test plan:
# Firefox Driver Config # Web Driver Sampler # View Results Table
Your basic Test Plan should look like this after adding the Thread Group:
/img/web-driver-tutorial-thread-group.png
Add a Firefox Driver Config to your Thread Group.
/img/web-driver-tutorial-firefox-config.png
Add a Web Driver Sampler to your Thread Group.
/img/web-driver-tutorial-web-sampler.png
Add the following *Javascript* code to the Script section to navigate to Google.
Add a View Results in Table to your Thread Group and then *run* your test. You should see something like the screenshot below that captures the result of the sample.
/img/web-driver-tutorial-view-results-in-table.png
The 2 minute guide is useful to get something up and running, but at the cost of some understanding. This section hopefully fills in some of the gaps/questions that can come from going through the previous guide.
JMeter allows the creation of multiple threads, and each thread is responsible for creating load on the server. However, for the Web Driver use case, the reader should be *prudent in the number of threads* they will create as each thread will have a single browser instance associated with it. *Each browser consumes a significant amount of resources*, and a limit should be placed on how many browsers the reader should create. However, the load testing cloud providers may help to scale WebDriver test up to thousands of real browsers, look at BlazeMeter for example.
From experience, the number of browser (threads) that the reader creates should be limited by the following formula:
This might should a little limiting. However, it is important to note that this is mainly used to *complement JMeter's HTTP Sampler*. The [WebDriverSampler] is meant to be run in conjunction with a HTTP Sampler so that from the server's perspective, the load is production like. At the same time, the web sampler will simulate the user experience of interacting with the website/webapp whilst the server is under load, hence measuring the real user's experience at the same time.
For the `Web Driver Sampler` to work, it will need to know what type of browser to run the tests with. Firefox has been chosen for the quick tutorial. However you can try out different Driver Configs to evaluate the other supported browsers as well.
There is more in depth information on the [WebDriverSampler]. However, it is important to understand that the content of the script editor is to allow the user total control over when the interactions with the browser *as well as* when the sampling should be conducted. This last part is important as it will allow the reader to not only measure the amount of time it takes to load a URL (as the above example), but also to measure AJAX requests as well (more on this in the [WebDriverSampler]).
In the below example, the `Browser.get()` method call will make the browser load the specified URL. The `SampleResult.sampleStart()` will start timing and then calling `SampleResult.sampleEnd()` afterwards will stop the timing and allow JMeter to use the `SampleResult` when measuring the results in the `View Results in Table` component.
The `Browser` object exposed in the `Script` section is an instance of the `WebDriver` object documented in the Selenium documentation. It is recommended that the reader have a look at the documentation to see what methods are available on the WebDriver API, to better understand what can be scriptable on the Browser instance.
It is also up to the reader to ensure that they verify that the correct page has been loaded and then setting the corresponding `SampleResult.setSuccessful(boolean)` method when invoking this script. Since the full WebDriver instance is available to the reader, they should be able to verify that the page has loaded successfully and then set the correct status on the SampleResult. The example below will verify that the page contains the correct title and uses this to indicate success/failure on the SampleResult.
<syntaxhighlight lang="javascript"> WDS.sampleResult.sampleStart() WDS.browser.get('http://google.com') WDS.sampleResult.sampleEnd() if(WDS.browser.getTitle() != 'Google') { WDS.sampleResult.setSuccessful(false) WDS.sampleResult.setResponseMessage('Page title is not Google!') } </syntaxhighlight>For a more thorough understanding, the reader should familiarise themselves with the SampleResult documentation as well, to better understand what is scriptable on the SampleResult object.