简体   繁体   中英

Access object property that is outside of array

My page object is structured so that I have all of the elements in an object and then an array of objects containing data about the fields that can be looped over to test max char length and error texts.

I would like the locator to reference a property that is outside the array so that the value does not need to be updated twice if the element changed.

Snippet from page object as an example...

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

I have tried using the following with no luck...

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

Is there a way to do this?

Try this : Export from a file like this

Sandbox: https://codesandbox.io/s/compassionate-bas-fg1c2

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

Get it imported like this:

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

Your exporting looks pretty good. Import it correctly.

在此处输入图片说明

What you could do is set siteName as another variable and reference that in your fields object like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM