How to store Value returned from 1st keyword and then using that value in another keywords without running the 1st keyword again in robot framework - robotframework

I struck in one problem in which I want to store Value returned from 1st keyword in variable during initial run and then using that variable in another keywords without running the 1st keyword again .
I tried below code but not able to resolve the issue.
Create Account Post Request
${response}= call post request ${headers2} ${base_url} ${base_url_account} ${account_data}
log ${response}
Should Be True ${response.status_code} == 201
${account_id} get value from json ${response.json()} id
log ${account_id}
RETURN ${account_id}
Get Network ID --> [While running this keyword Create Account Post Request will run again which updates the account ID which I don't want]
${acc}= Create Account Post Request --> [During this It will run Create Account Post Request again which I don't want]
log ${acc}
My questions are :
How to use account_ID in any keywords without running the first
keyword again and again ?
How to use return value from 1st keyword in any other keywords? [Condition, I don't want to run 1st keyword again to get the return value]
Below scenario is expected :
1st Keyword ran
1st Keyword return some value
Store value in some variable
Use returned value anywhere in the suite file, in any keyword within the suite, in any test case .

Declare a blank variable
*** Variables ***
${account_id}
Add an IF condition in the keyword to run only if the account id is blank, else return the global variable. This way the function steps will be executed only once throughout the suite.
Create Account Post Request
IF "${account_id}" == "" --> ${account_id} should be in inverted commas "${account_id}".
${response}= call post request ${headers2} ${base_url} ${base_url_account} ${account_data}
log ${response}
Should Be True ${response.status_code} == 201
${account_id} get value from json ${response.json()} id
log ${account_id}
Set Suite Variable ${account_id}
END
[RETURN] ${account_id}

Related

Return in Robot Framework

I am trying to return a value from a keyword to my test case, but I am getting error
Variable 'xxx' not found.
This is my code.
*** Keywords ***
Click Dispatched check box from Status
${dispatchOptionPresent}= run keyword and return status click element ${selectDispatchedCheckbox_xpath}
set global variable ${g_dispatchOptionPresent} ${dispatchOptionPresent}
run keyword if '${dispatchOptionPresent}' == 'False' NoDispatchedStatusFoundforSelection
... ELSE Set Test Message *HTML* Applied <b>'Dispatched'</b> status filter
click element xpath://body
sleep 2
NoDispatchedStatusFoundforSelection
Fail 'Dispatched' status option not found...
[Return] ${g_dispatchOptionPresent}
*** Test Cases ***
Click 'Dispatched' check box for Status
[Tags] Filter-From Date/Status for LOA
[Documentation] Test to select 'Dispatched' check box for Status
${dispatchStatusFound}= Click Dispatched check box from Status
set global variable ${g_dispatchStatusFound} ${dispatchStatusFound}
pass execution if '${g_dispatchStatusFound}' == 'False' *HTML* <b>'Dispatch'</b> status found for selection!!!
For some reason, the test case is not seeing the returned value from the keyword section.
I checked for any formatting issues also, but no luck.
A similar scenario in one of my another test case works correctly with [Return]
But this one throws me :
Variable '${g_dispatchStatusFound}' not found.
Any help is much appreciated.
Thank you.

Evaluating keyword inside a for loop in robot framework

I am trying to use "exit for loop" in checking an Api availability
sleep till Rest api
FOR ${i} IN RANGE 20
Sleep 5s till rest api is available
Exit For Loop If Rest Api == True
Log to Console try again in 5 sec
END
When I try to execute this the exit for loop statement fails to recognise the Rest Api as another keyword and it throws an error like " no keyword named Exit For Loop If Rest Api ". Can we evaluate a keyword to exit from for loop instead of just variables?
Can we evaluate a keyword to exit from for loop instead of just variables?
No, you can't. The documentation clearly states that Exit for loop if requires a boolean expression. You will need to call the keyword first and save the result. You can then use the result as the expression.

How do I find if a variable has been defined?

How do I find out if a variable has been defined in my Robot Framework script? I am doing API testing, not UI testing. I have a complex set up and tear-down sequence and, since I am interacting with multiple computers through the script, it is important to know the current state if a fatal error has occurred. I could track what I have done with some complex set of meta variables or a variable tracking list, but I would prefer to query if a particular variable has been defined and if so take the appropriate tear-down steps.
A simplified version is something like:
*** Test Cases ***
Check monitor
${monitored}= Connect to Monitor ${Monitor IP Address} ${User name} ${password}
${peer connected}= Connect to Monitor ${Peer IP Address} ${User name} ${password}
Get Information from Monitor ${IP Address}
Send Info to Peer ${buffer1}
Report back to Monitor ${Monitor IP Address}
We are assuming that the tear-down closes the connections. I want to close any connections that are open, but if I failed to open the peer connection I will close the monitor connection and fail on closing the monitor connection.
I am trying to determine if ${peer connected} is defined. Can I look into Robot Framework's variable storage to see if it is there (in that dictionary?)?
You can call Get Variables to get a dictionary of all variables, then check whether the variable you're interested in is in the dictionary.
*** Test cases ***
Example
${foo}= set variable hello, world
${variables}= Get variables
Should be true "\${foo}" in $variables
Should not be true "\${bar}" in $variables
There a pretty straightforward approach - the built-in keyword Get Variable Value returns python's None (by default) if there is no such variable defined:
${the var}= Get Variable Value ${peer connected}
${is set}= Set Variable If """${the var}""" != 'None' ${True} ${False}
I am fine with this approach. In case the variable is not defined, the test case does not fail....
${variables} Get variables
${status} Run Keyword And Return Status Evaluate $new_table in $variables
${new_table} Set variable if ${status}==${FALSE} new_tbl ${new_table}
Also possible is:
${variables} Get Variables
IF "\${dataPluginVersion}" in "${variables}"
No Operation
ELSE
${dataPluginVersion} Set Variable 0
END
Or:
${variables} Get Variables
IF not "\${dataPluginVersion}" in "${variables}"
${dataPluginVersion} Set Variable 0
END
A shorter way:
OEM-T01-99-Test-variables
[Tags] TEST
Variable Should Not Exist \${TESTDEVICE_SSH_CONNECTION}
Variable Should Exist \${TEST_NAME}
This method is more readable and less verbose than using "Get Variables" keyword, IMHO
Reference: Robotframework built-in keywords

How to use values from one keyword to another keyword in robot framework

I would like to use value from keyword 1 to keyword 2. Tried searching on net but i could not able to solve it.
Verify that apps are listed
wait until element is visible ${AppMenuGrid} ${Timeout} "Apps NOT listed. Step execution failed"
log "Apps listed"
${APPSCOUNT} = GET ELEMENT COUNT ${AppMenuGrid}
log "Number of apps loaded are ${APPSCOUNT}"
[Return] ${APPSCOUNT}
Click on Refresh button
wait until element is visible ${Refresh} ${Timeout} "Refresh button is not visible"
click element ${Refresh}
log "click on refresh button successful"
Verify that same apps are listed
wait until element is visible ${AppMenuGrid} ${Timeout} "Apps list not refreshed. Step execution failed"
log "Apps list refreshed"
${APPSRECOUNT} = GET ELEMENT COUNT ${AppMenuGrid}
${Count} = verify that apps are listed ${APPSCOUNT}
log "Number of apps before refresh ${Count}"
log "Number of apps after refresh ${APPSRECOUNT}"
run keyword if "${APPSRECOUNT}" == "${Count}" log "Number of apps matching after refresh"
... ELSE fail "All apps not loaded after refresh"
I want to use AppsCount value (ex .10) from keyword "Verify that apps are listed" into "Verify that same apps are listed" keyword. But in the 2nd keyword, APPSCOUNT value is always blank.
Change the keyword Verify that same apps are listed to accept arguments:
Verify that same apps are listed
[Arguments] ${expected appscount}
# the rest of its code
And then, in the case where it's used, pass the value from the first keyword:
A case
${the count}= Verify that apps are listed
Verify that same apps are listed ${the count}
I agree with Todor Minakov's approach, to share the value via return clauses. Here is another approach:
Robot Framework (as described in the User Guide) has notion of variable scope: Local (Keyword) level, Test case level, Test suite level and Global. By default, the variables defined in the keywords have local scope.
To share the value of the variable between two keywords, just add a test case scope to the variable, like this:
Verify that apps are listed
wait until element is visible ${AppMenuGrid} ${Timeout} "Apps NOT listed. Step execution failed"
log "Apps listed"
${APPSCOUNT} = GET ELEMENT COUNT ${AppMenuGrid}
Set Test Variable ${APPSCOUNT}
Then, you can call ${APPSCOUNT} inside any other keyword in the same test case and it will have the stored value.
i tried the following and it worked.
In the test case file, i added a variables with the same name ${APPSCOUNT} and set the variables to the keyword like below,
Verify that apps are listed ${APPSCOUNT}
After this i can see value from keyword 1 in keyword 2.
Is this the correct approach?

Test Step Move Properties on SoapUI with http post request

I created a Soap-UI Test-Suite with a Test-case.
This Test-case has a http request as Test-Step.
The method of the http request is post.
The http request has the Parameter P_FILNR=1111&P_HDLNR=123456.
How can I set/modify these Parameters with the Test-Step?
as Mentioned by #A Joly above here is the code which can help you. I have used the custom property and a groovy script
First of all you can mention the property name under the value like ${#TestCase#address} <-- This means a test case property with the name address.
You can now add a groovy step with the below code
def values=["India", "Russia","USA"]
for(int i=0; i < 3 ; i++)
{
testRunner.testCase.setPropertyValue("address",values[i])
testRunner.runTestStepByName("Request 1")
}
So what happens here is the test step which we have to run has the step name as "Request 1". We are setting the value of Address dynamically and running the step via Groovy.
Also you can disable the request 1 step so that it does not run when you run the suite because groovy will run the request 3 times for 3 values

Resources