自动重复 Nightwatch 测试

Repeat Nightwatch test automatically

有谁知道我可以自动重新运行 Nightwatch 测试一定次数的方法吗?

我有以下代码:

module.exports = {
  'Log into system - create order': function (client) {
    client
      .url('XXXX')
      .waitForElementVisible('body', 1000)
      .assert.title('Reach - Log in')
      .assert.visible('#UserName')
      .setValue('#UserName', 'XXXX')
      .assert.visible('#Password')
      .setValue('#Password', 'XXXX')
      .assert.visible('input[value="Login"]')
      .click('input[value="Login"]')
      .waitForElementVisible('img.test', 1000)
      .assert.visible('li[title="XXXX"] a[tabindex="5"]')
      .click('li[title="Sales"]')
      .assert.cssClassPresent('li[title="XXXX"]', 'active')
      .click('a[href="/Quotes/Add"]')
      .waitForElementVisible('#s2id_CustomerId_Remote', 1000)
      .click('#s2id_CustomerId_Remote')
      .assert.visible('#s2id_autogen2_search')
      .setValue('#s2id_autogen2_search', 'bik')
      .waitForElementVisible('.select2-highlighted', 1000)
      .click('.select2-highlighted')
      .waitForElementVisible('#customerNotes', 1000)
      .click('#s2id_ProductId_Remote')
      .assert.visible('#s2id_autogen3_search')
      .setValue('#s2id_autogen3_search', '123XP')
      .pause(5000)
      .assert.visible('.select2-highlighted')
      .click('.select2-highlighted')
      .pause(5000)
      .assert.visible('.ui-sortable > tr')
      .setValue('#Quote_PONumber', 'abc123')
      .click('input[value="Create Order"]')
      .waitForElementVisible('.ac-order-number', 1000)
      .assert.visible('a[data-value="abc123"]')
      .pause(5000)
      .end()
  }
}

而不是 .end() 测试我想 .rerun() 测试说 30 次。我在文档中的任何地方都看不到执行此操作的选项。

非常感谢。

您可以将命令包装在 client.perform() 和 for 循环中

client.perform(function(){
    for (i = 0; i < 29; i++) { 
      client
        .url('XXXX')
        .
        .
        .
        .end();
    }
})

您需要的是一些异步迭代逻辑和 client.perform() 函数:

module.exports = {
  'Log into system - create order': function (client) {
    var currentIteration = 0,
        iterationCount = 30;

    function runTest() {
      client
        .url('XXXX')

        // ... YOUR CODE HERE, WITHOUT .end()

        .perform(function() {
          if (++currentIteration < iterationCount) {
            return runTest();
          }

          client.end(); // After passing 30 iterations end the session
        });
    }

    runTest();
  }
};

如果你想用不同的输入重复他的测试?那么你可以这样做

module.exports = {
  "Login Fail Cases": function(browser) {
    let dataSet = [
      { username: "madhus", pass: "madhus" },
      { username: "admin", pass: "admin" }
    ];

   //will run for 2 times as length of dataset is 2
    dataSet.forEach(function(data) {
      browser
        .url("https://localhost:3000/")
        // you tests here
    }, this);

    // note: end the test outside the loop once all tests are executed
    browser.end();
  }
};