How to verify if a list of words is sorted?


I have a list of words and I want to test if the sort function is working correctly

Each word is inside a h4 as shown below





After sorted:





How I can test in Katalon if the sorting function is working correctly?

I was trying to extract the text with getAttribute(‘innerText’) to convert the data into a list but not sure if this is the correct approach and not sure how to compare two words

Any suggestion?

You question lacks information for others to work on. Please show the source of target HTML and your test code. Please explain what you have done; what you expected to see and what you actually saw. Please show the execution log if informative.

What you can try is to create two lists and then store your words into both and then sort one of them and then compare both lists. Maybe something like below:

"are words in alphabetical order"
List<String> expectedOptions = new ArrayList<String>()
List<String> actualOptions = new ArrayList<String>()

// collect your words
CustomKeywords.'com.Tools.getWords'(expectedOptions, actualOptions)

// now sort one of the lists
try {
} catch (NullPointerException)
	println ("Array list had a null element!");

// we sort one list and then compare both
WebUI.verifyEqual(actualOptions, expectedOptions, FailureHandling.OPTIONAL)
for (int irow = 0; irow < actualOptions.size(); irow++)
	expectedText = expectedOptions.get(irow).toString();
	actualText = actualOptions.get(irow).toString();

	WebUI.verifyMatch(actualText, expectedText, false, FailureHandling.OPTIONAL)

Edit: you might want to compare both lists without capital and small letters and just put your words to one or the other, like:

	expectedText = expectedOptions.get(irow).toString().toLowerCase();
	actualText = actualOptions.get(irow).toString().toLowerCase();

Edit2: or another way might be:

// collect your words
expectedOptions = CustomKeywords.'com.Tools.getWords'()
actualOptions = CustomKeywords.'com.Tools.getWords'()

With apologies to Linus Torvolds, I wouldn’t even start there

First, answer these simple questions…

What method does the web site and its developers use to effect the sort?

If the data arrives having first been sorted by a database or…

If the developers used a core JavaScript function or…

If it uses any other kind of standard/3rd party sorting/ordering routine…

Don’t waste your time. You cannot test formal, standard sorting/ordering routines any better than the standards bodies or manufacturers have already done. Unless you work for one of those setups, this is simply not your job. And if your boss/superiors say otherwise, they need to explain why they consider it productive to waste your time.

However, if the devs coded their own sorting algorithm from core primitives, get the specification they’re working to, construct your own copy of how the list should be ordered based on that same spec, and compare the incoming list to that.

I made a HTML in the project’s root folder with name “page.html”:

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8">



I made a Test Case:

import org.openqa.selenium.WebElement

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

TestObject makeTestObject(String id, String selector) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty("css", ConditionType.EQUALS, selector)
	return tObj

File html = new File("./page.html")
// we collect the h4 elements out of the page
List<WebElement> h4s = WebUI.findWebElements(makeTestObject("h4s", "h4"), 10)
WebUI.comment("In the page found:")
for (WebElement webElement : h4s) { 
// we expect to see these text in this order (sorted alphabetically)
List<String> expectList = ["Apple", "Banana", "Carrot", "Melon"]
// lets compare the two lists
for (int i = 0; i < h4s.size(); i++) {
	String actual = h4s.get(i).getText()
	String expected = expectList.get(i)
	if (expected != actual) {
		KeywordUtil.markFailed("index=${i}: expected=${expected}, actual=${actual}")

I ran the test case. I saw the messages in the console:

2023-09-05 10:11:13.034 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-09-05 10:11:13.038 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/dario
Test Cases/dario
2023-09-05 10:11:17.404 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - In the page found:
2023-09-05 10:11:17.448 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - Carrot
2023-09-05 10:11:17.485 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - Apple
2023-09-05 10:11:17.559 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - Banana
2023-09-05 10:11:17.613 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - Melon
2023-09-05 10:11:17.750 ERROR com.kms.katalon.core.util.KeywordUtil    - ❌ index=0: expected=Apple, actual=Carrot
2023-09-05 10:11:17.780 ERROR com.kms.katalon.core.util.KeywordUtil    - ❌ index=1: expected=Banana, actual=Apple
2023-09-05 10:11:17.798 ERROR com.kms.katalon.core.util.KeywordUtil    - ❌ index=2: expected=Carrot, actual=Banana
2023-09-05 10:11:17.960 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/dario FAILED.
com.kms.katalon.core.exception.StepFailedException: index=0: expected=Apple, actual=Carrot
	at com.kms.katalon.core.util.KeywordUtil.markFailed(
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(
	at com.kms.katalon.core.main.TestCaseExecutor.execute(
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$ Source)

Test Cases/dario
2023-09-05 10:11:17.984 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/dario

This sample code shows how to

  1. get a list of WebElements each of which corresponds to a <h4> element in the target HTML
  2. declare a list of Strings named expectList that you expect to find in the page
  3. verify if the 2 lists have the same string values

I guess @dario just wants to know how to do these 3 steps. I guess @dario is not concerned about the sorting algorithms.

+1 from me.