守夜页面对象(Nightwatch Page Object)

编程入门 行业动态 更新时间:2024-10-09 01:15:19
守夜页面对象(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 function

Unable 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.js

In 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]

更多推荐

本文发布于:2023-04-29 09:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   页面   Nightwatch   Page   Object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!