守夜页面对象(Nightwatch Page Object)
无法找到页面对象,我在这里错过了什么?
Nightwatch 0.9.16
在nightwatch.json中
"page_objects_path": "pages",在实际文件夹中
\pages\MyLegacyPage.js在MyLegacyPage.js中
module.exports = { myPauseMethod: function (browser) { browser.pause(1000); return this; } };在测试中
describe('CTA', function () { it('page objects tests', function (browser) { console.log('browser.page.MyLegacyPage() = ' + JSON.stringify(browser.page.MyLegacyPage())); var myPageObject = browser.page.MyLegacyPage(); myPageObject.myPauseMethod(browser); }); });产量
browser.page.MyLegacyPage() = {} TypeError: myPageObject.myPauseMethod is not a functionUnable to find page objects, what am I missing here?
Nightwatch 0.9.16
In nightwatch.json
"page_objects_path": "pages",In actual folder
\pages\MyLegacyPage.jsIn MyLegacyPage.js
module.exports = { myPauseMethod: function (browser) { browser.pause(1000); return this; } };In test
describe('CTA', function () { it('page objects tests', function (browser) { console.log('browser.page.MyLegacyPage() = ' + JSON.stringify(browser.page.MyLegacyPage())); var myPageObject = browser.page.MyLegacyPage(); myPageObject.myPauseMethod(browser); }); });Output
browser.page.MyLegacyPage() = {} TypeError: myPageObject.myPauseMethod is not a function最满意答案
也许这个POM的例子可以帮助您解决问题:
var dashCommands = { uploadAvatar:function(){ return this.waitForElementVisible('@profileIcon', TIMEOUT) .click('@profileIcon') .waitForElementVisible('@avatarUpload', TIMEOUT) .moveToElement('@avatarUpload', 0,0) .click('@avatarUpload') .waitForElementVisible('@profileImageUploadOverlay', TIMEOUT) .assert.visible('@closeButton') .click('@selectPicture') } module.exports = { commands:[dashCommands], elements: { dashLogo : { selector: 'div.topbar__title-wrap.topbar-title > h1'}, profileAvatar: { selector: 'span:nth-child(4) > div > img'}, searchField: { selector: 'div.topbar__search-feald-wrap > input'}, topicsColumn: { selector: 'div.inner-dashboard-wrap.topic-wrap'}, conclusionsColumn: { selector: 'div.inner-dashboard-wrap.conclusion-wrap'}, messagesColumn: { selector: 'div.inner-dashboard-wrap.messages-wrap'}, bookmarksColumn: { selector: 'div.inner-dashboard-wrap.request-wrap'}, profileIcon: { selector: 'span:nth-child(4) > div'}, avatarUpload: {selector: '//*[@id="profile-section"]/div[1]/div[1]/img', locateStrategy: 'xpath' }, selectPicture: { selector: '#dialogContent_profile-image-modal'}, profileImageUploadOverlay: { selector: '.layout-column button'}, closeButton: { selector: '.ng-scope.icon-close'} }, }这意味着您必须在var内部创建函数,然后您必须在module.exports部分中使用以下代码行插入该命令:commands:[nameOfYourVar]
Maybe this example of POM might help you solve your issue:
var dashCommands = { uploadAvatar:function(){ return this.waitForElementVisible('@profileIcon', TIMEOUT) .click('@profileIcon') .waitForElementVisible('@avatarUpload', TIMEOUT) .moveToElement('@avatarUpload', 0,0) .click('@avatarUpload') .waitForElementVisible('@profileImageUploadOverlay', TIMEOUT) .assert.visible('@closeButton') .click('@selectPicture') } module.exports = { commands:[dashCommands], elements: { dashLogo : { selector: 'div.topbar__title-wrap.topbar-title > h1'}, profileAvatar: { selector: 'span:nth-child(4) > div > img'}, searchField: { selector: 'div.topbar__search-feald-wrap > input'}, topicsColumn: { selector: 'div.inner-dashboard-wrap.topic-wrap'}, conclusionsColumn: { selector: 'div.inner-dashboard-wrap.conclusion-wrap'}, messagesColumn: { selector: 'div.inner-dashboard-wrap.messages-wrap'}, bookmarksColumn: { selector: 'div.inner-dashboard-wrap.request-wrap'}, profileIcon: { selector: 'span:nth-child(4) > div'}, avatarUpload: {selector: '//*[@id="profile-section"]/div[1]/div[1]/img', locateStrategy: 'xpath' }, selectPicture: { selector: '#dialogContent_profile-image-modal'}, profileImageUploadOverlay: { selector: '.layout-column button'}, closeButton: { selector: '.ng-scope.icon-close'} }, }which means you have to make your function inside the var and then you have to insert that commands with the following line of code in module.exports part: commands:[nameOfYourVar]
更多推荐
发布评论