Robot Framework, pass a variable from Suite Setup to test suite - robotframework

I am looking to make a new user per test suite to ensure state. I have setup keywords to achieve this as show below, the issue is getting the "USERNAME" accessible to the tests without making it a global (and thus preventing parallel running)
I currently receive a "Cannot Set Test variable when no test is started" error produced by robot.
MainTestSuite.robot
*** Settings ***
Resource base.robot
Suite Setup Suite Start Default
Suite Teardown Suite teardown Default
*** Test Cases ***
Test One
[Setup] Login ${USERNAME}
Do testing
...
Base.robot
*** Keywords ***
Suite Start Default
${USERNAME} Create User
Suite Teardown Default
Delete User ${USERNAME}
Possibly i have missed some variable definition, but happy to reformat to get the intended result

To set a suite-level variable in the setup (ie: a variable accessible only to the tests in the suite), use Set Suite Variable
*** Keywords ***
Suite Start Default
${USERNAME} Create User
set suite variable ${USERNAME}

Related

Should I use Force Tags or Default Tags

I want to make use of Tags to note down the defect ID in test case.
So that I can run specific test case which defect occurred easily.
From user guide, example, there is Force Tags and Default Tags at settings. For my case, which one should I use? :
*** Settings ***
Force Tags req-42
Default Tags owner-john smoke
*** Variables ***
${HOST} 10.0.1.42
*** Test Cases ***
No own tags
[Documentation] This test has tags owner-john, smoke and req-42.
No Operation
With own tags
[Documentation] This test has tags not_ready, owner-mrx and req-42.
[Tags] owner-mrx not_ready
No Operation
Own tags with variables
[Documentation] This test has tags host-10.0.1.42 and req-42.
[Tags] host-${HOST}
No Operation
Empty own tags
[Documentation] This test has only tag req-42.
[Tags]
No Operation
Set Tags and Remove Tags Keywords
[Documentation] This test has tags mytag and owner-john.
Set Tags mytag
Remove Tags smoke req-*
My test cases is written in one file and setup as test suite, the defect appear at one of step for both cases, is this the correct setup?:
*** Settings ***
Resource ../Resources/res.robot
Suite Setup Suite Setup Suite
Test Setup Test Setup
Suite Teardown Test Teardown
Default Tags Defect1
*** Test Cases ***
TC001-001-01
[Tags] Defect1
Go To Page 1
Go Back
TC001-001-02
[Tags] Defect1
Go To Page 2
Go Back
If you want both your tests to get the tag Defect1 then you can use either Force Tags in the Settings or [Tags] in the tests themselves.
Default Tags is not appropriate in that case because you would take the risk that some tests don't get the Defect1 tag if some other tag is defined on the test.
So I would say the 2 possibilities are:
Use of [Tags] (handy because you see directly when looking at a test that it has the tag)
*** Settings ***
Resource ../Resources/res.robot
Suite Setup Suite Setup Suite
Test Setup Test Setup
Suite Teardown Test Teardown
*** Test Cases ***
TC001-001-01
[Tags] Defect1
Go To Page 1
Go Back
TC001-001-02
[Tags] Defect1
Go To Page 2
Go Back
Use of [Force Tags] (handy because you don't have to repeat the tag on each test)
*** Settings ***
Resource ../Resources/res.robot
Suite Setup Suite Setup Suite
Test Setup Test Setup
Suite Teardown Test Teardown
Force Tags Defect1
*** Test Cases ***
TC001-001-01
Go To Page 1
Go Back
TC001-001-02
Go To Page 2
Go Back

Run Multiple test suite in same jsession using Robot Framework : Python

I have to run multiple test suite in same session and same session cookie in robot framework , since i want to test whether the url is working for all the nodes for doing that i have to hit url with same session cookies , which cannot be set explicitly . So is there any way i can run all the Test Suite in same session . ? or i can run all the test suite in same browser on different tabs .
So the code i tried runs each test suite on each browser page there by end of one browser run deleting session and session cookie created and the new test run will have new cookie and session , so that was the problem so when doing it , it test each test suite with different cookie . but i want it to be tested in same session and cookie .
Can anyone help me on this , this is second time posting this question in Stack.
I don’t know if this works the same way for opening tabs, but you can make test suite setup. Declare your environment and this environment will be shared in all the tests in the suite.
I used it with ssh, but same analogy should work in your case. If you run this code, you can see that the ssh connection exists in both tests, but is only initialized in the suite setup.
*** Settings ***
Library SSHLibrary
Suite Setup Run Keyword SSH Login ${your_host} ${host_name} ${host_pass}
*** Test Cases ***
Test 1
${output}= SSHLibrary.Get Connections
Log ${output}
Test 2
${output}= SSHLibrary.Get Connections
Log ${output}
*** Keywords ***
SSH Login
[Arguments] ${your_host} ${host_name} ${host_pass}
SSHLibrary.Open Connection ${your_host}
SSHLibrary.Login ${host_name} ${host_pass}
you can read more about suite setup and teardown here
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#suite-setup-and-teardown
For sharing a session, you can just use the Suite Setup to create the session and then use this session for the following tests, for example, you share a session named mysession :
*** Settings ***
Library RequestsLibrary
Library Collections
...
Suite Setup Create session mysession ${my_application_url}
...
Keywords
*** Keywords ***
Login
[Arguments] ${username} ${password}
${header}= Create Dictionary Content-Type=application/json
${body}= Create Dictionary username=${username} password=${password}
${resp}= Post Request mysession /login headers=${header} data=${body}
Log ${resp.content}
Get all users
#Don't create session again
${resp}= Get Request mysession /users
Log ${resp.text}
Tests
*** Test Cases ***
Test login
login tester tester
Test get users
Get all users
In this way, you can do login first, then use the same session in the next test. you could do a teardown to logout if you need. Then the cookie also can be found in ${myresponse.cookies} but the format is CookieJar. I don't use Selenium2Library but I think in this lib, you don't need to parse the CookieJar manually. then if you want to share the cookie, you can just set a global variable for this cookie. There is one similar question: Set session cookie across the test suites in Robot framework

Resource cleanup after each test

I'm using robot framework to test a REST API and as such I do have to create many temporary resources (test user or any other resource). Those are abstracted in keyword, but I need to clean them up after each test. But I would like to not worry about cleaning that explicitly in each test since it would require our test case to "play" at different levels of abstraction.
What would be great would be to have the keyword teardown running after the testcase is completed instead of directly after the keyword is completed.
I did some research but haven't found a good way to handle that.
Is there any solution to clean up resources created in a keyword at the end of a test case without doing it explicitly in the test case?
here is a code example to illustrate the situation:
helper.robot
*** Keywords ***
a user exists
create a user
Delete user
actually remove the user
test.robot
Resource helper.robot
*** Test Cases ***
test user can login
Given a user exists
When user login
Then it succeeds
[Teardown] Delete user
What I want is to move the teardown out of the test case to some other way that deletes the user after each test case, but without specifying if in each test case. I don’t want to configure that exact teardown at the setting level since we don’t always use the same resource for all test cases.
https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#test-setup-and-teardown
At the time of writing, the only special tags are robot:exit, that is
automatically added to tests when stopping test execution gracefully,
and robot:no-dry-run, that can be used to disable the dry run mode.
More usages are likely to be added in the future.
2.2.6 Test setup and teardown
Robot Framework has similar test setup and teardown functionality as
many other test automation frameworks. In short, a test setup is
something that is executed before a test case, and a test teardown is
executed after a test case. In Robot Framework setups and teardowns
are just normal keywords with possible arguments.
Setup and teardown are always a single keyword. If they need to take
care of multiple separate tasks, it is possible to create higher-level
user keywords for that purpose. An alternative solution is executing
multiple keywords using the BuiltIn keyword Run Keywords.
The test teardown is special in two ways. First of all, it is executed
also when a test case fails, so it can be used for clean-up activities
that must be done regardless of the test case status. In addition, all
the keywords in the teardown are also executed even if one of them
fails. This continue on failure functionality can be used also with
normal keywords, but inside teardowns it is on by default.
The easiest way to specify a setup or a teardown for test cases in a
test case file is using the Test Setup and Test Teardown settings in
the Setting table. Individual test cases can also have their own setup
or teardown. They are defined with the [Setup] or [Teardown] settings
in the test case table and they override possible Test Setup and Test
Teardown settings. Having no keyword after a [Setup] or [Teardown]
setting means having no setup or teardown. It is also possible to use
value NONE to indicate that a test has no setup/teardown.
*** Settings ***
Test Setup Open Application App A
Test Teardown Close Application
*** Test Cases ***
Default values
[Documentation] Setup and teardown from setting table
Do Something
Overridden setup
[Documentation] Own setup, teardown from setting table
[Setup] Open Application App B
Do Something
No teardown
[Documentation] Default setup, no teardown at all
Do Something
[Teardown]
No teardown 2
[Documentation] Setup and teardown can be disabled also with special value NONE
Do Something
[Teardown] NONE
Using variables
[Documentation] Setup and teardown specified using variables
[Setup] ${SETUP}
Do Something
[Teardown] ${TEARDOWN}
if you provide it in settings , it works as you want you don't have to specify for each test case

Robot FW : Builtin library : "Pass Execution" keyword

Referring to the documentation
When used in any setup or teardown (suite, test or keyword), passes that setup or teardown. Possible keyword teardowns of the started keywords are executed. Does not affect execution or statuses otherwise.
Could you elaborate with simple examples what the above statement mean?
Means that if you use on a suite, test or keyword, all the code after Pass Execution will not be called.
Example:
*** Settings ***
Suite Setup Setup
Suite Teardown Teardown
*** Keywords ***
Setup
Pass Execution Setup passed
Log to Console Setup
Teardown
Pass Execution Teardown passed
Log to Console Teardown
*** Test Cases ***
Keyword1
Pass Execution Keyword1 passed
Log Test
Pass Execution works like a return in python, making the keyword, testcase, teardown or setup exit with PASS. Try to comment `Pass Execution, then you can see the remaining code being executed

How to make Gherkin style testcase as datadriven in Robot Framework?

I have a test case in Gherkin style in Robot Framework. For example:
*** Settings ***
Documentation Tests Login to website
Resource ../keywords/resources.txt
Test Teardown Close Browser
*** Variables ***
${user} demo
${userpass} demo
*** Test Cases ***
Scenario: Login as a valid user to website
Given Browser is opened to home page
When I log in as ${user} with ${userpass}
Then I can see page after sign in and verify
And I will logout
Is there any way that I can run same test case for multiple sets of data?
You should be able to simply convert your test case to a keyword, then use that keyword with the Test Template feature. You can then create a whole set of permutations.

Resources