Sorting:
In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order and lexicographical order or alphabetical order.
In Selenium WebDriver it is important to verify the Sort Functionality of the Dynamic HTML Content
is working as expected.
Steps to Perform:
1. Retrieve the List from HTML Table.
2. Store the List in an Array
3. Sorting the items in the Array using Swapping.
Swapping is the process of Exchanging the Values.
4. Click on Sort button in the WebPage.
5. Retrieve the List again.
6. Compare the Sorted Array generated in Step 3 with the List generated in Step 5.
Selenium WebDriver Logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package seleniumiq; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class SortingTable { public static void main(String[] args) { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.get("https://www.w3schools.com/howto/howto_js_sort_table.asp"); /** * Click on Sort button and fetch the List from the html table col1 and store in an Array */ driver.findElement(By.xpath("//button[contains(text(),'Sort')]")).click(); List<WebElement> tdList = driver.findElements(By.xpath("//table[@id='myTable']/tbody/tr/td[1]")); String strArry [] = new String[tdList.size()]; for(int i=0;i < tdList.size();i++) { strArry[i]=tdList.get(i).getText(); } /* * Compare the String */ boolean sortFunctionality = true; outer: for(int i=0;i < strArry.length;i++) { for(int j=i+1;j < i;j++) { int result = strArry[j].compareTo(strArry[i]); if(!(result > 0)) { System.out.println("Data in the Table is not SORTED::" +strArry[j]+":::"+ strArry[i]); sortFunctionality=false; break outer; } else { System.out.println("Data in the Table is SORTED::" +strArry[j]+":::"+ strArry[i]); } } } if(sortFunctionality) { System.out.println("SORT Functionality is working"); } else { System.out.println("SORT Functionality is not working"); } } } |
How do I sort if it is a multipage table with pagination?
thanks for sharing the sorting trick but I think there is something missing here
for(int j=i+1;j < i;j++)
{
}
.consider the loop if : j=1,i=0,1<0 -this will neve get executed, It should be instead
and first loop I loop should start with 1
// outer loop
for(int i=1;i < strarr.length;i++)
{
//inner loop
for(int j=i-1;j < i;j++)
{
strar[i]compare(strarr[j]);
///comparison logic
}
}
hi suparna did you find the answer? If yes can you please share it to me.