我们的应用程序包含Web元素表。我的目的是检查内容表是否按字母顺序排列。
WebDriver d=new FirefoxDriver();
d.get("http://www.javatpoint.com/wrapper-class-in-java");
WebElement table=d.findElement(By.xpath(".//*[@id='city']/table/tbody"));
List<WebElement> lst=table.findElements(By.tagName("tr"));
System.out.println(lst);
List ls=new ArrayList<String>();
for(int i=0;i<=6;i++)
{
ls=lst.addAll(get(i).getText())
}
我的目标是将字符串类型webelement存储在一个列表中,并创建另一个已排序的列表 最后比较两个清单。请帮忙。
答案 0 :(得分:10)
您可以将默认排序存储在sortedList
之类的字符串数组中,然后使用ArrayList<String> obtainedList = new ArrayList<>();
List<WebElement> elementList= driver.findElements(By.xpath(YourLocator));
for(webElement we:elementList){
obtainedList.add(we.getText);
}
ArrayList<String> sortedList = new ArrayList<>();
for(String s:obtainedList){
sortedList.add(s);
}
Collections.sort(sortedList);
Assert.assertTrue(sortedList.equals(obtainedList));
的集合对其进行排序,最后两者都进行排序。
Collections.reverse(sortedList);
对于降序添加:
Collections.sort(sortedList);
在{{1}} 之后
答案 1 :(得分:1)
Collections.sort(SortedList,String.CASE_INSENSITIVE_ORDER);
即兴发挥作用,我们需要添加String.CASE_INSENSITIVE_ORDER
来匹配区分大小写的排序!
答案 2 :(得分:-1)
public boolean sortVerify() {
List<WebElement> theList=driver.findElements(By.xpath("//*[@id='productView']/div/div/div/div"));
List<Integer> Prices=new ArrayList<>();
for (int NumberOfItem=1; NumberOfItem<theList.size();NumberOfItem++) {
System.out.println("feteching by xpath price");
String priceOneRaw=driver.findElement(By.xpath("//*[@id=\"p_"+NumberOfItem+"_1\"]/div/div[5]/div/span[1]")).getText();
System.out.println("feteching by xpath price--------"+priceOneRaw);
////below 2 lines are to get the price in exact integers, as earlier it was in format Rs.3829.
String priceOneFinal=priceOneRaw.replaceAll("[\\-\\?\\+\\.\\^:,\\\u20B9]","");
String PriceComplete = priceOneFinal.split("Rs")[1];
int finalPrice=Integer.parseInt(PriceComplete);
Prices.add(finalPrice);
}
int previousPrice=Prices.get(0);
for(int SortCheckNumber=1;SortCheckNumber<theList.size()-1;SortCheckNumber++) {
if(Prices.get(SortCheckNumber)<previousPrice) {
System.out.println("notSorted");
return false;
}else {
previousPrice=Prices.get(SortCheckNumber);
}
}
System.out.println("allSorted");
return true;
}