簡體   English   中英

通過 Selenium WebDriver 驗證列表元素

[英]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"));
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");
    }
}

我可以驗證列表中的一個元素,但下拉列表中有大約 20 個元素需要驗證,我不想使用上述邏輯 20 次。 有沒有更簡單的方法來做到這一點?

不要使用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"));
}

OP改變了他的問題后進行編輯:

假設您有一組期望值,則可以執行以下操作:

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

無論如何,您也可以手動進行操作,沒問題。

您可以這樣做:

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());

public void verifyElementsInListEquals(List elementsList, String expectedValue) {

    ArrayList<String> TextList =new ArrayList<>(); // new list to have Text from list of Webelement

    for( WebElement x :elementsList){  //for each loop in JAVA
        TextList.add(x.getText());  //add "x" times text to list above
    }

    if(TextList.contains(expectedValue)){  //check if value exist in list above
        System.out.println("Value is there");  
    }
    else{
        System.out.println(" no value");
    assertTrue(false); //will always stop the program here 
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM