Forum Discussion

rancohen's avatar
rancohen
Occasional Contributor
7 years ago

how to make Data generator to execute every test for random data

Hi,

 

I need to generate random data and with diffren size every time I run test.

how can I do it?

I saw post that suggest to generate once a large file and then take only part of it but I need to define diffrent parameters for the file.

is there any way to configure and generate data generator from script or any other option?

 

Best regards,

Ran

  • MoUddin's avatar
    MoUddin
    7 years ago

    To create random passwords (Strings), and numbers you can use the RandomStringUtils: Documentation here

     

    You'll then want to store them into custom properties to then be used in your test requests.

     

    Example:

    import static org.apache.commons.lang3.RandomStringUtils.*
    
    //if you have a property step you can use it to store them, otherwise you can store them at test case level
    def randomProperties = testRunner.testCase.getTestStepByName("randomProperties")
    
    def randomPassword = randomAlphanumeric(8,21) // arguments: minimum length, max length (so a string between 8 and 20 characters)
    def randomString = randomAlphanumeric(8,21)
    def randomNumber = randomNumeric(5) // random numbers of length 5
    
    randomProperties.setPropertyValue("randomPassword",randomPassword)
    randomProperties.setPropertyValue("randomString",randomString)
    randomProperties.setPropertyValue("randomNumber",randomNumber)

    you can then access these properties in your requests with

    ${randomProperties#<propertyName>}

    Hope this helps,

    Mo

  • Hi there,

     

    What kind of data would you like to generate? Random strings of characters? Random dates? Random numbers? And would you require multiples of these?

     

    Mo

    • rancohen's avatar
      rancohen
      Occasional Contributor
      Hi,

      I have test that I need to generate random passwords, other test that I need random strings and in the last test I need random numbers.

      I don't want that in my test any random option will repeat twice.

      Ran
      • MoUddin's avatar
        MoUddin
        Contributor

        To create random passwords (Strings), and numbers you can use the RandomStringUtils: Documentation here

         

        You'll then want to store them into custom properties to then be used in your test requests.

         

        Example:

        import static org.apache.commons.lang3.RandomStringUtils.*
        
        //if you have a property step you can use it to store them, otherwise you can store them at test case level
        def randomProperties = testRunner.testCase.getTestStepByName("randomProperties")
        
        def randomPassword = randomAlphanumeric(8,21) // arguments: minimum length, max length (so a string between 8 and 20 characters)
        def randomString = randomAlphanumeric(8,21)
        def randomNumber = randomNumeric(5) // random numbers of length 5
        
        randomProperties.setPropertyValue("randomPassword",randomPassword)
        randomProperties.setPropertyValue("randomString",randomString)
        randomProperties.setPropertyValue("randomNumber",randomNumber)

        you can then access these properties in your requests with

        ${randomProperties#<propertyName>}

        Hope this helps,

        Mo