How do I find if a variable has been defined? - robotframework

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

Related

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?

Using Winsock (TCP/IP) functions in ATEASY development enviroment

I am using WsReceive() function of the ATEasy framework and wanted to ask what is the meaning of the values "aioDefault
and aioDisableWsReceiveEarlyReturn" of "enMode" parameter?
I found this in the ATEASY documentation:
If enMode, input receive mode includes aioDisableWsReceiveEarlyReturn,
it prevents WsReceive from an "early return" when there is a momentary
interruption in the data being received.
And this from the online help of ateasy (By a tip of an expert from the ateasy forum) :
If sEos parameter is an empty string and aioDisableWsReceiveEarlyReturn mode flag is not used (default case), the function will return immediately if characters are found in the input buffer, and the timeout will be ignored. Using the aioDisableWsReceiveEarlyReturn flag will ensure that the function will return only if the timeout is reached or all lBytes characters were received.

Can multiple requests update a single environment variable in Paw?

I have a variable named primary_address_id which can be set or updated via several API requests. For example, I may call AddAddress and specify that the new address should be the primary, or I can call MakePrimaryAddress to set an existing address as the primary.
I'm coming from Postman where I have tests defined for each of these API endpoints to update primary_address_id -- simple. But I can't find a way to do this in Paw; it seems I have to set the value to the response of just a single request. Am I missing something obvious? Or is this feature planned for a future release?
A workaround is to set the value of primary_address_id to the response from GetPrimaryAddress, but that means if I'm adding or updating an address I have to make a second call just to update my environment (which I may forget to do). If I could trigger GetPrimaryAddress to run after the Add/Update/List/etc endpoints that would be an acceptable workaround, but I shouldn't need to manually make two separate requests to accomplish this.
It sounds like you will need to make two subsequent requests but you can make groups of requests that will execute in sequence from one command.
Right click the request list and click "New Group" then within that group you can make a sequence of requests that will update your desired environment variable each time.
Create a new group of requests
To run a group of requests click on the group name; in this case "Address" and then click "Send Requests"
Execute group of requests in sequence
Hope this helps.

Can you please give me an example for Switch Connection Using Telnet library on Robot framework

I Don't how to use the Switch connection for the following test case.
Following one is my test case...So for this how can i switch the connections from R1 to R2 & again to R2 :
*** Settings ***
Library Telnet
*** Variables ***
${R1} 20.1.1.1
${R2} 20.1.1.2
${username1} naveen
${password1} kumar
${username2} hi
${password2} bye
*** Test Cases ***
telnet4
Open Connection ${R1} prompt=username prompt=password alias=conn01
Write ${username1}
Write ${password1}
Read Until >
Write enable
Read Until Password
Write ${password1}
Write R1
Read Until \#
Write show ip route
Read Until \#
Open Connection ${R2} prompt=username prompt=password alias=conn02
Write ${username2}
Write ${password2}
Read Until >
Write enable
Read Until Password
Write ${password2}
Write R2
Read Until \#
Write show ip route
Read Until \#
Switch Connection conn01
Switch Connection conn02
If you wish to change a previously set variable, the Set Variable keyword should do just fine, assuming the variable only needs to be available in its native context. Otherwise, Set Global Variable is what you're looking for.
Example of use of Set Variable:
*** Test Cases ***
Change a Variable
${thing} = Set Variable some
${thing} = Set Variable some1
Log to Console ${thing}
Variables declared in a variable table (like you did) can be declared based on other variables, so if you don't care if a variable is global, just declare it locally to keep the mutability.

How to write python function to test the matched strings (to use for Robot framework keyword)?

I am writing a custom library for robot framework in python. I don't want to use builtin library for some reasons.
My python code :
import os
import re
output = "IP address is 1.1.1.1"
def find_ip():
cmd = 'ipconfig'
output = os.popen(cmd).read()
match1 = re.findall('.* (1.1.1.1).*',output)
mat1 = ['1.1.1.1']
if match1 == mat1:
print "PASS"
In the above program I have written python function to :
Execute a windows command "ipconfig"
Written regular expression to match 1.1.1.1
create a list variable, mat1 = ['1.1.1.1']
Now I want to put condition like, if "match1" and "mat1" are equal my TEST should PASS. else it should fail in Robot framework.
Any one please give idea on how to write python function for this purpose?
Please note I dont want to use "Should Match Regexp" keyword in Robot Framework. Because I know it will do the same whatever I am asking.
To make a keyword pass, you don't need to do anything except return normally to the caller. To fail, you need to raise an exception:
def find_ip():
...
if match1 != mat1:
raise Exception('expected the matches to be similar; they are not")
This is documented in the robot user guide in the section Returning Keyword Status:
Reporting keyword status is done simply using exceptions. If an
executed method raises an exception, the keyword status is FAIL, and
if it returns normally, the status is PASS.
The error message shown in logs, reports and the console is created
from the exception type and its message. With generic exceptions (for
example, AssertionError, Exception, and RuntimeError), only the
exception message is used, and with others, the message is created in
the format ExceptionType: Actual message.

Resources