Hari Charan
Katalon Apprentice
05/09/2017

How can I create a Java Class in Katalon?

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

Upvote
Quote

Comments

  • Deane
    Katalon Apprentice
    05/09/2017
    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'()
    Upvote
    Quote
  • Hari Charan
    Katalon Apprentice
    05/09/2017
    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?
    Upvote
    Quote
  • Marek Melocik
    Katalon Ambassador
    05/09/2017
    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)
    Upvote
    Quote
Sign In or Register to comment.