0

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
sreenath
  • 21
  • 2
  • 3
  • 6

3 Answers3

11

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

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

Prateek
  • 1,145
  • 1
  • 8
  • 21
  • please refer to http://stackoverflow.com/questions/40944436/can-ruby-codes-be-converted-to-java-for-webdriver-use –  Dec 03 '16 at 05:25
  • How about in Selenium with Python, could you support me for this, please. Thank you – Jin Mar 05 '21 at 08:48
  • For descending order you have to use Collections.sort(obtainedList,Collections.reverseOrder()); – Sobhit Sharma Aug 07 '21 at 13:49
1
Collections.sort(SortedList,String.CASE_INSENSITIVE_ORDER);  

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

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Sandeep
  • 11
  • 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;
}
A.H.
  • 63,967
  • 15
  • 92
  • 126
rohit
  • 1