Skip to content

How to check that all items in the list contains any character from string

List<WebElement> elements = driver.findElements(By.className("price"));
        for (WebElement element : elements) {
            System.out.println(element.getText());
    }


        String currency = driver.findElement(By.cssSelector("#_desktop_currency_selector > div > span.expand-more._gray-darker.hidden-sm-down")).getText();
        System.out.println(currency);

I have list that contains: 22,33 $ 44,22 $ 22,11 $ …

And a string that contains: USD $

My test needs to check that the currency type from the dropdown located in the site header matches the currency type char in all of the displayed items.

If all elements from the list contain any symbol from currency settings (for example $) – test passed, else failed

Tried to use assert that contains but still can’t get how to make it work… I would be grateful for the help

Answer

First, I’d extract the currency symbol (assumption: it’s a single character at the end of the currency string):

String currencySymbol = currency.substring(currency.length() - 2);

Then, I’d go over all the elements and check they contain it:

for (WebElement element : elements) {
    // You should probably also add a user-friendly message here
    assertTrue(element.getText().endsWith(currencySymbol));
}