I'm trying to create a test setup hierarchy in robot framework.
I have a sub-suite, that defines its own Test Setup - but this overrides the parent suite's Test Setup.
I want both Test setups to run - one after the other, first the Parent Test Setup (that is defined in init.txt) and after that the Test setup that is defined using the * Settings * section.
You can achieve this kind of behavior at least with a bit of a hack way, by using set global variable, run keywords and an external resource file. This however requires you to define the test setup setting with a variable.
Example below:
Contents of __init__.txt:
*** Settings ***
Resource Resource.txt
Suite setup Set test setup variable
Test setup Test setup keyword
*** Keywords ***
Set test setup variable
Set global variable ${test setup variable} Test setup keyword
Contents of Resource.txt:
*** Keywords ***
Test setup keyword
Log Test setup from top level
Contents of Test_suite.txt:
*** Settings ***
Resource Resource.txt
Test setup Run keywords ${test setup variable} Test setup from test suite
*** Test cases ***
Test test setups
Log this should run two log keywords.
*** Keywords ***
Test setup from test suite
Log Test setup from test suite
I think this is the closest you can get.
Related
In case of setting Metadata for top-level test suites with the --metadata command line option (as described here) I don't see any working variants of accessing Metadata items (via &{SUITE METADATA} automatic variable, as mentioned here) within the Test Suite.
Namely, when running
pybot --metadata prettyMetaName:someMetaValue ...
trying to get key prettyMetaName in the test suite setup with &{SUITE METADATA}[prettyMetaName], I get this error:
Parent suite setup failed:
Dictionary variable '&{SUITE METADATA}' has no key 'prettyMetaName'.
More detailed part of the test:
*** Keywords ***
Custom Setup
Log &{SUITE METADATA}[prettyMetaName] level=WARN
*** Settings ***
Suite Setup Custom Setup
But if I try to get metadata via python library Listener API, I'm getting the valid result.
On the other hand, in case of explicit declaring Metadata in the Settings section, everything works as expected.
I'm using Robot 3.0.4.
I think you need an __init__.robot file in the root folder of your robot project with the following content.
*** Settings ***
Suite Setup Store Top Suite Metadata
*** Keywords ***
Store Top Suite Metadata
Set Suite Variable ${TOP SUITE METADATA} ${SUITE METADATA} children=True
Then you can use the ${TOP SUITE METADATA} variable everywhere else to get access to the metadata set by command line arguments.
How to use Variable and UserKeyword from test case level, which created at project level.
I have a robot framework project like:
Project
Suite1
TC_001
TC_002
Suite2
TC_003
TC_004
I have create Variable and UserKeyword at project level and I want to use those Variable and UserKeyword from test case level. My question is, how to use them at test case level?
From the Robot Framework User Guide there are two sections you may want to go through in more detail:
Variables
Resource and variable files
Both of these describe how to import variables and external keywords from external files. In essence a resource file is a regular robot file but without the test cases. It only contains keywords and has the regular settings and variable sections. It can be imported in your test suite file through the Resource common.robot construct:
*** Settings ***
Resource common.robot
Resource feature_1.robot
Resource feature_2.robot
*** Variables ***
${HOST} localhost:7272
*** Keywords ***
Open Login Page
Do something
You'll have to import the file created in the "Project" directory, and afterwards you'll have access to the variables and keywords defined in it.
If the file there is called "The_project_file.robot", in "Suite_1.robot":
*** Settings ***
Resource ../The_project_file.robot
*** Test Case ***
TC_001
Log ${variable defined in The_project_file}
${value}= Keyword Defined In The Project File
Is there a way to generate Robot Framework documentation for test cases?
I can successfully generate documentation for Keywords and Libraries, using libdoc, but when I try to do the same for .robot files that contain only test cases, I get the errors below.
Test case file:
*** Settings ***
Documentation Suite documentation to appear on top of the html doc.
Resource ../Root.robot
Suite Setup Create Data
Suite Teardown Delete Data
Test Setup Go To Homepage
Test Teardown Close All Browsers
*** Test Cases ***
Test A
[Documentation] The test case documentation.
Do Something
Errors when using libdoc on this file:
Try --help for usage information.
[ ERROR ] Error in file '/<path>Test.robot': Non-existing setting 'Test Setup'.
[ ERROR ] Error in file '/<path>Test.robot': Non-existing setting 'Test Teardown'.
[ ERROR ] Error in file '/<path>Test.robot': Non-existing setting 'Suite Setup'.
[ ERROR ] Error in file '/<path>Test.robot': Non-existing setting 'Suite Teardown'.
Resource file '/<path>Test.robot' contains a test case table which is not allowed.
Is it because the setups and teardowns are not supported in libdoc?
Should use TestDoc and not LibDoc for test case documentation.
Both tools here:
http://robotframework.org/robotframework/#built-in-tools
Hi, I am using robot framework to automate testing of a website, and the image above is the structure of test in the RIDE:
Test: a test suite folder, and I import resource file here, which is in the "init.robot" under the folder
Sub1: a sub test suite, import nothing
test: a test case
My problem is: in the test case "test", robot cannot recognize the keyword that imported in the "Test" test suite folder, because there will be more sub test suites, like sub2, sub3, how can I import resource in one place? I don't want to import the resource file in every test suite, is there a way to do that?
You can chain an imports. Below is an example of such a chain and reuse. In this example we have a single resources.robot that imports all of the different sub*.robot files. This is the only file that imports these.
Then there are two testcases*.robot files that proceed to import resources.robot and are able to access the content of the sub*.robot keywords.
resources.robot
*** Settings ***
Resource ../resources/sub1.robot
Resource ../resources/sub2.robot
Resource ../resources/sub1.robot
testcases1.robot
*** Settings ***
Resource ../resources/resources.robot
*** Test Cases ***
TC
No Operation
testcases2.robot
*** Settings ***
Resource ../resources/resources.robot
*** Test Cases ***
TC
No Operation
As discussed in the comments, that any keyword imported in the __init__.robot file is not available beyond that file. This is clearly described in the Robot Framework User Guide section on Initialization files.
That said, if the effort of including the master resource file in each suite file undesirable, then an alternative approach is to load the resource file using a listener at the start of each Suite. The documentation on Listeners can be found here: Docs
A new example:
AddResourceListener.py
from robot.libraries.BuiltIn import BuiltIn
class AddResourceListener(object):
ROBOT_LISTENER_API_VERSION = 2
def __init__(self):
pass
def start_suite(self, name, attributes):
BuiltIn().import_resource('${EXECDIR}/resource.robot')
resource.robot
*** Keywords ***
Resource Keyword
Log "Keyword Executed from Resource File"
TestCase.robot
*** Test Cases ***
TC
Resource Keyword
Then run your regular robot command with the additional argument --listener AddResourceListener.py and you'll be able to use the keyword regardless if it's imported or not.
Can you import files using Import Resource?
I need to be able to run the same testcases but using different variables to be able to run the same testcases in different languages. I have created different resource libraries to do this.
In order to tell my test cases which variable file I run a keyword to import only resource files for that country.
For example (This in the importAU text, which shares the same resource folder as the AU Resource.txt)
*** Keywords ***
AU
[Documentation] Initializes the AU keyword variables
Import Resource ../variables/AU/Resource.txt
And then this is the setup txt (Opens the browser and runs the proper files)
*** Settings ***
Library Selenium2Library
Resource ../variables/US/Resource.txt
Resource ../variables/AU/Resource.txt
Resource ../variables/DE/Resource.txt
*** Variables ***
${COUNTRY} AU //sets which country the file should run
*** Keywords ***
Homepage should be open
Run Keyword AU
Set Selenium Speed 1 second
Open Browser ${url_staging} chrome
Set log level TRACE
Maximize Browser Window
I ran this and this is the error I get.
SETUP: setup.Homepage should be open
Start / End / Elapsed: 20150707 18:56:35.038 / 20150707 18:56:35.048 / 00:00:00.010
00:00:00.009 KEYWORD: BuiltIn.Run Keyword AU
Documentation:
Executes the given keyword with the given arguments.
Start / End / Elapsed: 20150707 18:56:35.039 / 20150707 18:56:35.048 / 00:00:00.009
00:00:00.007 KEYWORD: importau.AU
Documentation:
Initializes the AU keyword variables
Start / End / Elapsed: 20150707 18:56:35.040 / 20150707 18:56:35.047 / 00:00:00.007
00:00:00.006 KEYWORD: BuiltIn.Import Resource ../variables/AU/Resource.txt
Documentation:
Imports a resource file with the given path.
Start / End / Elapsed: 20150707 18:56:35.041 / 20150707 18:56:35.047 / 00:00:00.006
18:56:35.046 FAIL Resource file '..\variables\AU\Resource.txt' does not exist.
Did I do something wrong with the keyword or is the keyword faulty? I know this was a known issue as RobotFramework but it was supposedly fixed.
Here is the link: http://code.google.com/p/robotframework/issues/detail?id=944
First of all, you can get use ${CURDIR} and his friends.
${CURDIR} An absolute path to the directory where the test data file is located. This variable is case-sensitive.
${TEMPDIR} An absolute path to the system temporary directory. In UNIX-like systems this is typically /tmp, and in Windows c:\Documents and Settings\\Local Settings\Temp.
${EXECDIR} An absolute path to the directory where test execution was started from.
They can also be used in the declaration of the Import Resource. This should save you some problems.
I've used a different method in the past - by defining a global "required" variable that is the main project folder, and using it in all of the imports.
You can pass such a variable in the command line (and force your users to use a batch wrapper) or if you use the maven wrapper, you can put it there.