java - Verify list elements by Selenium WebDriver -


webelement select = myd.findelement(by.xpath("//*[@id='custfoodtable']/tbody/tr[2]/td/div/select")); list<webelement> alloptions = select.findelements(by.tagname("option")); (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 verify 1 element in list, there 20 elements in dropdown need verify , not want use above logic 20 times. there easier way it?

don't use for-each construct. it's useful when iterating on single iterable / array. need iterate on list<webelement> , array simultaneously.

// assert number of found <option> elements matches expectations assertequals(exp.length, alloptions.size()); // assert value of every <option> element equals expected value (int = 0; < exp.length; i++) {     assertequals(exp[i], alloptions.get(i).getattribute("value")); } 

edit after op's changed question bit:

assuming have array of expected values, can this:

string[] expected = {"gram", "ounce", "pound", "millimeter", "tsp", "tbsp", "fluid_ounce"}; list<webelement> alloptions = select.findelements(by.tagname("option"));  // make sure found right number of elements if (expected.length != alloptions.size()) {     system.out.println("fail, wrong number of elements found"); } // make sure value of every <option> element equals expected value (int = 0; < 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 first code did. real difference you're doing work manually , printing results out.

before, used assertequals() static method assert class of junit framework. framework de-facto standard in writing java tests , assertequals() method family standard way verify results of program. make sure arguments passed method equal , if not, throw assertionerror.

anyway, can manual way, too, no problem.


Comments