尝试使用Nightwatch + Browserstack设置简单的登录测试,失败



使用"nightwatch": "^1.0.11""browserstack-local": "^1.3.4"

我已经尝试了很多方法,但我无法运行文档中的示例。

我有一个带有页面对象的简单登录页面

module.exports = {
url: function() {
return this.api.launchUrl + '/login';
},
elements: {
email: {
selector: '#user_login_email'
},
password: {
selector: '#user_login_password'
},
button: {
selector: '#login-btn'
}
}
};

然后我想运行测试以登录站点(注意:第一个屏幕截图很好(

module.exports = {
before(client) {
client.maximizeWindow();
},
'Login' : function (client) {
var login = client.page.login();
login.navigate();
client.waitForElementPresent('body', 500)
.saveScreenshot('tests_output/login.png');
login.setValue('@email', 'test@user.email')
.setValue('@password', 'Pa55w0rd')
.click('@button');
client.pause(1000)
.saveScreenshot('tests_output/login-complete.png');
},
after(client) {
client.end();
}
};

尝试通过浏览器堆栈(使用本地 URL(运行测试时出现以下错误

✖ login.test
– Login (5.196s)
An error occurred while running .setValue() command on <Element [name=@email]>: Error: First argument passed to .elementIdValue() should be a web element ID string. Received object.
at Function.validateElementId (node_modules/nightwatch/lib/api/protocol.js:36:19)
at ProtocolActions.elementIdValue (node_modules/nightwatch/lib/api/protocol.js:951:25)
at transport.locateElement.then.result (node_modules/nightwatch/lib/api-loader/element-command.js:106:54)
at process._tickCallback (internal/process/next_tick.js:68:7)
at process._tickCallback (internal/process/next_tick.js:68:7)
at process._tickCallback (internal/process/next_tick.js:68:7)

An error occurred while running .setValue() command on <Element [name=@password]>: Error: First argument passed to .elementIdValue() should be a web element ID string. Received object.
[...]
An error occurred while running .click() command on <Element [name=@button]>: Error: First argument passed to .elementIdClick() should be a web element ID string. Received object.
[...]

我已经运行了成功的测试;只是没有使用记录的示例。

例如,下面的代码可以很好地关闭cookie消息

client
.waitForElementPresent('.cookie-message__button', 5, true, function(result) {
client.elementIdClick(result.value[0].ELEMENT);
})
.saveScreenshot('tests_output/cookie-closed.png');

但显然它真的很长篇大论。

对我做错的事情的任何帮助都会很棒。

以以下内容为例进行 Nightwatch 配置:与本地运行器 https://github.com/browserstack/nightwatch-browserstack/blob/master/conf/local.conf.js 并在 3 个浏览器中并行运行测试:https://github.com/browserstack/nightwatch-browserstack/blob/master/scripts/local.runner.js

TL;DR:文档示例不适用于版本 1.0.11;但是 Nightwatch 和 Browserstack 配置正确,可以运行一个套件(在特拉维斯和本地(,只是使用非常冗长的代码,我一起破解了这些代码。

我在这里为页面对象模型创建了示例项目。此外,还添加了使用page_objects_path的示例登录测试。

还添加了最新守夜人的兼容性。最新版本的夜巡似乎有符合w3c的更改。因此,我们需要将'browserName'用作能力,而不是'browser'

最新更新