Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run Nightwatch tests in a specific order?

I have several tests which test the UI and also serve to create data along the way.

A separate set of tests rely on this data, meaning that these must run only after the first set have run.

I know about running a group of them, or running them with tags, but how can I run them in a specific order?

like image 594
Sua Morales Avatar asked Sep 21 '15 20:09

Sua Morales


People also ask

How do you run multiple test cases on Nightwatch?

To execute tests in multiple browsers, you need to add the desired capabilities of the browsers and Test_worker configurations in nightwatch. json file. For example if you want to execute tests in three browsers parallely - Chrome, Firefox and Opera, your nightwatch. json should something like this.

How do I run a single test file on Nightwatch?

A new parameter --testcase has been added to run a specified testcase. Show activity on this post. Test 2 and Test 3 will be executed. Separate each test function is mandatory because Nightwatch handled with filematcher each file.

What is the command to run the script Nightwatch?

You can add Nightwatch to your project simply by running npm install nightwatch --save-dev . This places the Nightwatch executable in your ./node_modules/.


3 Answers

To control the order (and also to use a common module for authentication) I used a "main" test module and imported the tests in the order I wanted:

Inside main.test.js

// import test modules
const first = require('./first.test.js');
const second = require('./second.test.js');
module.exports = {
    before(){
         // login, etc.
    },
    'first': (browser) => {
         first.run(browser);
    },
    'second': (browser) => {
         second.run(browser);
    },
}

and in first.test.js

var tests = {
    'google': (browser) => {
        browser.url('https://google.com';
    },
    'cnn': (browser) => {
        browser.url('https://cnn.com';
    }
};

module.exports = {
    // allow nightwatch to run test module only inside _main
    '@disabled': true,
    'run': (browser) => {
        // call all functions inside tests
        Object.values(tests)
            .filter(f => typeof f === 'function')
            .forEach(f => f(browser));
    }
};
like image 75
ow3n Avatar answered Oct 06 '22 14:10

ow3n


Nightwatch will run each test within a particular file in order, so one (naive) solution would be to put every test in the same file, in the order you want them to run.

This will get unwieldy if you have too many tests for a single file. To get around this, you can take advantage of Nightwatch running each test file in alphabetical order. One way to do this would be to prefix each test file with a number indicating the order you want them to be run in. For example, if you had two test files, before.js and after.js, and you wanted before.js to run first, you could just change the names of the files to 01before.js and 02after.js. This will make Nightwatch read the files in the order you want.

like image 45
Josh Dripps Avatar answered Oct 06 '22 15:10

Josh Dripps


This isn't a great answer but it works: numerically sequence your test files.

0001_first_test_I_want_to_run.js
0002_second_test_I_want_to_run.js
...
9999_last_test_I-want_to_run.js
like image 31
QualiT Avatar answered Oct 06 '22 13:10

QualiT