0

In my automation code,I have two javascript file having different class and some getters. I get some input from user and depending on that input, i create the name of getter during runtime and access the getter of both classes.

I am able to access the getter of same class but i am facing issue while accessing the getter of different class.

const AutomationClass2 = require('./AutomationClass2.js')
class AutomationClass1 extends Page {
  get GeneralTile() {
    return browser.isAndroid ? $(~abc) :$(~abc)
  }

  navigateLandingPage(page, tab) { 
    if (tab == "tab") {
       //page=zones, tab=tab
      var lObj = page+ "Tab"          //facing issue while accessing the getter of "AutomationClass2"
      AutomationClass2.lObj.waitForExist(20000)     //facing issue
      AutomationClass2.lObj.click()                 //facing issue
    }
    else if (tab == "zone") {
      //page=General
      var lObj = page+ "Tile"         //GeneralTile
      this[lObj].waitForExist(20000)  //working fine
      this[lObj].click()              //working fine
    }
  }
}

AutomationClass2 looks like

class SettingGeneral extends Page {
  /**
   * define elements
   */


  get zonesTab() {
    console.log("in zones tab getter ")
    const elem = browser.isAndroid ? $('~TabZones') : $('~TabZones')
    return elem
  }
  }

In first if i.e "if (tab == "tab") {" I am not able to access the getter "zonesTab" of class "AutomationClass2" Thanks in Advance,It will be great help

  • You need to put it in square brackets on the second class too: Instead of `AutomationClass2.lObj.waitForExist(20000)` change it to `AutomationClass2[lObj].waitForExist(20000)` – some Sep 16 '19 at 08:43

3 Answers3

1

if (tab == "tab") {
       //page=zones, tab=tab
      var lObj = page+ "Tab"          //facing issue while accessing the getter of        "AutomationClass2"
      AutomationClass2[lObj].waitForExist(20000)     //facing issue
      AutomationClass2[lObj].click()                 //facing issue
    }
Mischa
  • 1,591
  • 9
  • 14
1

You can access properties in Javascript using square brackets. For instance using

AutomationClass2[lObj]

allows dynamic access to the propery.

hnzlmnn
  • 393
  • 2
  • 14
1

There are two ways to access a property of an object, either by name or by variable:

AutomationClass2.lObj.waitForExist(20000); // trying to access a property named "lObj"
AutomationClass2[lObj].waitForExist(20000); // tyring to accses a property named by the lObj variable.
some
  • 48,070
  • 14
  • 77
  • 93