Random String generator for a text field?

Yes, it can be done with external JAR and written code, but i have no experience in writing code (i did not write for a few months now, and maybe someone has a solution ready)

Thanks!

Hi Francisc,

you can do it using standard Java libraries. Try this method:

String chars = "abcdefghijklmnopqrstuvwxyz0123456789"

public static String randomString(String chars, int length) {
  Random rand = new Random();
  StringBuilder sb = new StringBuilder();
  for (int i=0; i<length; i++) {
	sb.append(chars.charAt(rand.nextInt(chars.length())));
  }
  return sb.toString();
}

println randomString(chars, 10)

You can edit chars variable to set what characters should be in your final random string.

4 Likes

You can use third party libraries like Apache Commons Lang 3.5 for this purpose. It has many member functions to generate random strings like randomalphabetic(), randomalphanumeric(), randomnumeric(), etc. Also, you can use standard Java libraries for this purpose too.

Hi Marek Melocik:
How can I use your code in katalon? when I build a new keyword, copy your code, it always said error, and I cannot add the keyword in my cases.

@Marek Melocik said:
Hi Francisc,

you can do it using standard Java libraries. Try this method:

String chars = "abcdefghijklmnopqrstuvwxyz0123456789"

public static String randomString(String chars, int length) {
  Random rand = new Random();
  StringBuilder sb = new StringBuilder();
  for (int i=0; i<length; i++) {
	sb.append(chars.charAt(rand.nextInt(chars.length())));
  }
  return sb.toString();
}

println randomString(chars, 10)

You can edit chars variable to set what characters should be in your final random string.

2 Likes

freeman said:

Hi Marek Melocik:

How can I use your code in katalon? when I build a new keyword, copy your code, it always said error, and I cannot add the keyword in my cases.

@Marek Melocik said:

Hi Francisc,

you can do it using standard Java libraries. Try this method:

String chars = "abcdefghijklmnopqrstuvwxyz0123456789"public static String randomString(String chars, int length) {  Random rand = new Random();  StringBuilder sb = new StringBuilder();  for (int i=0; i<length; i++) {  sb.append(chars.charAt(rand.nextInt(chars.length())));  }  return sb.toString();}println randomString(chars, 10)

You can edit chars variable to set what characters should be in your final random string.

you can create this in custom keyword :

@Keyword

def randomString(int length) {

String chars = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”

Random rand = new Random();

StringBuilder sb = new StringBuilder();

for (int i=0; i<length; i++) {

sb.append(chars.charAt(rand.nextInt(chars.length()))); }

return sb.toString(); }

4 Likes

Hi
Can anyone tell me step by step process to implement this code in my test scripts?

Hi, this is a similar thread that I started a while ago, you will find more useful information here:

I could successfully do with the below code. Created a Keyword so that i can call anywhere.
auto_eng 071018-1101 is the String generated with the below code

@Keyword

def randomEngagementNameGenerator() {

Date today = new Date()

String todaysDate = today.format(‘MMddyy-hhmm’)

String engagementName = 'auto_eng ’ + todaysDate

WebUI.comment(engagementName)

}

Console Log

07-10-2018 11:01:45 AM - [START] - Start action : da.common.methods.CommonActionsInDA.randomNumber

07-10-2018 11:01:45 AM - [INFO] - auto_eng 071018-1101

07-10-2018 11:01:45 AM - [PASSED] - da.common.methods.CommonActionsInDA.randomNumber is PASSED

2 Likes

for me it’s not work :frowning:

[quote=“Marek_Melocik, post:2, topic:6607”]
you can do it using standard Java libraries.
[/quote] Hi I can use standard Java libraries in katalon and how you make this code works ? Thanks.

Hi guys, only need to add this script in your test case, it will work fine, in this script I generated a string includes number and characters with length = 10:

String s1 = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”
Random rand = new Random()
StringBuilder sb = new StringBuilder()
for (int j=1;j<=10;j++)
{
sb.append(s1.charAt(rand.nextInt(s1.length())))
}
WebUI.comment(sb.toString())

Hi, @Marek_Melocik how can I use the random text value in other test cases as input value? Thanks

Personally, there are all sorts of pseudo strings that you can find on various testing sites that don’t make any sense that you can “hardcode”, rather than run the random string maker routine, like below (I added the special characters as extra to see what happens for them as well):

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed aliquam #866&!% sapien a risus dapibus.

However, if you want to see the random string maker in action, perhaps create a Keyword with a parameter of the length of String you want and then return the built up String, like:

@Keyword
public String randomStringMaker(int strLength) {
    String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    Random rand = new Random()
    StringBuilder sb = new StringBuilder()
    for (int j = 1; j <= strLength; j++)
    {
        sb.append(s1.charAt(rand.nextInt(s1.length())))
    }
    return sb
}

I am not overly hyped on calling the Random function repeatedly instead of only once in your program, so maybe you can pass that in as well. I will let you do that.

Anyways, in your TC you would (you will have to replace “com” and “name” with your KeyWords location):

String str = CustomKeywords.'com.name.randomStringMaker'(7)
WebUI.setText(findTestObject('your test object'), str)