自定義命令
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>