Documentation / Pre/post scripts

Pre/post scripts and login the user


Before loads and tests a URL you can run your own Selenium script. Maybe you want to access a URL and pre-load the cache or maybe you want to login as a user and then measure a URL.

We use the NodeJs version of Selenium, you can find the API documentation here.

Login example

Create a script where you login the user. This is an example to login the user at Wikipedia. Create a file login.js with the following.

module.exports = {
  run(context) {
    return context.runWithDriver((driver) => {
      // Go to Wikipedias login URL
      return driver.get('')
        .then(() => {
          // You need to find the form, the login input fields and the
          // password field. Just add you name and password and submit the form
          // For more docs, checkout the NodeJS Selenium version

          // we fetch the selenium webdriver from context
          var webdriver = context.webdriver;
          // before you start, make your username and password
          var userName = 'YOUR_USERNAME_HERE';
          var password = 'YOUR_PASSWORD_HERE';
          var loginForm = driver.findElement(webdriver.By.css('form'));
          var loginInput = driver.findElement('wpName1'));
          var passwordInput = driver.findElement('wpPassword1'));
          return loginForm.submit();

Make sure to change the username & password first

Then run it like this:

$ --preScript login.js

The script will then login the user and access and measure that page.

Checkout the magic row:

var webdriver = context.webdriver;

From the context object you get a hold of the Selenium Webdriver object that you can use to find elements on the page.

Test a page with primed cache

One other thing you can do with a pre script is simulate a user that browsed a couple of pages and then measure the performance of a page (by default the cache is emptied when you use

Create a pre script (pre.js):

module.exports = {
  run(context) {
    return context.runWithDriver((driver) => {
      // Go to the start page of
      return driver.get('');

And then run it like this:

$ --preScript pre.js -b chrome

The browser will then first access, fill the cache and then go to where we will collect all the metrics.

Firefox (and/or the HAR Export trigger) has a bug that reports requests in the HAR file as 200 not flagging that they are from the local browser cache. Follow the bug here. We recommend you use Chrome until this is fixed.