Nightwatch(PageObject)来自不同页面对象的访问元素 [英] Nightwatch(PageObject) access elements from different page object

查看:117
本文介绍了Nightwatch(PageObject)来自不同页面对象的访问元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从另一个页面目标文件访问一个页面目标文件中定义的元素?

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

示例: 如果我们需要从LoginPage.ts文件访问'@usernameInput',是否需要从HomePage.ts复制它?还有其他办法吗?

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: {}
  };

推荐答案

您可以使用this.api.page.pageObjectName来访问另一个页面对象.

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

在您的示例中,您只需

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

然后要获取usernameInput元素,您可以执行此操作

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

const usernameInput = loginPage.elements.usernameInput.selector;

所以您的enterUsername函数应类似于以下内容

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);
}

这篇关于Nightwatch(PageObject)来自不同页面对象的访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆