How can I create a Java Class in Katalon?


#1

I have a scenario where I need to pass Random Test Data in the text fields
To achieve this in Java (not in Katalon), I’ve created Random Text Generator class with few methods like getRandomName, getRandomCity, getRandomPhoneNumber.

How can I achieve the same in Katalon, I want to pass this random text instead of using Test Data from internal/external files


#2

You don’t need .java file. Just call a custom keyword (see previous post), save it into a variable and pass the variable into setText() method.

def randomString = CustomKeywords.'seleniumKeyword.exampleKeyword.getSaltString'()

WebUI.setText(findTestObject(/your/text/field/object), randomString)


#3

I have created a keyword too, but code being displayed in Groovy. I have converted script to .java extension. But I still see keyword with .groovy extension. Where’s is the .java file?
How can I pass ‘getSaltString’ in text field?


#4

Hi

I created a new keyword in Katalon and add all my java code in there. In your test you would call the newly created keyword.

Code in Keyword
@Keyword
public String getSaltString()
{
String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder salt = new StringBuilder();
Random rnd = new Random();
while (salt.length() < 18) { // length of the random string.
int index = (int) (rnd.nextFloat() * SALTCHARS.length());
salt.append(SALTCHARS.charAt(index));
}
String saltStr = salt.toString();
return saltStr;
}
Code in test/script
def randomString = CustomKeywords.'seleniumKeyword.exampleKeyword.getSaltString'()


#5

That’s a method call, not a class definition.