Javascript 页面对象:访问<;页面url>;在nightwatch.js中
在Javascript 页面对象:访问<;页面url>;在nightwatch.js中,javascript,nightwatch.js,Javascript,Nightwatch.js,在Geb和WATIR中,我们使用某些关键字访问我们在page类中指定的页面url。例如Geb中的to关键字和WATIR中的visit关键字 我们可以在nightwatch.js中使用的类似功能。这是我尝试过的,但它给出了错误: 我试过: module.exports = { url: function () { return this.api.globals.launchUrl + "/goto/desiredPage.html"; }, commands
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: {}
};
在page类中,我将其用作:
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()”命令。您需要在te页面中定义url,并使用“navigate()”。您可以在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()”命令。您需要在te页面中定义url,并使用“navigate()”