自定义命令
Nightwatch 支持创建可以模拟击键,鼠标点击和其他输入的自定义命令。自定义命令可以与其他 Nightwatch 命令链接,如下所示:
module.exports = {
"Login App" : function (client) {
client
.url("http://localhost:3000")
.login("janedoe@somewhere.com", "janedoe123")
.end();
}
};
要启用此功能,请在 ./tests/nightwatch/commands/login
中定义命令,如下所示:
exports.command = function(username, password) {
this
.verify.elementPresent('#login')
// we clear the input in case there's any data remaining from previous visits
.clearValue("#emailInput")
.clearValue("#passwordInput")
// we simulate key presses
.setValue("#emailInput", username)
.setValue("#passwordInput", password)
// and we simulate a mouse click
.click("#signInToAppButton").pause(1000)
return this; // allows the command to be chained.
};
要使这一切正常,你需要将 id
属性添加到登录页面。在某种程度上,它需要大致如下所示:
<template name="login">
<div id="login">
<input id="emailInput" name="email" type="email" />
<input id="passwordInput" name="password" type="password" />
<button id="#signInToAppButton">Sign In</button>
</div>
</template>