Nightwatch.js Async Selenium Operations

烂漫一生 提交于 2019-12-25 09:11:07

问题


Say I have a Nightwatch test with two steps that fill out a form. As part of the first step, I need to dynamically query some data from the page (using the Selenium api), then use that data to make additional selenium calls, and use the final result to make custom assertions. The reason I need to use the Selenium api is not that I do not know how to use the normal Nightwatch assertions, but rather that the normal assertions are not sufficient to test the types of things I want to test. Additionally, at the end of the first step a button is clicked that moves on to the next part of the form (in preparation for the second step).

(code version):

module.exports = {
  'Part 1': (client) => {

    // ... do cool stuff

    client.SOME_SELENIUM_COMMAND(...SOME_ARGS..., (result) => {

      client.SOME_OTHER_SELENIUM_COMMAND(...SOME_OTHER_ARGS..., (result2) => {
        // ... do more cool stuff with result2
      });
    });

    // moves the page onto part 2
    client.click(SOME_BUTTON);
  },

  'Part 2': (client) => {
    // ... part 2 stuff
  }
};

My problem is this: the test moves on to the second part before the selenium command result part resolves.

I am aware that internally Nightwatch uses some kind of event queue and EventEmitters to make sure that commands are executed in the correct order however it appears that the click command at the end of part one is being queued up before the commands in the callback can be.


回答1:


You can use the Nightwatch perform command to perform some other commands and call done() when you want the test to continue to Part 2. You would do something like this:

module.exports = {

    'Part 1': (client) => {

    // ... do cool stuff

    client
    .perform(function(client, done) {

         client.SOME_SELENIUM_COMMAND(...SOME_ARGS..., (result) => {

             client.SOME_OTHER_SELENIUM_COMMAND(...SOME_OTHER_ARGS..., (result2) => {
                 // ... do more cool stuff with result2
                 // Call done to continue to Part 2
                 done();
             });
        });
    })
    // moves the page onto part 2
    .click(SOME_BUTTON);
  },

  'Part 2': (client) => {
    // ... part 2 stuff
  }
};


来源:https://stackoverflow.com/questions/38339987/nightwatch-js-async-selenium-operations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!