How to run a single test in nightwatch

32,633

Solution 1

A new parameter --testcase has been added to run a specified testcase.

nightwatch.js --test tests\demo.js --testcase "Test 1"

It's a new feature since the v0.6.0

https://github.com/beatfactor/nightwatch/releases/tag/v0.6.0

Solution 2

You must use specific tags before function and separate all functions in diferent files under tests directory, and then call command with --tag argument. See wiki nightwatch tags page and watch this example:

// --- file1.js ---
module.exports = {
    tags: ['login'],
    'Test 1':function(){
        //TODO test 1
    }
};

// --- file2.js ---
module.exports = {
    tags: ['special', 'createUser'],
    'Test 2':function(){
        //TODO test 2
    },
};

// --- file3.js ---
module.exports = {
    tags: ['logoff', 'special'],
    'Test 3':function(){
        //TODO test 3
    },
}

If you run:

nightwatch.js --tag login

only runs Test 1, however if you run:

nightwatch.js --tag special

Test 2 and Test 3 will be executed.

You can specific more than one tag

nightwatch.js --tag tag1 --tag tag2

Separate each test function is mandatory because Nightwatch handled with filematcher each file. See Github code.

PD: If file has syntax errors, is possible that test don't run or test hasn't been found

Solution 3

The --testcase flag can since version 0.6 be used to run a single test from the commandline, e.g.

nightwatch.js --test tests\demo.js --testcase "Test 1"

This could be done using either test groups or test tags. You can also execute a single test with the --test flag, e.g.

nightwatch.js --test tests\demo.js

Solution 4

For me, it only works with:

npm run test -- tests/01_login.js --testcase "Should login into Dashboard"

npm run <script> -- <test suite path> --testcase "<test case>"

my script in package.json:

"test": "env-cmd -f ./.env nightwatch --retries 2 --env selenium.chrome",

at nightwatch version 1.3.4

You can also use tags:

npm run <script> -- <enviroment> <tag>
npm run test -- --env chrome --tag login

just add it to your test case:

module.exports = {
  '@tags': ['login', 'sanity', 'zero1'],
...
}
Share:
32,633
Mohamed El Mahallawy
Author by

Mohamed El Mahallawy

Updated on July 09, 2022

Comments

  • Mohamed El Mahallawy
    Mohamed El Mahallawy almost 2 years

    How do I run only Test 3 from the following tests?

    module.exports = {
      'Test 1':function(){},
      'Test 2':function(){}
      'Test 3':function(){}
    }
    
  • Mohamed El Mahallawy
    Mohamed El Mahallawy about 9 years
    This is pretty awesome - I think what would be optimal is if it worked like mocha and grepping. That's what I was hoping for!
  • albertoiNET
    albertoiNET about 9 years
    Yeah, but it doesn't work for this way. If you like, check filematcher.js util lib from github nightwatch repo
  • Mohamed El Mahallawy
    Mohamed El Mahallawy about 9 years
    Woot! Thanks @NicoPennec
  • Jeremy
    Jeremy over 5 years
    @albertoiNET could you provide a solution where using nightwatch programmatically? Meaning, running a node script that invokes nightwatch.runner