Katalon studio - Call and Pass variable to another test case - automated-tests

I want to call a test case and pass a variable of type webdriver and extenttest and his values to another testcase but i don’t have type “other” in the list of type and i can’t pass the value

Define "extent" and "extenttest" variables (in "Variables" tab beside "Script" tab) in "test1" test case with the 'String' type and null value ('') as default. (Define them just in "test1")
Simply Call test case "New Test Case" wherever you want in "test1".
"Call test case" can add manually from "+Add" button or using this command in script mode:
WebUI.callTestCase(findTestCase('New Test Case'), [:], FailureHandling.STOP_ON_FAILURE)

Related

Error "a name is not defined" when using a variable in expression

I am trying to test against multiple environments with a single test case using the passing in of a variable from the command line. Using the following command line:
robot --variable TESTENV:prod advertisingdisclosure_Page.robot
I need to test the value of TESTENV and depending on the value passed in set a different variable, specifically a URL, to the appropriate value. With the following code in the first keyword section of the test case I get an error:
IF ${TESTENV} == "uat"
$(MAIN_URL)= Set Variable ${env_data['uat_url']}
ELSE IF ${TESTENV} == "dev"
${MAIN_URL}= Set Variable ${env_data['dev_url']}
ELSE IF ${TESTENV} == "prod"
${MAIN_URL}= Set Variable ${env_data["prod_url"]}
ELSE
Fail "No URL specified"
END
I get the following error:
Evaluating expression 'prod' failed: NameError: name 'prod' is not defined nor importable as module
The examples I have found show how to use the Global Variable directly, but not how to evaluate it for a specific value.
Help.
Jeff
You need to put quotes around the variable name - otherwise the framework just puts its value in, and passes that expression to python.
Thus it becomes prod == '"uat", and py rightfully complains there is no defined variable prod.
The fix is simple - just surround the usage in quotes, and after substitution this will become a string comparison:
"${TESTENV}" == "uat"
Alternatively you can use another syntax - no curly brackets, which tells RF to use/pass the variable itself, not its value, and then it will be a defined one for python:
$TESTENV == "uat"

Robot FW : Buitlin library : Set Suite Variable : How to pass the variable within the current test scope to a new test scope?

Acc.to the documentation:
(1) If a variable already exists within the current scope, the value can be left empty and the variable within the new scope gets the value within the current scope.
The goal is to implement an example to statement (1).
Here is an attempt:
(Test 1/2) Use "Set Suite Variable" : Set Locally Scoped Variable As Suite Scoped
[Documentation] If a variable already exists within the current scope
... (i.e. Test 1/2), the value can be left empty and
... the variable within the new scope (i.e. Test 2/2) gets
... the value within the current scope (i.e Test 1/2)
${local_to_suite_scoped} = Set Test Variable ${3}
# intentionally not setting any value to comply with the statement (1)
Set Suite Variable ${local_to_suite_scoped}
(Test 2/2) Use "Set Suite Variable" : Use local_to_suite_scoped in this test
Variable Should Exist ${local_to_suite_scoped}
Should Be Equal ${local_to_suite_scoped} ${3} # fails with None != 3, expected 3 == 3
Test 2/2 fails but why? Is statement (1) incorrect or are the test cases implemented incorrectly? If the test cases are implemented incorrectly, can you provide the correct implementation?
Set Test Variable (like Set Suite Variable -keyword) expects that the first argument is the name of the variable. Second variable will then be the value of the variable. So instead of
${local_to_suite_scoped} = Set Test Variable ${3}
do
Set Test Variable ${local_to_suite_scoped} ${3}
See more at http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Suite%20Variable

Robot framework: Assigning variables from a dictionary

I have a scenario in which I would have to read from a dictionary and create variables with key as the variable name and value as the variable's value.
For example, if "hello": "world" is a dictionary item, then I have to set hello = world. This variable also needs to be later called in a test case. I tried to achieve this via below method but not sure if it has the right syntax and anyway it fails with No keyword with name '${$k}} found error.
Can anyone let me know if this can be done?
*** Keywords ***
Spin Variables
${items}= get dictionary items ${arg}
:For ${k} ${v} IN #{items}
\ ${${k}}= set variable ${v}

How to Assign a variable after running Run keyword If - else ?

I am trying to assign a variable - body ,depending on the status of another variable phonenumber_id. If the phonenumber_id is NULL, body gets assigned False.
But it doesnt seem to be working. It works only if he phonenumber_id is not NULL.
${body}= Run keyword if '${phonenumber_id}'!='NULL' Set variable TRUE
... Else Set Variable FALSE
Not sure what i am doing wrong.
The keyword Set Variable If will set a variable based on a given condition
${body}= Set Variable If '${phonenumer_id} != 'NULL'
... ${True} ${False}
You got it almost right - just mistyped the ELSE - it must be in capital letters, to be considered a part of the Run Keyword If. So in your particular case, it should've been:
${body}= Run keyword if '${phonenumber_id}'!='NULL' Set variable TRUE
... ELSE Set Variable FALSE
For the simple case of just setting a new constant value though, #ILostMySpoon answer is good enough - and more "readable".
In general, for someone stumbling on this post, the Run Keyword If combined with ELSE Set Variable is a very powerful construct to set/change a variable - based on the fact that it not only runs a keyword(s) conditionally, but also propagates its return values back to the stack.
Consider this example:
${var}= Run Keyword If ${bool condition} Do Some Action Returning A Value
... ELSE Set Variable ${var}
In it {var} will be set to the return value of Do Some Action Returning A Value only if ${bool condition} evaluates to true, and will keep its old value otherwise.
Another artifical but less abstract example:
${value}= Run Keyword If ${should be int} Convert To Integer ${value}
... ELSE IF ${should be float} Convert To Number ${value}
... ELSE Set Variable ${value}

How to pass object-variables to test cases in RobotFramework?

Here's what's going on (generically) with my python-based test.
==============================================================================
***Settings***
Variables variable_file.py
Library library.py
***Test Cases***
A Test Case
myTest ${some string} [I want to pass an object here]
=============================================================================
At the end, I want to pass an actual class object which has been defined inside my variable file. I've looked far and wide without finding a way to pass anything beyond a string or a list. Does anyone know how?
By default, ${variable} are strings, but it can also contain objects.
And you can pass it to keywords as argument.
Take a simple example like that:
${mydict} = Create Dictionary a 1 b 2
=> Then you have ${mydict} = {'a': '1', 'b': '2'}
And then you can call a keyword with your object as argument. For example:
Dictionary Should Contain Key ${mydict} a
I am using that kind of thing to test a REST API that sends me JSON. I store JSON objects in Robot variable and check the results by exploring the content of the dict. I explained it a little bit in a blog post.
You can instantiate object variables in variable files as stated in User guide
Here is a generic example
Variable file:
#var_file.py:
class foo(object):
a = "foo"
b = "bar"
MY_VAR = foo()
MY_SECOND_VAR = foo
Test case file:
#var_file.txt
*** Test cases ***
log variables
log ${MY VAR.a}
log ${MY VAR.b}
Execute using:
pybot -V var_file.py var_file.txt

Resources