0

I'm new to Selenium. I like to verify if a specific class contains a text snippet in JavaScript.

I have following HTML:

<div class="product-small">
   <div class="data">
      <p>This is some Text</p>
   </div>
</div>
<div class="product-small">
   <div class="data">
      <p>This is some Text</p>
   </div>
</div>
...

The above class are available multiple times on the page, so i have to iterate the "product-small" class to check if all 12 classes contains the text "some". If one of the 12 elements with that "product-small" class does not contain the text "some", the test will fail.

I thought something like this:

const cars = driver.findElements(By.className('product-small'));
    cars.forEach(function (element) {
        // Check if element contains "some" inside class "data"
        // If element does not contain "some", test will fail
    });

Thank you :)

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
janfe
  • 1

1 Answers1

0

A generic solution would be to collect the elements using the following based Locator Strategy and probe the length, if it equals to 12 then mark it as Pass else mark it as Fail

const cars = driver.findElements(By.xpath("div[@class='product-small']//p[contains(., 'some')]"));
DebanjanB
  • 118,661
  • 30
  • 168
  • 217