【问题标题】:Access object property that is outside of array访问数组外的对象属性
【发布时间】:2020-06-11 11:38:48
【问题描述】:

我的页面对象是结构化的,因此我拥有一个对象中的所有元素,然后是一个对象数组,其中包含有关可以循环测试最大字符长度和错误文本的字段的数据。

我希望定位器引用数组外部的属性,以便在元素更改时不需要更新值两次。

以页面对象的片段为例...

module.exports = {
    siteName: element(by.id('P662_NAME')),
    fields: [
        {
            name: 'site name',
            max: 45,
            locator: element(by.id('P662_NAME'))
        }
    ]
}

我尝试过使用以下方法,但没有成功...

this.siteName, this.siteName, module.exports.siteName

有没有办法做到这一点?

【问题讨论】:

  • 绝对有必要在字段对象本身之外使用定位器吗?

标签: javascript node.js json testing protractor


【解决方案1】:

您的导出看起来不错。正确导入。

【讨论】:

    【解决方案2】:

    您可以将siteName 设置为另一个变量,并在您的fields 对象中引用它,如下所示:

    let siteName = "foo"; // now, updating this variable will also update the one in fields
    
    let fields = [{
      // other props
      locator: siteName
    }];
    
    console.log(fields[0].locator); // expects "foo"
    
    // module.exports = { siteName, fields };

    【讨论】:

      【解决方案3】:

      试试这个: 从这样的文件中导出

      沙盒:https://codesandbox.io/s/compassionate-bas-fg1c2

      var siteName = "dsdsd";
      var fields = [
        {
          name: "site name",
          max: 45,
          locator: "dsdsd"
        }
      ];
      module.exports = {
        siteName,
        fields
      };;
      

      像这样导入它:

      import { siteName } from "./test.js";
      console.log(siteName);
      

      【讨论】:

        猜你喜欢
        • 2015-08-03
        • 1970-01-01
        • 1970-01-01
        • 2013-06-06
        • 2018-10-18
        • 2016-02-26
        • 2018-05-21
        • 2018-11-12
        相关资源
        最近更新 更多