for-each 구문을 사용하지 마십시오. 단일 Iterable
/배열을 반복 할 때만 유용합니다. List<WebElement>
및 배열을 동시에 반복해야합니다. 영업 이익의 후
// 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"));
}
편집 그의 질문 약간 변경 :
예상 값의 배열을 가지고 가정을, 당신은이 작업을 수행 할 수 있습니다
이
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);
}
}
이 코드는 기본적으로 무엇을합니까 내 첫 번째 코드가 그랬다. 유일한 차이점은 작업을 수동으로 수행하고 결과를 인쇄한다는 것입니다.
이전에는 JUnit 프레임 워크의 Assert
클래스의 assertEquals()
정적 메서드를 사용했습니다. 이 프레임 워크는 Java 테스트를 작성하는 데있어 사실상의 표준이며, assertEquals()
메소드 패밀리가 프로그램 결과를 검증하는 표준 방법입니다. 그들은 메소드에 전달 된 인수가 동일한지 확인하고 그렇지 않은 경우 AssertionError
을 던집니다.
어쨌든 수동 방식으로도 문제가 없습니다.
인쇄 결과는 무엇인가? –