跳过正文
Background Image
  1. Posts/

Nightwatch 异步操作

·213 字·1 分钟· ·
沈显鹏
作者
沈显鹏

在自动化测试中常常需要通过一个 command(或 function )中返回的值来进行下一步的操作,JavaScript 与 JAVA 在调用返回值时有所不同,JS 中需要特定的写法来进行这种异步操作。

以下面的 get License 数量为例,首先需要 get 一次 License 数量,然后进行一些列操作之后,再一次 get License 数量,比较这两次 License 数量值。

getLicenseNum 方法:

getLicenseNum: function (cb) {
    const license = 'ul > li.license-id.ng-binding';
    this.waitForElementVisible(license, 5000);
    this.api.elements('css selector', license, function (result) {
        cb(result.value.length);
    return this;
    });
}

对两次得到的 License num 进行比较:

'JavaScrpit asynchronous operation': function(client) {
    const license = client.page.license();
    let num1, num2;
    license.getLicenseNum(function(num) {
        num1 = num;
    });
    license.getLicenseNum(function(num) {
        num2 = num;
    });
    client.perform(function() {
        client.assert.equal(num2 - num1, 1, 'license number increase 1');
    });
}

相关文章

Nightwatch 模拟键盘操作
·275 字·1 分钟
本文介绍了如何在 Nightwatch.js 中模拟键盘操作,包括输入文本和组合键操作的示例代码。
Nightwatchjs 中文参考手册
·302 字·1 分钟
Nightwatch.js 是一个基于 Node.js 的自动化测试框架,本文提供了 Nightwatch.js 的中文参考手册和示例代码。