在Geb
和WATIR
中,我们使用某些关键字来访问我们在页面类中指定的page_url
。例如。 Geb
中的 to
关键字和 WATIR
中的 visit
关键字。
我们可以在 nightwatch.js
中使用类似的东西。这是我尝试过的,但它给出了错误:
我已经尝试过:
module.exports = {
url: function () {
return this.api.globals.launchUrl + "/goto/desiredPage.html";
},
commands: [pageCommands],
elements: {}
};
在页面类中,我将其用作:
desiredPage
.url()
.foo()
.bar();
client.end();
但它给出错误.url不是函数
。
最佳答案
您可以在 nightwatch 文件夹中查看 nightwatch 示例,例如:
[page-objects/home.js]
var searchCommands = {
submit: function() {
this.waitForElementVisible('@submitButton', 3000)
.click('@submitButton')
.api.pause(1000);
return this; // Return page object for chaining
}
};
module.exports = {
url: 'http://google.com',
commands: [searchCommands],
elements: {
searchBar: { selector: 'input[name=q]' },
submitButton: { selector: 'button[type=submit]' }
}
};
然后在测试中:
/* jshint expr: true */
module.exports = {
'Demo Google search test using page objects' : function (client) {
var homePage = client.page.home();
homePage.navigate();
homePage.expect.element('@searchBar').to.be.enabled;
homePage
.setValue('@searchBar', 'Nightwatch.js')
.submit();
var resultsPage = client.page.searchResults();
resultsPage.expect.element('@results').to.be.present.after(2000);
resultsPage.expect.element('@results').to.contain.text('Nightwatch.js');
resultsPage.expect.section('@menu').to.be.visible;
var menuSection = resultsPage.section.menu;
menuSection.expect.element('@web').to.be.visible;
menuSection.expect.element('@video').to.be.visible;
menuSection.expect.element('@images').to.be.visible;
menuSection.expect.element('@shopping').to.be.visible;
menuSection.productIsSelected('@web', function(result) {
this.assert.ok(result, 'Web results are shown by default on search results page');
});
client.end();
}
};
因此页面中不存在“url()”命令,您需要在页面中定义 url 并使用“navigate()”代替。
关于javascript - 页面对象: visit <page_url> in nightwatch. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38282861/