Java 如何检查 webtable 中的 webelement 是否使用 selenium webdriver 按字母顺序排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36950061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:52:00  来源:igfitidea点击:

How to check webelements in webtable is sorted alphabetically using selenium webdriver?

javaseleniumselenium-webdriver

提问by sreenath

Our application contains table of web elements. My aim is to check whether the contents table is alphabetically ordered.

我们的应用程序包含网络元素表。我的目的是检查目录表是否按字母顺序排列。

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

My aim is to store string type webelement in one list and create another list which is of sorted kind in the end compare two list. Please help.

我的目标是将字符串类型的 webelement 存储在一个列表中,并创建另一个排序类型的列表,最后比较两个列表。请帮忙。

采纳答案by Prateek

You can store the default sorting in a string array like obtainedListand then sort it using collections for sortedListand lastly both.

您可以将默认排序存储在类似的字符串数组中obtainedList,然后使用集合对其进行排序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));

For descending order add:

对于降序添加:

Collections.reverse(sortedList);

after Collections.sort(sortedList);

Collections.sort(sortedList);

回答by Sandeep

Collections.sort(SortedList,String.CASE_INSENSITIVE_ORDER);  

Just improvising we need to add String.CASE_INSENSITIVE_ORDERto match case sensitive sorts!

只是即兴我们需要添加String.CASE_INSENSITIVE_ORDER以匹配区分大小写的排序!

回答by rohit

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;
}