Replacing a ${variable} within a file containing test date

1,925 views
Skip to first unread message

komodospider

unread,
Jan 26, 2015, 9:22:09 AM1/26/15
to robotframe...@googlegroups.com
Hello all, 
I've been having some issues with how to replace a variable appearing within test data. Let me explain. I am running tests data driven and lets say I have 2 test cases. One does something and returns an 'id'. This 'id' is then used by the 2nd test case to add another entry. Such dependency is currently unavoidable since the 'id' generated is unique each time and TestCase2 depends on it.
Currently when running testcase1 I get back an 'id' which I set as a suite variable. And then testcase2 uses this 'id' variable thus set. This works if the data is hardcoded into the test case. But when the data is abstracted into a test data file I dont know how to replace the '${id} in the test data.

As an example.
TestCase2 reads data from the file like this.
{"name":"some name", "previous id":${id}"}

What I would like to find out is 1. How to replace the ${id} with suite variable that I set after running TestCase1?
2. In another scenario, if I were to pass ${id} to TestCase2 as an argument how do I get it to replace the ${id} field in the test case data?

Both answers both be great appreciated. Thank you.


Kevin O.

unread,
Jan 26, 2015, 9:37:11 AM1/26/15
to robotframe...@googlegroups.com
BuiltIn.Replace Variables is what you need.


If test case 1 is setting/creating a suite variable called ${id}, then no further action is needed. ${id} in your JSON will be replaced.
${json}=    Read data from file    # json={"name":"some name", "previous id":${id}"}
${json}=    Replace Variables    ${json}    # json={"name":"some name", "previous id":96"}

komodospider

unread,
Jan 26, 2015, 11:04:09 AM1/26/15
to robotframe...@googlegroups.com
Hello Kevin, this unfortunately does not solve my problem since it replaces the entire string with the ${variable}. The string I want to send to the keyword is something like {"name":"some name", "value of id":"${id}"} and I want it to replace only the ${id} but it currently replaces that whole text.

to make is more clearer.

    ${data}        Extract data   ${data to be extracted}    #${data} will here contain  {"name":"some name", "value of id":"${id}"}
    ${new_data}       Replace Variables   ${id}      #currently replaces the entire ${data} string with the value of ${id} while I only want to replace the ${id} in the string with the value of ${id}

Kevin O.

unread,
Jan 26, 2015, 2:13:14 PM1/26/15
to robotframe...@googlegroups.com
You're giving the keyword the wrong argument value. You don't specify what variables to replace. Try this instead:

 ${new_data}       Replace Variables   ${data}

Here's a full working example. Since not reading from a file, ${id} has to be escaped with backslash.

*** Test Cases ***
1
    ${id}=    Set Variable    4
    Set Suite Variable    ${ID}    ${id}

2
    ${json}=    Set Variable    {"name":"some name", "value of id":"\${id}"}
    ${json}=    Replace Variables    ${json}



Starting test: Kwargs.1
20150126 13:08:48.031 :  INFO : ${id} = 4
20150126 13:08:48.033 :  INFO : ${ID} = 4
Ending test:   Kwargs.1

Starting test: Kwargs.2
20150126 13:08:48.036 :  INFO : ${json} = {"name":"some name", "value of id":"${id}"}
20150126 13:08:48.038 :  INFO : ${json} = {"name":"some name", "value of id":"4"}
Ending test:   Kwargs.2

komodospider

unread,
Jan 29, 2015, 10:47:52 AM1/29/15
to robotframe...@googlegroups.com
Hello Kevin, that was the problem btw thank you for pointing it out. Now it does what I expected it to do :)
Reply all
Reply to author
Forward
0 new messages