Forum Discussion

tientran's avatar
tientran
Occasional Contributor
8 years ago

Hi Guys

How can I generate a string then I can use it for all steps in a test case? Please help me.

  • nmrao's avatar
    nmrao
    Champion Level 3

    You can generate a random string in Setup Script of test case, save it as test case level custom property  and use it all the steps of test case using Property Expansion.

     

    See below link to

     

    //Courtesy: http://stackoverflow.com/questions/8138164/groovy-generate-random-string-from-given-character-set
    
    def generator = { String alphabet, int n ->
      new Random().with {
        (1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
      }
    }
    
    //Provide the length of the string, here it generates 9 characters
    def id = generator( (('A'..'Z')+('0'..'9')).join(), 9 )
    
    //Save it into custom property
    testCase.setPropertyValue('ID', id)
    
    

    How to use above generated id in steps?

    • If the step is of Groovy Script test step, then use below to get the id - context.expand('${#TestCase#ID}')
    • If the step of Request type step, then use below - <elementId>${#TestCase#ID}</elementId>
    • tientran's avatar
      tientran
      Occasional Contributor

      I don't understand clearly at: 

      - Save it as test case level custom property

      //Save it into custom property
      testCase.setPropertyValue('ID', id)

      If you have any screenshot, please show me or any example. Thank you very much!

  • nmrao's avatar
    nmrao
    Champion Level 3

    By the way, request you to properly provide the summary of the problem while creating the topic in the forums instead of using unsuitable one like you wrote "Hi Guys".