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

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?

Related

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

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}

How to avoid TypeError when calling keyword from the "Run Keyword If" (Robot Framework)

I have a nested dictionary inside another dictionary created in .py file.
When I am calling it like that
FOR ${key} IN #{keys}
I fill "${key}" tab on Applicant page with "${data}[${key}]" data
END
it works absolutely fine. However, if I use this call
Run Keyword If $ADD_TO_BANKING_KEY in $keys I add Applicant to Banking with "${data}[${ADD_TO_BANKING_KEY}]" data
I always get an error "TypeError: Expected argument 1 to be a dictionary or dictionary-like, got string instead." when I am trying to use the argument as a dictionary inside "I add Applicant to Banking with ..." function.
Example of the "${data}[${ADD_TO_BANKING_KEY}]": {'Member No.': 'DO_NOT_CHECK', 'Benefit': 'Regular', 'Reason Opened': 'radio'}
How can I solve this issue and do it in the right way?
Thank you!
Update:
First function:
I add Applicant to Banking with "${data}" data
Log To Console \#\#\# I add Applicant to Banking with "${data}" data
I fill "Add To Banking" tab on Applicant page with "${data}" data
Second Function:
I fill "${tabName}" tab on Applicant page with "${data}" data
Log To Console \#\#\# I fill "${tabName}" tab on Applicant page with "${data}" data
#{keys}= Get Dictionary Keys ${data}
FOR ${key} IN #{keys}
I input "${data['${key}']}" to "${key}" element in "${tabName}" tab on "Applicants" page
END
There is a place where it is failed
#{keys}= Get Dictionary Keys ${data}
Third function:
I input "${value}" to "${elementName}" element in "${tabName}" tab on "${pageName}" page
[Documentation] Input ``value`` to ``elementName`` element in ``tabName`` tab on the ``pageName`` page
Log To Console \#\#\# I input "${value}" to"${elementName}" element in "${tabName}" tab on "${pageName}" page
${tempLocator}= Get Locator From DOM ${RETAIL_DOM} ${pageName} ${elementName} ${tabName}
SeleniumExtendedResource.Input Value ${tempLocator} ${value}

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 catch click to dial events in Unified Service Desk?

The event raised by clicking on a phone number will either be of the form "tel:" or "skype:". Here are the steps I've followed so far to enable a window navigation rule to capture the event, and I've attached events to the rule to actually see the action get fired in the Debugger. Still, even with navigation rules set to capture tel: and skype:, the action will never fire in USD Debugger. Here is the general approach I've used so far (From another post):
Create a Windownavigation rule.
Don't put anything into the entity Settings but put "tel:" or "skype:" into the URL TextBox.
Routetype will be Popup
Target will be Tab (or registercard, at least I think that's the name for it in english - I'm using a german one)
Define None as Action in result for your Windownavigationrule
Create your own Action to resolve when the Navigation rule is triggered
Set your own hosted control (In this case I use the CTIConnector class.)
Define an Actionname for your Action that will be exectued (I named it "MakeCall" in CRM)
Set Data to [[SUBJECTURL]] so the URL ist given to the Action als Parameter.
Override the method DoAction from your hosted control
Just 2 -3 points to verify.
Do you have a UII action with the name "MakeCall"? If that is there then only your code will be triggered from DoAction.
In case, if you have above in place please check whether your action calls and other records are added to the respective configuration reocrd?

Set log level for built in keywords in robotframework

In robot framework, it looks like it logs messages for keywords like "=" by default with 'INFO' log level. Ex:
<Test case>
${xyz} = "hello"
Would log message with:
'INFO': ${xyz} = "hello"
I would like to lower the log level for this to 'DEBUG' or 'TRACE' but can't seem to find it in the source code.
An advice for this?
Have you tried to execute the test bringing the whole execution to a deeper level "DEBUG" or "TRACE" adding this -L trace or -L debug to your test call. robot -L trace mytest.robot for instance.
Also, you can set your log level in the code. Like this:
Test Setup Set Log Level TRACE
Then in the log.html file, a visible log level dropdown is shown in the upper right corner. This allows users to remove messages below the chosen level from the view. This can be useful especially when running tests at the TRACE level.
for more information see: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#visible-log-level
Source code defined like this
def log(self, message, level='INFO', html=False, console=False,
repr=False, formatter='str'):
u"""Logs the given message with the given level.
Valid levels are TRACE, DEBUG, INFO (default), HTML, WARN, and ERROR.
Messages below the current active log level are ignored. See
`Set Log Level` keyword and ``--loglevel`` command line option
for more details about setting the level.
usage example
Log you message:{message} level=DEBUG

Resources