Search code examples
seleniumselenium-webdriverprotractorangularjs-e2e

Why evaluate() in Protractor?


When I run the below snippet, I got the following output. But I'm still unclear why and when evaluate() has to used ....

    browser.get('https://weather.com/en-IN');

    $$("input[data-ng-change='goSearch()']").evaluate('placeholderText').then(function(value) {
        console.log(value);
      });

enter image description here


Solution

  • evaluate() is rarely used, but has a unique purpose - it gives you an access to the scope of the current element you are working with. This is usually needed when a value you are looking for is not exposed in the HTML as an attribute or element's text.

    For example, when you have a repeater over an array of objects and you need to access some object property that is not in the HTML:

    element.all(by.repeater("address in addresses")).filter(function (elm) {
         return elm.evaluate("address.zipCode").then(function (zipCode) {
              return zipCode === "10801";
         });
    });