CS agents will respond to tickets via forum instead of private helpdesk to support other users with similar issues.

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!

Comments

  • edited September 25

    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 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.

  • edited November 28

    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.

Sign In or Register to comment.