Skip to content

Web Sampler Explained

cplim edited this page Nov 28, 2012 · 1 revision

This will describe in detail the concepts and purpose of the Web (Browser) Sampler. This should also serve as a guide to help the performance tester decide how much scripting they wish to do per sampler.

Note: This builds upon the [Getting Started](Getting Started) section, so if you have not used JMeter before it is recommended to go through it quickly.

Purpose of a Web Browser Sampler

The intention of each Sampler is to represent a unit of user interaction that needs to be measured. This can range from a page load, to an AJAX load, or anything that can be simulated using the Selenium Web Driver.

Example Test

Lets take a simple example, where we wish to measure the following user interactions:

  1. Load the home page.
  2. Auto-suggest as the user enters their search terms
  3. Time it takes the search results to appear.

If each of the above mentioned interactions is important and needs to be measured, then each would be represented by a separate web sampler, so that the time it takes for each sampler can be collected and assessed. We will discuss each of the 3 samplers in detail below, and we will use Amazon as the example site.

Load Home Page

After the Thread Group has been created, we can now create the first Web Sampler. When creating the sampler, name it Amazon Home Page and fill in the script text field with the following Javascript code:

Browser.get('http://www.amazon.com/');

What the one line above will do is tell the current browser to load up the Amazon home page, and after that collect the time it took to complete loading the page (AJAX, CSS rendering, analytics, etc). For those familiar with JMeter, it is possible to create multiple threads within a test suite, and each thread will have their own browser object available to them. The threads never share browser objects.

The browser variable is actually a reference to a WebDriver object and the standard behaviour can be scripted by looking up the Selenium Javadoc.

Now that the home page is loaded, lets script the next interaction.

Show Auto-suggest

As mentioned, each thread will keep a reference to its own browser instance. So when going on to the next web sampler, the page state and URL of the browser will be maintained and available to this subsequent sampler.

Now create a new sampler called Auto-suggest and fill in the script field as follows:

with(websampler) {
    // enter 'Java Testing' into the search field
    var searchField = Browser.findElement(By.id('twotabsearchtextbox'));
    searchField.sendKeys('Java Testing');

    // wait up to 10 seconds for the auto-suggest to appear, 
    // throwing an exception otherwise.
    var wait = new WebDriverWait(Browser, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id('srch_sggst')));
}

We now introduce a new variable called websampler, which is nothing more than a JavaImporter for some of the Selenium packages. At the time of writing, only the following packages are imported:

  • org.openqa.selenium
  • org.openqa.selenium.support.ui

By importing these packages, some of the Selenium utility classes can be referenced in a more readable format. The above script will perform the following steps:

  1. Find the search input by the id twotabsearchtextbox
  2. Enter the text Java Testing into this field.
  3. Create a maximum wait time of 10 seconds for autosuggest to appear.
  4. Wait for the list of auto-suggest entries to appear by looking for elements with suggest_link class.

Now that the auto-suggest appears we can now proceed to the next step to measure the search results page load time.

Show Search Results

Create the third and final web sampler called Search Results and enter the following into the script field:

with(websampler) {
    // execute search
    var submit = Browser.findElement(By.className('nav-submit-input'));
    submit.click();
}

This will click on the search button and complete when the search results page has finished loading.

By separating these into separate samplers it is possible to measure how long it takes the browser to load each of these interactions by attaching JMeter listeners to each sampler and running through this suite with different input values for the search field.

Clone this wiki locally