JS function to execute in Katalon

Hello!

Please advise how to execute JS script in Katalon?
I need to compare first letters of the words.

function compare(wordOne, wordTwo) {
    return wordOne[0] <= wordTwo[0];
}

where
wordOne =WebUI.getText(findTestObject(‘Folder/Element’))
wordTwo is hardcoded.

The purpose is to check that records are sorted in reversed alphabetical order.
Is there other easier way?

Thank you for your answers!

Interesting. Perhaps you could write a Groovy or Java function instead. Like:

def boolean compareLetters(String wordOne, String wordTwo) {
  return wordOne.substring(0,1) <= wordTwo.substring(0,1);
}

I changed your “compare” function into “compareLetter” as I don’t like to use phrases that are possibly reserved words.

As for if there is an easier way, that is possible but try your way and then keep investigating afterwards, such as using a Sort routine for Lists that is possible.

Edit: You should try your routine idea also:
return wordOne[0] <= wordTwo[0];

Something that I use but you will have to adjust to the column in the table that has your text

Maybe like:
def boolean compareOrder() {
	"are rows in reverse alphabetical order"
	List<String> expectedOptions = new ArrayList<String>()
	List<String> actualOptions = new ArrayList<String>()
	
	orderTable = driver.findElement(By.xpath('//table/tbody'));
	
	rows_table = orderTable.findElements(By.tagName('tr'));
	
	rows_count = rows_table.size();
	
	for (int irow = 0; irow < rows_count; irow++)
	{
		List<WebElement> Columns_row = rows_table.get(irow).findElements(By.tagName('td'));
		'retrieve text from column 5 (zero based)'
		cellText = Columns_row.get(4).getText()
		println("the text is ${cellText}")
		expectedOptions.add(cellText);
		actualOptions.add(cellText);
	}
	
	try {
		// we sort one list and then compare both
		Collections.sort(expectedOptions, Collections.reverseOrder());
	} catch (NullPointerException)
	{
		println ("Array list had a null element!");
	}
	return WebUI.verifyEqual(actualOptions, expectedOptions, FailureHandling.CONTINUE_ON_FAILURE)
}

Thank you very much, it works.
I will use more sophisticated scripts when I improve my coding skills.

attribute = WebUI.getText(findTestObject('Smoke Test Objects/Element'))

boolean compareLetters(String wordOne, String wordTwo) {
	return wordOne.substring(0, 1) <= wordTwo.substring(0, 1)
}
def sortOrder = compareLetters(attribute, 'w')

str = String.valueOf(sortOrder)

WebUI.verifyMatch('true', str, true)

My variation:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

def attribute = WebUI.getText(findTestObject('Smoke Test Objects/Element'))

def char1 = attribute.substring(0,1)

if (char1.compareTo("w") < 0) {
	WebUI.comment(char1 + " is smaller than w")
	
} else if (char1.compareTo("w") == 0) {
	WebUI.comment(char1 + " is equal to w")

} else {
	WebUI.comment(char1 + " is larger than w")	
}

1 Like

The code looks and works perfect!

But a result should be compared with an expected one in Katalon.
It seems that return declaration fits better.

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

def attribute = WebUI.getText(findTestObject('Smoke Test Objects/Element'))

def char1 = attribute.substring(0,1)

if (char1.compareTo("w") != 0) {
    KeywordUtil.markFailedAndStop("attribute(${attribute}) doesn't start with 'w'")
}

Or even simpler one:

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

def attribute = WebUI.getText(findTestObject('Smoke Test Objects/Element'))

if ( ! attribute.startsWith("w")) {
    KeywordUtil.markFailedAndStop("attribute(${attribute}) doesn't start with 'w'")
}

1 Like

The WebUI.verifyMatch keyword and WebUI.verifyEqual keyword emit silly messages, such as:

FAILED 02-27-2018 04:48:39 午後 Unable to verifyequal between actuial object ‘true’ and expected object ‘false’ (Root cause: Actual object ‘true’ and expected object ‘false’ are not equal)

Do you understand what this message is meant for?

true and false are not equal

This sounds philosophical, but I don’t think this message is useful. I would never use these poorly designed built-in Keywords…

Instead, I would use KeywordUtil.markFailed(String) or my Custom Keyword :

  • Assert.assertTrue(String, Boolean)
    or
  • Assert.assertEquals(String, Objecgt, Object)

I can’t judge the functionality in such deep way. So I will use the well-grounded advice.
Thank you!

One last point if you don’t mind on the above statement. The last true indicates whether or not you want to use Regular Expression (RegEx) and true means you do. For the above statement, you have no RegEx characters and the comparison is small enough that it may not matter, however, you should know that doing a RegEx comparison as to a direct comparison can cause you concerns if you don’t know what you are doing. You should use false in the third position as a default unless you really need to use RegEx.

WebUI.verifyMatch('true', str, false)

Thank you very much! I really did not pay due attention to the 3rd parameter of WebUI.verifyMatch description.

I use WebUI.verifyMatch in other cases as well. E.g.:

topMachineName = WebUI.getText(findTestObject('Smoke Test Objects/Fleet/Top menu machine name'))

leftMenuFirstMachineName = WebUI.getText(findTestObject('Smoke Test Objects/Fleet/First machine on AllProject or Favorites Tab'))

WebUI.verifyMatch(topMachineName, leftMenuFirstMachineName, true)

@grylion54 it seems I should verify matching in another way?

I use verifyMatch as well, and it does what I want. In my case, I want to do the comparison and keep going. I don’t want to fail my test, but to instead tell me when something is not what I was expecting. Personally, start with verifyMatch and if you have concerns somewhen, then change it.

With the above, again you should not use true, but false.

WebUI.verifyMatch(topMachineName, leftMenuFirstMachineName, false)