Adrian
Adrian

Reputation: 23

Expecting an element to have a class attribute

I am trying to make some automation tests using rspec and selenium. How can I verify if a class from has an attribute active? I have attached the element code below:

<li class="nav-item pl10 pr10 active" data-nav="dashboard" 
  id="dashboard_navbar_item">
  <a class="nav-link" href="/dashboard">
     Dashboard
  </a>
</li>

I want to verify to have the class name (containing the active attribute, as if the attribute were set to activate the page that is displayed, I want to use it as another measure besides the URL to verify.

Upvotes: 1

Views: 239

Answers (1)

Foo Bar Zoo
Foo Bar Zoo

Reputation: 216

First you have to find the element and then check whether the classes of the element includes active or not, like below;

anchor  = @driver.find_element(:xpath, '//li[@id="dashboard_navbar_item"]/a')
classes = anchor.attribute('class')

expect(classes).to include('active')

Upvotes: 1

Related Questions