我正在尝试自动化双列表框测试。我想比较选定的值(左侧)与移动的值(进入右侧列表)。这是代码。
Select allFromListData = new Select(listData);
// values selection
allFromListData.selectByIndex(0);
allFromListData.selectByVisibleText("Helena");
List<WebElement> selectedList=allFromListData.getAllSelectedOptions();
//clicking on add button
for(int i=0;i<selectedList.size();i++)
System.out.println(selectedList.get(i).getText());
WebElement addButton = driver.findElement(By.xpath("//*[@id='pickList']/div/div[2]/button[1]"));
addButton.click();
//Verification of selected content...
WebElement toListData=driver.findElement(By.xpath("//*[@id='pickList']/div/div[3]/select"));
Select allToListData = new Select(toListData);
List<WebElement> movedData=allToListData.getOptions();
问题是如何比较List<WebElement> selectedList=allFromListData.getAllSelectedOptions();
之间的数据和List<WebElement> movedData=allToListData.getOptions();
最佳答案
我假设,您想要比较所选项目字符串的列表而不是 Web 元素列表,因为 getOptions() 方法将返回 Web 元素列表。逻辑很简单,首先从移动前后的列表中取出值的列表。然后对两个值列表进行排序并比较/断言是否相等。
Select allFromListData = new Select(listData);
// values selection
allFromListData.selectByIndex(0);
allFromListData.selectByVisibleText("Helena");
List<WebElement> selectedList=allFromListData.getAllSelectedOptions();
//add selected Items to list
List<String> lstSelectedItem=new ArrayList<String>();
for(int i=0;i<selectedList.size();i++){
System.out.println(selectedList.get(i).getText());
lstSelectedItem.add(selectedList.get(i).getText());
}
//clicking on add button
WebElement addButton = driver.findElement(By.xpath("//*[@id='pickList']/div/div[2]/button[1]"));
addButton.click();
//Verification of selected content...
WebElement toListData=driver.findElement(By.xpath("//*[@id='pickList']/div/div[3]/select"));
Select allToListData = new Select(toListData);
List<WebElement> movedData=allToListData.getOptions();
//add moved Items to list
List<String> lstMovedItem=new ArrayList<String>();
for(int i=0;i<movedData.size();i++){
System.out.println(movedData.get(i).getText());
lstMovedItem.add(movedData.get(i).getText());
}
//sort the items
Collections.sort(lstSelectedItem);
Collections.sort(lstMovedItem);
//verify the lists are equal
Assert.assertEquals(lstSelectedItem, lstMovedItem);
关于java - 如何与 webdriver 中的 List<WebElement> 进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44945522/