最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I run Nightwatch tests in a specific order? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited May 6, 2022 at 19:42 bad_coder 12.9k20 gold badges54 silver badges88 bronze badges asked Sep 21, 2015 at 20:26 Sua MoralesSua Morales 91611 silver badges22 bronze badges 2
  • You can run a single testcase by using --testcase. I suppose you could just run them one-at-a-time using that. – Brian Commented Sep 23, 2015 at 15:39
  • 1 @Brian No, because it Nightwatch creates a separate session every time we run the test the way you suggest. – Sua Morales Commented Oct 2, 2015 at 17:02
Add a comment  | 

5 Answers 5

Reset to default 14

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.

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

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));
    }
};

If you have the files first.js and second.js then create a new file main.js and import all the functions present in these files into main.js.

first.js:

module.exports = {
 'function1' function(browser){
    //test code
 },
 'function11' function(browser){
    //test code
  }
}

second.js:

module.exports = {
 'function2' function(browser){
    //test code
}
}

main.js:

const { function1,function11 } = require('./path/to/first.js')
const { function2 } = require('./path/to/second.js')
module.exports = {
    //run the functions mentioned in a order which you want
    run: function (browser) {
     funtion1(browser)
     function11(browser)
     function2(browser)
    }
 }

Now execute the main.js file.

This has a solution: https://github.com/nightwatchjs/nightwatch/issues/649 It enables nightwatch to run tests sequentially instead of in parallel. Disable test_workers in the config file by changing "true" to "false" here:

{
  "test_workers": {
    enabled: false,
    workers: 'auto'
  }
},
发布评论

评论列表(0)

  1. 暂无评论