Is there anyway to test sorting functionality of a web table?

Is it possible to test the sorting functionality of a web table in Katalon Studio?
Does Katalon Studio have any default method to verify whether the datas within a single column is either in ascending or descending order?

The following is the code that I used to fetch all the values listed in the 1st column of a web table and save them in an array:

WebDriver driver = DriverFactory.getWebDriver()

‘To locate table’

WebElement Table = driver.findElement(By.xpath(’/html[1]/body[1]/table[1]/tbody[1]’))

‘To locate rows of table it will Capture all the rows available in the table’

List rows_table = Table.findElements(By.tagName(‘tr’))

‘To calculate no of rows In table’

int rows_count = rows_table.size()

String[] celltext = new String[rows_count]

for (int row = 0; row < rows_count; row++) {

‘To locate columns(cells) of that specific row’

List Columns_row = rows_table.get(row).findElements(By.tagName(‘td’))

‘It will retrieve text from 1st cell’

String celltext_1 = Columns_row.get(0).getText()

celltext[row] = celltext_1

}

For example, celltext = [4,3,2,1]
Now I want to verify that the values saved in celltext is in descending order.

Any help will be highly appreciated.

I wouldn’t be surprised if there is a Java routine out there you could convert into a Custom Keyword. I’ve done similar validation just using IF statements.
I read the two values, then did the standard IF A<B, IF B<C and it worked fine.
It’s not ideal, but could get you by until you get a custom sort function written.

Would you try my implementation?

… It’s shame. All indentation characters have gone when pasted.

Test Case:

WebUI.openBrowser('')
WebUI.navigateToUrl('http://demoaut-mimic.kazurayam.com/6597_testbed.html')
WebDriver driver = DriverFactory.getWebDriver()
'To locate table'
/*
 <table id="customers">
 <tbody>
 <tr>
   <th>Company</th>
   <th>Contact</th>
   <th>Country</th>
 </tr>
 <tr>
   <td>Alfreds Futterkiste</td>
   <td>Maria Anders</td>
   <td>Germany</td>
 </tr>
 <tr>
   <td>Centro comercial Moctezuma</td>
   <td>Francisco Chang</td>
   <td>Mexico</td>
 </tr>
 <tr>
   <td>Ernst Handel</td>
   <td>Roland Mendel</td>
   <td>Austria</td>
 </tr>
 <tr>
   <td>Island Trading</td>
   <td>Helen Bennett</td>
   <td>UK</td>
 </tr>
 <tr>
   <td>Laughing Bacchus Winecellars</td>
   <td>Yoshi Tannamuri</td>
   <td>Canada</td>
 </tr>
 <tr>
   <td>Magazzini Alimentari Riuniti</td>
   <td>Giovanni Rovelli</td>
   <td>Italy</td>
 </tr>
 </tbody>
 </table>
 */
WebElement table = driver.findElement(By.xpath('//table[@id="customers"]'))
WebUI.comment("${table.getAttribute('outerHTML')}")
'To select a set of <td> element of each rows'
List<WebElement> original = table.findElements(By.xpath('./tbody/tr/td[1]'))
original.each {
WebUI.comment("original: ${it.getAttribute('outerHTML')}")
}
'verify if the original is NATURAL ordered --> should PASS'
CustomKeywords.'mypackage.WebElementListVerifier.verifySortedNatural'(original)
'verify if the original is REVERSE ordered --> should FAIL'
CustomKeywords.'mypackage.WebElementListVerifier.verifySortedReverse'(original)
WebUI.closeBrowser()

Custom Keyword:

package mypackage
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import internal.GlobalVariable
import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.WebElement
import org.openqa.selenium.WebDriver
import org.openqa.selenium.By
import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObjectProperty
import com.kms.katalon.core.mobile.helper.MobileElementCommonHelper
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.webui.exception.WebElementNotFoundException
class WebElementListVerifier {
@Keyword
def boolean verifySortedNatural(List<WebElement> list) {
List<WebElement> natural =
list.sort(false, {left, right -> left.getText() <=> right.getText()})
//natural.each {
//	WebUI.comment("natural: ${it.getAttribute('outerHTML')}")
//}
def left, right
try {
for (int i = 0; i < list.size(); i++) {
left  = list.get(i).getText()
right = natural.get(i).getText()
//WebUI.comment("left=${left},right=${right}")
assert left == right
}
} catch (AssertionError e) {
StringBuilder sb = new StringBuilder()
list.each {
sb.append("${it.getAttribute('outerHTML')}\n")
}
KeywordUtil.markFailed("Not ordered natural.\n${sb.toString()}\nactual <=> expected\n\'${left}\' <=> \'${right}\'")
return false
}
return true
}
@Keyword
def boolean verifySortedReverse(List<WebElement> list) {
List<WebElement> reverse =
list.sort(false, {left, right -> right.getText() <=> left.getText()})
//list.each {
//	WebUI.comment("list: ${it.getAttribute('outerHTML')}")
//}
//reverse.each {
//	WebUI.comment("reverse: ${it.getAttribute('outerHTML')}")
//}
def left, right
try {
for (int i = 0; i < list.size(); i++) {
left  = list.get(i).getText()
right = reverse.get(i).getText()
//WebUI.comment("left=${left},right=${right}")
assert left == right
}
} catch (AssertionError e) {
StringBuilder sb = new StringBuilder()
list.each {
sb.append("${it.getAttribute('outerHTML')}\n")
}
KeywordUtil.markFailed("Not ordered reverse.\n${sb.toString()}\nactual <=> expected\n\'${left}\' <=> \'${right}\'")
return false
}
return true
}
}

I was able to solve it with the following way:

    List<Integer> celltext_list = Arrays.asList(celltext);    Collections.sort(celltext_list, Collections.reverseOrder());    int[] celltext_new = celltext_list.toArray();    if(Arrays.equals(celltext_new, celltext)){        System.out.println("Celltext is in descending order")    }    else{        System.out.println("Celltext is in ascending order")    }However, thank you so much for your feedback. I will definitely try your way as well and get back at you as soon as possible Kazurayam.
1 Like