Forum Discussion

Lifeguard's avatar
Lifeguard
Occasional Contributor
13 years ago

Managing variables in a http request

Hello.


I'm dealing with a particular rest web service who works directly with http request and json response. I would simply like to know, a way to mange variables so I can send (in the request) random user data ( for example ) in different test cycles. For example :


Send http://services.test/user_name/action?=1


The thing I would like to do is to generate a random "user_name" and send it each test cycle (each time I execute the test-suite). Is it possible? I've searched the following :

viewtopic.php?f=1&t=13850&p=33663&hilit=variable+rest+http#p33663
viewtopic.php?f=5&t=12821&p=31234&hilit=variable+rest+http#p31234
viewtopic.php?f=2&t=1186&p=4402&hilit=variable+rest+http#p4402

But they didn't give me a clear answer.

Is it a properties transfer question? A xpath question?? I actually don't know. Sorry for my "newbie-ing".


Thanks in advance!!

5 Replies

  • MartinSpamer's avatar
    MartinSpamer
    Frequent Contributor
    Write Groovy Test Step to create your Random Data.


    testCase = context.testCase

    def userName(String prefix)
    {
    Random rand = new Random()
    def alphaNumPool = ['a'..'z',0..9].flatten()
    def randomChars = (0..6).collect { alphaNumPool[rand.nextInt(alphaNumPool.size())] }
    return prefix + randomChars.join()
    }


    Then put the value into a property readily accessible by the request. e.g.


    testCase.setPropertyValue("newUserName", userName("user-"))


    The put the following into the request


    ${#TestCase#newUserName}
  • Lifeguard's avatar
    Lifeguard
    Occasional Contributor
    Thank yoy very much!


    Would it be possible to avoid using Groovy?



    +Thanks!
  • Lifeguard's avatar
    Lifeguard
    Occasional Contributor
    Thanks to all again.

    Ok so i did the following steps (and worked fine). I post them step by step so that any other person can read it. If I am wrong, just correct me




    Mi PUT method was like http://services.test.mycompany/api/user ... mypassword
    I really needed an "email" random value, cause it has to be "Unique".


    So I did the following :




    1. Go to the soapUI project, and select "New web Tescase"

    2. Paste the root url without the variables : http://services.test.mycompany/api/userdata?
    Uncheck "start recording"
    Click "Ok"

    3. Go to the recently created "Web testSuite", and click on "Setup script"


    4. I pasted basically MartinSpammer's code (Many Thanks!!)
    testCase = context.testCase

    def userName(String prefix)
    {
    Random rand = new Random()
    def alphaNumPool = ['a'..'z',0..9].flatten()
    def randomChars = (0..6).collect { alphaNumPool[rand.nextInt(alphaNumPool.size())] }
    return prefix + randomChars.join()
    }

    testCase.setPropertyValue("newEmailName", userName("user"))



    5. Next, I had two options :

    --- Option 5.1 ---

    Go to the http method, and add the parameters :

    email ; Value = ${#TestCase#newEmailName}
    password; Value = 1234
    username; Value = userfake
    method type : "PUT"

    And that is all. Just go to the webtestcase and click on "Play"

    --- Option 5.2 ---

    Go to the http method and add the parameters
    email ; ---empty---
    password; Value = 1234
    username; Value = userfake

    method type : "PUT"


    Go to the "test steps", click add "property transfer"
    In "property transfer" Click "add a new property transfer" and type "username"

    (related to testCase.setPropertyValue("newEmailName",----> userName <-----("user")) )


    Click "Source" and select "Web Test Case" (or the name you set at the test-case creation) . Click Property and select "NewEmailName".
    Click "Target" and select the http method. Click "Property" and select "email"


    And that's all. Go to the webtestcase and just play.