Nightwatch.js异步Selenium操作 [英] Nightwatch.js Async Selenium Operations

查看:103
本文介绍了Nightwatch.js异步Selenium操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个Nightwatch测试,其中两个步骤填写了表格.作为第一步的一部分,我需要从页面动态查询一些数据(使用Selenium api),然后使用该数据进行附加的selenium调用,并使用最终结果进行自定义声明.我需要使用Selenium api的原因不是我不知道如何使用常规的Nightwatch断言,而是常规的断言不足以测试我要测试的事物的类型.此外,在第一步结束时,单击一个按钮,该按钮会移至表单的下一部分(为第二步做准备).

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).

(代码版本):

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.

我知道Nightwatch在内部使用某种事件队列和EventEmitters来确保命令以正确的顺序执行,但是在第1部分末尾的click命令似乎在队列中的命令之前排队了回调即可.

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.

推荐答案

您可以使用夜间观察执行命令执行其他命令,并在要继续第2部分的测试时调用done().您将执行以下操作:

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

这篇关于Nightwatch.js异步Selenium操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆