Website automation with Selenium and JMeter: part 2

December 12th, 2018 Posted by DevOps No Comment yet

This is the final post, part 2 of a series:

In this post I show how to script Selenium for handling asynchronous browser behaviour, and how to install the tools.

WebDriver Sampler

There are many online resources that show how to write boilerplate WebDriver Sampler code (see references at the end). Below are some code snippets (we chose JavaScript) that demonstrate the more interesting techniques we used.


Waiting for State

Create an instance of WebDriverWait, a helper for waiting until a page element has the required state e.g. element is present/visible. Our script code uses the helper extensively.



Use an HTTP GET command to load the OpenShift default web page:



The above HTTP GET generates a sequence of URL redirects before eventually loading the login page that expects a username and password. The script needs to wait through these redirects until one of the required form input elements becomes present. The snippet below performs the wait, and finds HTML elements by ID and CSS selector:



The login click above generates another sequence of URL redirects that eventually load the home page. The script needs to wait until the my-project link is present, then click the link to view the project page. The snippet below finds an HTML link element:



I omitted similar code that loads the Builds view. Having requested this view, the script needs to wait for the “last build” link to become visible, then click to view that build. The snippet below uses an XPath locator to find the element:




The WebDriver Sampler provides access to JMeter logging. The script engine doesn’t provide a debugger, and script commands can fail for various reasons, so it’s useful to log script commands, as in the snippet below:



Below is a simplified extract from the log:



If a server error caused the browser to load an error page, we can write that page to the JMeter log. This can be convenient for accessing error information without having to switch context to the server. Also, a faulty WebDriver script can load an unexpected page then fail because a page element is missing. The code below writes the page source code to the log (limited to 8 Kb in case of very large page content, e.g. not an error page).



Browser Timeouts

If the server takes a long time to serve a given request, this can cause a script to fail for the wrong reason: browser timeout. The snippet below increases the browser’s default time limit.



Installing the Tools


To install JMeter and required plugins, see the directions below:

  1. Install Java 8 or 9.
  2. Install JMeter: choose a download package, then extract the compressed file to a suitable local directory.
  3. Download the JMeter Plugins Manager and copy this file to the JMeter lib\ext sub-directory.
  4. Start the JMeter IDE: in the JMeter bin sub-directory, run either jmeter.bat (Windows) or (Linux).
  5. On the main menu, go to Options > Plugins Manager. Check the box at Selenium/WebDriver Support to auto-download the Selenium WebDriver plugins to the lib\ext sub-directory.



To install ChromeDriver, see the directions below:

  1. Download ChromeDriver from the main download page (screenshot below).
  2. Click the link at Latest Release:ChromeDriver-main-downloads-page
  3. Choose a build for the target platform:ChromeDriver-directory-for-zipfiles
  4. Extract the downloaded file (an executable) to a suitable directory. A ChromeDriver Config element in a JMeter script should specify this path.



Featured image:



Tags: , , ,

No comments yet. You should be kind and add one!

Leave a Reply