CzescTuSlawomr
CzescTuSlawomr

Reputation: 71

Protractor - wait for class change

How in protractor wait for element to change attribute? When i enter main page, connection button has class=red, want to wait till it change class to green

Upvotes: 1

Views: 107

Answers (1)

Sergey Pleshakov
Sergey Pleshakov

Reputation: 8948

This is a generic approach for the task you're solving

/**
*  waitUntilElementHasAttribute
*  @param    {ElementFinder}    $element                    Locator of element
*  @param    {String}           attributeName               Attribute of an element to evaluate
*  @param    {string}           [attributeString=""]        A target attribute value
*  @param    {number}           [timeout=1000]              Time in ms
*  @return   {Promise}
*/
waitUntilElementHasAttribute($element, attributeName, attributeString = '', timeout = 1000) {
  return browser.wait(
    () =>
      $element.getAttribute(attributeName).then(attributeValue => {
        if (attributeValue !== null) {
          return attributeValue.includes(attributeString);
        } else {
          return false;
        }
      }),
      timeout,
      "Wait until element '" + $element.locator() + "' HAS " + attributeName + ': ' + attributeString
  );
},

Note this wait until the specified attribute partially matches the string

To use just call it like this

await waitUntilElementHasAttribute(
  $element,
  'class',
  'green',
  3000
)

Upvotes: 1

Related Questions