I am trying to pass a variable inside another variable. Actually I want to use the response and pass it into another variable in the same script. How do I do that? Please help. I am new in the Framework. Thank you.
Hi Khaled,
Do you mean like this?:
*** Variables ***
${myvar_13} thirteen
${luckynum} 13
*** Test Cases ***
Lucky 13
Should Be Equal ${myvar_${luckynum}} thirteen
Log ${myvar_${luckynum}}
Dave.
To be precise,
Variables
${abc} This is a String
${def} This is another string with ${token}
*** Test Cases ***
Login Check
Request is sent using ${abc} and returns a token
${token}= token is extracted and saved in the variable
request is sent to perform another task using variable ${def}
Variables defined in the Variables section must be previously known (or it may not work at all).
What you want to do is to define a keyword that would accept the variable as argument, or get it from the scope, and returns the processed value.
Here is an experiment you can do:
*** Variables ***
${abc} This is a String
${def} This is another string with ${CURDIR}
*** Test Cases ***
Log Variable
Log ${def}
This is the exact thing I am trying to do. But it is not taking the variable ${grab_token_string} inside the string. What should I do in this case?
*** Settings ***
Library RequestsLibrary
Library Collections
Library OperatingSystem
Library String
Library JSONLibrary
*** Variables ***
${API_Base_Endpoint} http://localhost:8080/
${Token} Bearer ADMIN
${Create} mutation{ create (realm: “test1”, owner: “test1”) { id token realm } }
${Revoke0} mutation{ revoke (token: “${grab_token_string}” , reason: “nono”) { id token reason } }
*** Test Cases ***
Revoke A User
create session API_Testing ${API_Base_Endpoint}
${headers}= create dictionary Authorization=${Token} Content-Type=application/graphql
${response}= post request API_Testing /graphql data=${Create} headers=${headers}
${contents}= to json ${response.content}
${grab_token}= get value from json ${contents} $.data.create.token
log to console ${grab_token}
${grab_token_string} convert to string ${grab_token}
${response1}= post request API_Testing /graphql data=${Revoke0} headers=${headers}
${contents_revoked}= to json ${response1.content}
${grab_result}= get value from json ${contents_revoked} $.data.revoke.reason
${actual_code}= convert to string ${response1.status_code}
log to console ${grab_result}
should contain ${grab_result} nono
You could create a keyword that returns the value to ${Revoke0}, but you need to call it after defining, $
You are missing the point that the definitions on the *** Variables *** section are done before the actual Test Cases start being run.
This is a possible solution on your Test Case body:
(...)
${grab_token_string}= convert to string ${grab_token}
${Revoke0}= Set Variable mutation{ revoke (token: “${grab_token_string}” , reason: “nono”) { id token reason } }
${response1}= post request API_Testing /graphql data=${Revoke0} headers=${headers}
(...)
Thanks alot for the response. However, when I run the script according to your advice, it gives me the following error:
Revoke A User | FAIL |
ValueError: too many values to unpack (expected 2)
Well, that is a different problem.
Now you are probably passing a bigger list when it is expecting a tuple or two elements.
Tou show the final test result, but you should locate on what step is failing, and if necessary run with loglevel DEBUG or TRACE.