0

Here are my simple globals which are called by EX: this.u5boys outside of a loop.

u5boys: {
    xpath: "option[contains(text(), '5 Boys')]"
  },
  u6boys: {
    xpath: "option[contains(text(), '6 Boys')]"
  },

Here is a simple loop that will try to click on the literal string "this.u5boys" when run. how do I make it process the this.u5boys into the global xpath identifier stated above in the same file?

ctrlClick5To6Folders(){

    for(var i = 5; i < 7; i++){
      boysaction = "this.u" + i + "boys";
      I.click(boysaction);
    }

  },

How do I tell it to process the value of this.u5boys before running the I.click()function?

Legako
  • 33
  • 2
  • 8
  • What error do you get? – Moe kanan Oct 31 '17 at 16:52
  • Hi @Moekanan, thanks for your reply. The above code runs without error but instead of changing the value of this.boysactions into "option[contains(text(), '5 Boys')]" as I need, it comes out as "this.u5boys". I feel like I might only be missing some kind of escape character or quotes. – Legako Oct 31 '17 at 17:10
  • Where do you have this saved: u5boys: { xpath: "option[contains(text(), '5 Boys')]" } – Moe kanan Oct 31 '17 at 18:01
  • u5boys: { xpath: "option[contains(text(), '5 Boys')]" } is saved within the same file as the for loop, but outside of it's parent function and loop. – Legako Oct 31 '17 at 18:05

2 Answers2

1

it comes out as "this.u5boys"

And that's normal, given this:

for(var i = 5; i < 7; i++){
  boysaction = "this.u" + i + "boys";
  I.click(boysaction);
}

boysaction = "this.u" + i + "boys"; "boysaction" is just a string object in this context.

Something like this should work

for(var i = 5; i < 7; i++){
  boysaction = "this.u" + i + "boys";
  I.click(window[boysaction].xpath);//this one
  I.click(this[boysaction].xpath);//or this one
}

Here, "boysaction" is the string that represents the name of the variable available in the window context.

blaze_125
  • 2,262
  • 1
  • 9
  • 19
1

You need to use the Object.keys to get the keys and use them.

Try this:

var globals = {
  u5boys: {
    xpath: "option[contains(text(), '5 Boys')]"
  },
  u6boys: {
    xpath: "option[contains(text(), '6 Boys')]"
  }
}

Object.keys(globals).forEach((key) => console.log(globals[key].xpath));

This will print out the xpath in the console. if you want to invoke a costume function, you can do something like this:

Object.keys(globals).forEach((key) => I.click(globals[key].xpath)); 

And if you only want the 5th and 6th, can do something like this:

Object.keys(globals).forEach((key) => {
    if(key != "u5boys" && key != "u6boys")
        return;

    console.log(globals[key].xpath);
})
Moe kanan
  • 189
  • 12