Liviu P
Liviu P

Reputation: 93

Check the presence of an element's attribute - protractor

I am fairly new to protractor and angularJS so please help me with this. When the dayReport element is selected, the class='active' attribute is set but when the element is not selected, the attribute is completely removed.

I want to make sure that when another element is selected, dayReport doesn't have the class='active' attribute although the element itself is still present.

I tried this: expect(dayReport.getAttribute('class').isPresent()).toBe(false); and some other few ideas but it I don't seem to make it work. Any ideas?

Upvotes: 2

Views: 1150

Answers (2)

Liviu P
Liviu P

Reputation: 93

For whoever is reading this and needs a clear answer, the response from @TomNijs was very helpful but the hasClass function should be created in order for that to work. So the whole piece of code after defining the dayReport element is now looking like this:

  var hasClass = function (element, cls)
  {
    return element.getAttribute('class').then(function (classes)
    {
      return classes.split(' ').indexOf(cls) !== -1;
    });
  };

  expect(hasClass(dayReport, 'active')).toBe(false);

Upvotes: 2

Tom Nijs
Tom Nijs

Reputation: 3962

I had the same issue, try the following code : hasClass will return a promise which will immediatly be resolved by the expectstatement and return true or false wether your element has the class="active" attribute.

//hasClass will check if the first parameter (the element) has the following the second parameter(the class attribute)
expect(hasClass(dayReport, 'active')).toBe(true);

Upvotes: 1

Related Questions