0

i am writing a code to automate android and IOS using webdriverio I have a piece of js code where i have defined some getter and depending upon the user input, i want to call a getter


var assert = require('assert')
const Page = require('./Page.js')
const SELECTORS = {
  ANDROID: {
    landing_header: '~landing_header',
    sys_house: '~sys_house',
    aero_center: '~aero_center',

  },
  IOS: {
    //IOS Page Object
  },
}
class Landing extends Page {

get landingHeader() {
    return browser.isAndroid ? $$(SELECTORS.ANDROID.landing_header)[0] : $$(SELECTORS.ANDROID.landing_header)[0]
  }
  get sysHouseTile() {
    return browser.isAndroid ? $$(SELECTORS.ANDROID.sys_house)[0] : $$(SELECTORS.ANDROID.sys_house)[0]
  }
  get settingsCenterTile() {
    return browser.isAndroid ? $$(SELECTORS.ANDROID.aero_center)[0] : $$(SELECTORS.ANDROID.aero_center)[0]
  }

  navigateLandingPage(page) {
    if(page=="settings") {
      var lObj=eval(page+"CenterTile");
      this.settingsCenterTile.click();//this is working fine
      this.lObj.click();//this is not working
    }
    browser.pause(3000)
  }
}
module.exports = new Landing()

navigateLandingPage() method is being called from another js file Now my issue is depending on the page input, i want to click any one method and i can update the getter runtime, but don't know how to call it. this.lObj.click()

georg
  • 211,518
  • 52
  • 313
  • 390
  • It's a bit unclear what you're asking. Anyway, getters are not called, rather the value is just retrieved. – Teemu Sep 05 '19 at 11:46
  • What is the value of `page` argument? I suppose you need to read https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable – Teemu Sep 05 '19 at 11:49
  • Just use square bracket notation: `var lObj = this[page+"CenterTile"];` – trincot Sep 05 '19 at 11:51
  • @Teemu page is value that user has passed and it is coming from another page. according to this input, code will select and click the getter. – PRASHANT TRIPATHI Sep 05 '19 at 12:04

1 Answers1

0

Do not use eval() for this, you can use the computed property syntax object[propName] to dynamically invoke getters.

Just take a look at my sample code below, in the getters I am returning a function when it is invoked. When I accessed the property dynamically I invoked the getter and it returned me a function and i executed that:

class Landing{

 get landingHeader() {
   return () => {
     console.log("landingHeader");
   }
 }
 get sysHouseTile() {
  return () => {
     console.log("sysHouseTile");
  }
 }
 get settingsCenterTile() {
    return () => {
     console.log("settingsCenterTile");
    }
 }

 navigateLandingPage(page) {
    if(page=="settings") {
      var lObj = this[page+"CenterTile"]();
    }
  }
}
new Landing().navigateLandingPage("settings");
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44