简体   繁体   中英

How to call a getter of js file whose name is formed during runtime i.e call a getter of js file with string

I have a javascript class with few getter whose name are almost same with some difference I want to call a getter with its name formed during the runtime as it will reduce lot of lines of codes in our automation scripts




class automationClass extends Page {
/**
define elements
/
get abcObject() {
        return browser.isAndroid ? $("//[@text='clickElement1: ']") : $("//[@text='clickElement1']")
}

get defObject() {
        return browser.isAndroid ? $("//[@text='clickElement2']"): $("//[@text='clickElement2']")
}

get xyzObject() {
         return browser.isAndroid ? $("//[@text='clickElement3']") : $("//[@text='clickElement3']")
}

/*
function to click an object, objParameter parameter coming from another file according to user input
*/
clickObject(objParameter) {
         Var lObj=objParameter+”Object”
this.lObj.click() ////this is not working,,,,,any alternative????
}


}

i have used eval function and was working fine to call a function but eval is not working for class and getter _

You can use the []-syntax to get variable properties of an object.

And there were a lot of syntax errors in there:

  • missed a closing comment tag
  • Var has to be var
  • strings are surrounded by `'" not
class automationClass extends Page {
  /**
  define elements
  */
  get abcObject() {
    return browser.isAndroid ? $("//[@text='clickElement1: ']") : $("//[@text='clickElement1']")
  }

  get defObject() {
    return browser.isAndroid ? $("//[@text='clickElement2']") : $("//[@text='clickElement2']")
  }

  get xyzObject() {
    return browser.isAndroid ? $("//[@text='clickElement3']") : $("//[@text='clickElement3']")
  }

  /*
  function to click an object, objParameter parameter coming from another file according to user input
  */
  clickObject(objParameter) {
    var lObj = objParameter + "Object"
    this[lObj].click() ////this is not working,,,,,any alternative????
  }
}

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