I have written a few test cases but some of them can be reused instead of writing them again in the new scripts. So if I have an Input text keyword for one field declared on one page how can that be used in an another test cases w/o having to mention the same keyword and locator again.
You can create a user defined Keyword in robot framework and paste your test case scripts inside the keyword then use keyword to execute your tests
Follow this documentation to create a user defined keywords,
http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-user-keywords
I would advise you to look at this Link
Robot Framework allows the use of Keywords, I would suggest to create a diffrent folder with all the keywords e.g To open the browser or login with a user to website or what ever you want.
You can create your logic inside the keyword section, and then pass the Name of the Custom Created keyword (Equal String) to the Test script.
Example of Keywords.
keywords.robot
*** Keywords ***
Equal String
Should be equal Hello Hello
Tests.robot
*** Settings ***
Resource keywords.robot
*** Test Cases ***
Validate Equal String
Equal String
You can only share keywords, not tests.
https://github.com/robotframework/robotframework/issues/2591
Related
I am running a templated test where I would like to pass the current date or None as the third parameter to the template.
Completeness
[Documentation] All the fields must be set or the it would fail
[Tags] new-antifraud
[Template] When we update the record with id ${id} outcome ${outcome} datetime ${update_datetime} the result should be ${result}
Id1 Accept '${Get Current Date UTC}' John Ok
Id2 None '${Run Keyword Get Current Date UTC}' Mark Error
None Accept '${Run Keyword Get Current Date UTC}' Susan Error
Is there a right syntax which allows to obtain this behaviour?
At the moment, no, robot has no syntax to allowing calling a keyword and using the result as an argument to another keyword. However, you could certainly write your template keyword to require that you pass in a keyword as a parameter, and the template keyword can run the keyword for you.
There is an open issue in the robot framework issue tracker for inline keyword evaluation: issue #3187
I'm trying to use combination Run Keywords and Create Dictionary built-in keywords
This code:
Run Keywords
... &{dict} Create Dictionary ${data}
returns:
Variable '&{dict}' not found.
any ideas?
message
Run Keyword specifically designed to be used in setup or tear down methods where creating an user defined keyword is an Overkill. User defined keyword does the same thing as Run Keywords but additionally it can return you the values from the enclosing keywords.
Here is the Run Keywords documentation
Coming to the problem. Run keyword will treat any variable argument as keyword unless it is not the parameter of the keyword using AND operator. In the error - &{dict} it is searching for the variable but it is not found. hence the error. Following code demonstrate the behavior of Run keywords
***Variable
${keyword}= comment something
***Testcase
Test1
Run Keywords ${keyword}
***Keyword
comment something
log This is comment
Output -
Actual resolution -
Define new keyword and enclose the keywords of your requirement and call the user defined keyword.
***Keyword
User Defined Keyword [Arguments] ${key} ${value}
&{dict}= Create Dictionary ${key} ${value}
Return from keyword ${dict}
*** Testcase
User Keyword Demo
&{dict}= User Defined Keyword name=xyz
Log ${dict}
Output =
Set of instructions which are written after [Return] statement are getting executed. Robot Framework should through an error or should not consider the keywords written after [Return] keyword. Please give explanation if I missed something.
Settings
Variables
Keywords
Custom Keyword
[Return] hyyyy
Return From Keyword hyyyy2222
Test Cases
Test1
${var_rt_ky2}= Custom Keyword
Log To Console ${var_rt_ky2}
Output:
hyyyy2222
In other case
Settings
Variables
Keywords
Custom Keyword
Return From Keyword hyyyy2222
[Return] hyyyy
Test Cases
Test1
${var_rt_ky2}= Custom Keyword
Log To Console ${var_rt_ky2}
Output:
hyyyy
This is how robot is designed to work. [return] merely defines the return value, it doesn't cause the keyword to return when it is called.
If you want to explicitly return from a function you need to use one of the keywords from the built-in library (e.g. Return from keyword or Return from keyword if, etc. )
I am new to Robot Framework and going through its documentation. In Robot Framework's overall test data syntax they state the below example:
*** Settings ***
Documentation Example using the space separated plain text format.
Library OperatingSystem
*** Variables ***
${MESSAGE} Hello, world!
*** Test Cases ***
My Test
[Documentation] Example test
Log ${MESSAGE}
My Keyword /tmp
Another Test
Should Be Equal ${MESSAGE} Hello, world!
*** Keywords ***
My Keyword
[Arguments] ${path}
Directory Should Exist ${path}
I am unable to understand what [Documentation] / Example test and [Arguments] ${path} in respective sections means - there does not seems to be a clear explanation for same?
How can I learn these basics syntax as the guide does not starts from basic "Hello World" program?
Overview
Values in square brackets in a test case or keyword definition are called settings. They are documented in the robot framework user guide in the sections titled Settings in the test case table and Settings in the keyword table.
Robot supports a fixed number of settings, so you can't just put any word you want inside square brackets. Anything in the first word of a line that is in square brackets will throw an error if it's not one of the supported settings.
Test cases support the settings [Documentation], [Tags], [Setup], [Teardown], [Template], and [Timeout]
Keywords support the settings [Documentation], [Tags], [Arguments], [Return], [Teardown], and [Timeout]
[Documentation]
As you surmised, [Documentation] is for setting the documentation for a test case or keyword. More information can be found in the robot framework user guide, in a section titled Test case name and documentation and User keyword name and documentation
The advantage to using [Documentation] instead of comments is that the documentation will appear in reports and logs, and will be included in documentation generated by libdoc and testdoc.
[Arguments]
[Arguments] are how you specify arguments to a keyword. You cannot use this setting for test cases. It is only available for keywords, and is documented in a section titled User keyword arguments
For example, if you write a keyword that accepts the arguments "first_name" and "last_name", you would define the [Arguments] setting like this:
*** Keywords ***
Example Keyword
[Arguments] ${first_name} ${last_name}
log Hello, my name is ${first_name} ${last_name}
Within the keyword, the first argument will be assigned to the local variable ${first_name}, and the second argument will be assigned to ${last_name}.
I have declared a variable in Variables section and initiated as ${Empty}.
Assigned the value in the TestCase and accessing the same in other keyword, it showed as ${Empty}.
my sample code is as below:
*** Variables ***
${fields} ${Empty}
*** Test Cases ***
Sample Code
${fields}= Get Service Details
Validate Service Details
*** Keywords ***
Get Service Details
... code for Get Service Details
[Return] ${fields}
Validate Service Details
${sValue}= Get Text ${serviceXpath}
Should be Equal As Strings ${fields} ${sValue}
Here, my question is, i delcared ${fields} in Variable section, i assigned value comes from the keyword Get Service Details, i want to use the updated value in Validate Service Details section.
I thought, as i have declared the variable in variable section, the updated value will be there in Validate Service Details keyword, but it displayed empty value.
How I can get updated value in Validate Service Details keyword
In Get Service Details, call Set Test Variable, Set Suite Variable, or Set Global Variable instead of (or in addition to) using [return] to make the changes visible outside of the keyword scope.
*** Keywords ***
Get Service Details
... code for Get Service Details
set test variable ${fields}