Selenium WebDriver list items check

WebElement select = myD.findElement(By.xpath("//*[@id='custfoodtable']/tbody/tr[2]/td/div/select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
    System.out.println(String.format("Value is: %s", option.getAttribute("value")));
    option.click();
    Object vaLue = "Gram";
    if (option.getAttribute("value").equals(vaLue)) {
        System.out.println("Pass");
    } else {
        System.out.println("fail");
    }
}

      

I can check one item in the list, but there are 20 items in the dropdown that I need to check and I don't want to use the above logic 20 times. Is there an easier way to do this?

+2


source to share


2 answers


Don't use the for-each construct. This is only useful when iterating over a single array Iterable

/. You need to iterate over List<WebElement>

and over the array at the same time.

// assert that the number of found <option> elements matches the expectations
assertEquals(exp.length, allOptions.size());
// assert that the value of every <option> element equals the expected value
for (int i = 0; i < exp.length; i++) {
    assertEquals(exp[i], allOptions.get(i).getAttribute("value"));
}

      


EDIT after OP modified his question a bit:

Assuming you have an array of expected values, you can do this:



String[] expected = {"GRAM", "OUNCE", "POUND", "MILLIMETER", "TSP", "TBSP", "FLUID_OUNCE"};
List<WebElement> allOptions = select.findElements(By.tagName("option"));

// make sure you found the right number of elements
if (expected.length != allOptions.size()) {
    System.out.println("fail, wrong number of elements found");
}
// make sure that the value of every <option> element equals the expected value
for (int i = 0; i < expected.length; i++) {
    String optionValue = allOptions.get(i).getAttribute("value");
    if (optionValue.equals(expected[i])) {
        System.out.println("passed on: " + optionValue);
    } else {
        System.out.println("failed on: " + optionValue);
    }
}

      

This code essentially does what my first code did. The only real difference is that you are now doing the job manually and printing the results.

Before that, I used a static method assertEquals()

from Assert

the JUnit framework class . This framework is the de facto standard when writing Java tests, and the method family assertEquals()

is the standard way to check the results of your program. They make sure that the arguments passed to the method are equal, and if not, they throw AssertionError

.

Anyway, you can do it manually too, no problem.

+4


source


You can do it like this:



String[] act = new String[allOptions.length];
int i = 0;
for (WebElement option : allOptions) {
    act[i++] = option.getValue();
}

List<String> expected = Arrays.asList(exp);
List<String> actual = Arrays.asList(act);

Assert.assertNotNull(expected);
Assert.assertNotNull(actual);
Assert.assertTrue(expected.containsAll(actual));
Assert.assertTrue(expected.size() == actual.size());

      

+1


source







All Articles