我可以在nightwatch.js中创建可重复使用的测试步骤吗?

chr*_*ton 17 selenium node.js nightwatch.js

我希望在我的nightwatch.js测试中创建可重用的组件.

即.登录Web应用程序,注销Web应用程序

以可重用的方式创建这些步骤的最佳方法/模式是什么?

Cor*_*ter 28

您可以为其创建自定义命令:http://nightwatchjs.org/guide#writing-custom-commands

  1. 在nightwatch.json中指定包含自定义命令文件的文件夹的路径
  2. 创建一个js文件,并将其命名为自定义命令应该是如何命名的(即login.js)
  3. 写下你需要的代码:

exports.command = function(username, password) {
    
    this
        .waitForElementVisible('#password', 4000)
        .setValue('#password', password)
        .waitForElementVisible('#username', 1000)
        .setValue('#username', username)
        .waitForElementVisible('#sign_in', 1000)
        .click('#sign_in')
        .waitForElementVisible('h1.folder-title', 10000)
        
        return this;
};
Run Code Online (Sandbox Code Playgroud)

  1. 在测试中使用自定义命令:

.login("your_username", "your_password")
Run Code Online (Sandbox Code Playgroud)