Nightwatch(PageObject) access elements from different page object

雨燕双飞 提交于 2019-12-12 18:20:31

问题


Is there a way to access elements defined in one page object file from another page object file?

Example: If we need to access '@usernameInput' from LoginPage.ts file, do we need to duplicate it from HomePage.ts ? Is there any other way?

HomePage.ts
  const page: PageObject = {
    url: ...,
    commands: [
    enterUsername: (query: string) => {
      return this
        .waitForElementVisible('@usernameInput')
  }],
    elements: {
      usernameInput: {
        locateStrategy: 'xpath',
        selector: '//input[@name="Username"]',
      }
  };

LoginPage.js
  const page: PageObject = {
    url: ...,
    commands: [
    enterUsername: (query: string) => {
      return this
        .waitForElementVisible('@usernameInput')
  }],
    elements: {}
  };

回答1:


You can access one page object from another by using this.api.page.pageObjectName.

In your example you would just do

const loginPage = this.api.page.loginPage();

And then to get the usernameInput element you can just do this

const usernameInput = loginPage.elements.usernameInput.selector;

So your enterUsername function should look something like the following

enterUsername: (query: string) => {
  const loginPage = this.api.page.loginPage();
  const usernameInput = loginPage.elements.usernameInput.selector;

  return this
    .waitForElementVisible(usernameInput)
    .setValue(usernameInput, query);
}



回答2:


Use LocalStoreage to keep your data and access in all the page using Javascript or Jquery

Set your Object

localStorage.setItem("PortPlans", PortPlans);

call your localstorage in another page

var PortPlans = localStorage.getItem("PortPlans");

console.log(PortPlans);



来源:https://stackoverflow.com/questions/50029800/nightwatchpageobject-access-elements-from-different-page-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!