Categories
Automated Testing Selenium WebDriver

WebdriverJs & Mocha in Selenium 3

In upgrading to Selenium 3 we realized that the test hooks that WebdriverJs provides into Mocha are no longer compatible.

Categories
Ask Me Anything Automated Testing Mocha WebDriver

AMA: reusing the same browser for different automated tests

Ankitha asks…

Let’s say we have two tests files inside specs folder
1) test1.js — has 3 tests
2)test2.js– has 3 tests

When I do npm test, a seperate browser is opened for running test1.js and second browser is opened up for running test2.js.
Whereas I want only one browser to be opened.
Like perform test1.js tests close the browser, For test2.js is now open browser again run test2.js. I would like to see tests running sequentially. How can this be achieved?

My response…

You may have seen that we recently open-sourced our automated e2e tests for WordPress.com.

One the huge number of benefits this brings if you can see how we do things 🙂

If you have a look at driverManager.startBrowser, which is what we call from a before hook in each test, you’ll see how we reuse the browser across different tests.

There’s one additional thing you need to do which is close the browser down at the very end. We use a separate file after.js to do this, which we pass to Mocha when running any test.

Categories
Automated Testing Mocha WebDriver

Accessing Mocha Test Metadata in Hooks

In setting up my WebDriverJs tests to run in Mocha I wanted to add some intelligent screenshot taking ability:

  1. Only take screenshots if the test has failed
  2. Include the name of the test in the screenshot file name to easily identify which test it relates to

Both of these involve accessing some metadata about a test in Mocha. Fortunately it’s easy to do:

test.afterEach(function() {
       if (this.currentTest.state == 'failed') {
           var prefix = this.currentTest.title.replace(/[^a-z0-9]/gi, '-').toLowerCase() + '-';
           driver.takeScreenshot().then(function (data) {
               mediaHelper.writeScreenshot(data, prefix);
           });
       }
});

The this.currentTest.state gives us the status of the test (eg. ‘failed’), and this.currentTest.title gives us the title of the lowest level mocha test running (you can use this.currentTest.fullTitle() to get the entire test chain).

Categories
Automated Testing JavaScript Mocha WebDriver

WebDriverJS & Mocha Part 2: Hooks

I recently shared how to get started with WebDriverJS and Mocha.

This post continues on from there: I will share how to create hooks for our Mocha tests.

Categories
Automated Acceptance Testing Automated Testing

Don’t bury your hooks

A slightly technical post here.

If you’re using a BDD framework such as Specflow or JBehave, can I please ask that you don’t bury your hooks. These frameworks provide hooks, or events, you can use to repeat certain things. An example in Specflow is:

[BeforeScenario]
public static void GoToHomePage()
{
Driver.Navigate().GoToUrl(GoogleUrl);
}

You can put these hooks in any steps class, but please, put them in one place, all together, preferably in a class/file named hooks or the like.

I recently came across a code base where these hooks where spread across lots of different steps files, which makes it very confusing and hard to debug as you don’t know where all this code is being called from when you’re running your tests.