Automation | Webdriverio | What is the use of using get before element name?

Viewed 127

In my framework, I have a page xyz whose element properties are defined in xyz.page.js

    get logInPromptWindow() {
      return browser.element("//div[contains(@class,'abcdef')]");
    }

and

    logInPromptWindow() {
      return browser.element("//div[contains(@class,'abcdef')]");
    }
  1. Is there any purpose for using "get"?
  2. Will both these elements considered as different elements?
1 Answers

get is the getter keyword in javascript . It allows you to bing a variable to a property.

With getter:

    const obj = {
      log: ['a', 'b', 'c'],
      get latest() {
        if (this.log.length === 0) {
          return undefined;
        }
        return this.log[this.log.length - 1];
      }
    };
    
    console.log(obj.latest);
    // expected output: "c"
 
    console.log(obj);
    //prints the final object state

With out getter:

    const obj = {
      log: ['a', 'b', 'c'],
      latest() {
        if (this.log.length === 0) {
          return undefined;
        }
        return this.log[this.log.length - 1];
      }
    };

    console.log(obj.latest());
    // expected output: "c"

    console.log(obj);
    //prints the final object state

in with getter, the function is resolved into a property with the specified value. While without getter it will be resolved to be a property with function as the value you have to call the function to trigger it

or in simple words, it acts as a self invoked function

Related