user2730490
user2730490

Reputation: 297

How to use protractor to test class existance

I am using protractor to test my site. I countered a problem.

I have a ul, the number of li inside is dynamic,

<ul>
  <li class='listing-item'>
    <div class='prod-price'>$99</div>
  </li>
  <li class='listing-item price-onsale'>
    <div class='prod-price'>$99</div>
    <div class='prod-saving'>$10</div>
  </li>
  <li class='listing-item'>
    <div class='prod-price'>$50</div>
  </li>
  ...
</ul>

The 'prod-saving' div will only show up when 'price-onsale' class is present. I want to use protractor to test this logic, is there a way to do it? something like:

expect(elment(by.className('price-onsale').isPresent()).toBe(true).when('price-onsale).isPresent();

Upvotes: 1

Views: 55

Answers (1)

Isaac Lyman
Isaac Lyman

Reputation: 2205

Your syntax will work almost word-for-word if you rearrange it a bit:

element(by.className('price-onsale')).isPresent().then(function(present) {
  if(present) {
    expect(element(by.className('prod-saving')).isPresent()).toBe(true);
  }
});

It's a matter of testing the pre-condition first, and then testing the main condition based on the result of the first.

Upvotes: 1

Related Questions