Test Suites in Robot Framework

In Robot Framework, we create various test cases in the test case files with the help of test case tables. These files automatically generate a test suite as they contain a few test cases. The test case files can also be organized into directories, these directories can be created under other directories and lead to a higher-level suite.  

We can customize the test suite by including Suite Setup and Suite Teardown which is also known as Suite Precondition and Suite Postcondition in the settings section. The suite setup will execute just once before the test cases execute and the Suite teardown will execute just once after all the test cases have been executed. We can also execute our test suits in multiple ways, i.e. Sequentially, parallelly, using bat file, and by configuring PyCharm. 

In this blog we will discuss: 

  • Project Setup for Test Suite 
  • Features that we can include in our test suite i.e., Documentation, Suite Setup, and Suite Teardown. 
  • How to create multiple test suites in a directory and run them sequentially as well as parallelly. 
  • How to run tests using Bat file. 
  • How to configure PyCharm to run a Test Suite. 

Project Setup for Test suites: 

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/

To ensure that the required libraries are installed, use pip list command, or manually navigate to: 

File > settings > project: robot automation> python interpreter 

Test Suites project setup

After verifying the prerequisites, create a file named ‘Test_Suite’  

— Create the file under ‘TestCases’ directory with .robot extension >Enter 

Project file

Given below are the examples for a better understanding of how to work with Test Suite in our test.

Testcase: 

Test Case 1: 

Step1: Click on the login link 

Step2: Login to the app 

Step3: Go back to the main menu 

Step4: Validate Successful login message 

Test Case 2: 

Step1: Click on the Key Presses link 

Step2: Press ENTER key 

Step3: Validate that the ENTER key was pressed 

*** Settings ***
Library    SeleniumLibrary
Library     CryptoLibrary    variable_decryption=True
Suite Setup     Start Test
Suite Teardown    End Test
Documentation    Suite Test on herokuapp.com
*** Variables ***
${URL}  https://the-internet.herokuapp.com/
${Browser}  chrome
${Credentials} 
crypt:ffihi//yDKfAxTZwyZ2EpypSM+acM6g8eaKga6QyU0sOpvIWc9zXY/5Byua2u7AcXt3YCMoX
MxzaeieCRQNWr5CXTvZOxafGACsZ6XcpGfv25hK2EtmWbgJs74XIs7K6GIzAu70UmMqz 
*** Test Cases ***
TC1: Login To Webpage
    click link    xpath://a[normalize-space()='Basic Auth']
    open browser    ${Credentials}  ${Browser}
    maximize browser window
    ${display_message}   get text    //p
    run keyword if    "${display_message}"=="Congratulations! You must have the proper
credentials."    Contains the expected output
    ...    ELSE     Didn't Contain Expected Text
TC2: Press Keys
    click link    xpath://a[normalize-space()='Key Presses']
    Press Keys  xpath://input[@id='target']  ENTER
    Element Text Should Be  id:result  You entered: ENTER
*** Keywords ***
Start Test
    open browser    ${URL}  ${Browser}
End Test
    close browser
Contains the expected output
    log    expected text received
Didn't Contain Expected Text
    log     Exception

***Settings*** – To work with robot framework keywords we need to import Library SeleniumLibrary in the settings section because all the keywords are built in the selenium library. Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section. 

***Variables*** – This section contains the defined variables that are used commonly, here we declared ${URL}, ${Browser}, and ${Credentials} variables for browser, URL, and login credentials so that we can reuse these variables. In the same way, we can create more variables according to the project requirement. 

Test Suites code

Code explanation: 

In the above test example, there is a single test file that contains 2 test cases, one is to check the login functionality and the other is to perform keyboard actions.  

In the Settings Section, the Documentation feature is used to provide the test suite documentation. 

Suite Setup feature is used to perform the initial steps before the test cases. Here as a part of the initial step, we need to open the required URL in the browser, so the keyword ‘Start Test’ containing these steps has been created in the keyword section and provided to the suite setup to run it once before the testcases execute.  

Suite Teardown feature is used to perform the final steps after the test cases. As a last step we need to close the browser and finish the test, so the keyword ‘End Test’ containing these steps has been created in the keyword section and provided to the suite teardown in order to run it once after all the testcases have been executed. 

We can also create a directory for the test suites, as follows: 

Test suite directory

Testcase for Test_1 and Test_2 test suites:

Here Test_1 and Test_2 are the two test suites that are created under the Testsuite directory.

Testcase for Test_1 test suites: 

Step1: Visit https://the-internet.herokuapp.com/ 

Step2: Click A/B Testing Link 

Step3: Validate that the header is A/B Test Control 

Step4: Close the browser

*** Settings ***
Library    SeleniumLibrary
Suite Setup    Start Test
Suite Teardown    End Test
*** Test Cases ***
TC_1: Click A/B Testing Link
    ${retVal}=  ABTesting Variation
    log    ${retVal}
    should contain    ${retVal}     A/B Test
*** Keywords ***
ABTesting Variation
    click link    //a[text()='A/B Testing']
    ${h3Text}   get text    //h3
    run keyword if    "${h3Text}"=="A/B Test Control" or "${h3Text}"=="A/B Test Variation 1"   
Contains One of the Variations    element should be enabled
    ...     ELSE    Didn't Contain Any Expected Value
    [Return]    ${h3Text}
Start Test
    open browser    https://the-internet.herokuapp.com/     chrome
    maximize browser window
Contains One of the Variations
    log    Text contains either A/B Test Control or A/B Test Variation 1
Didn't Contain Any Expected Value
    log     Exception
End Test
    close browser
Test_1 Test Suite code

Testcase for Test_2 test suites: 

Step1: Visit https://www.saucedemo.com/ 

Step2: Click on the login link 

Step3: Enter username 

Step4: Enter the password 

Step5: Click the login button 

Step6: Validate that the page contains the expected text 

Step7: Close the browser 

*** Settings ***
Library    SeleniumLibrary
*** Test Cases ***
TC_1 : Test the login functionality
    open browser    https://the-internet.herokuapp.com/     chrome
    Login to Herokuapp
    close browser
*** Keywords ***
Login to Herokuapp
    click link    xpath://a[normalize-space()='Form Authentication']
    input text    xpath://input[@id='username']     tomsmith
    input text    xpath://input[@id='password']     SuperSecretPassword!
    click button    Login
    element text should be    //h4    Welcome to the Secure Area. When you are done click logout below.
Test_2 Test Suites code

Code explanation: 

The Testsuite directory was created to store multiple test suites, here there are 2 test suites I.e., Test_1 and Test_2. The Test_1 suite has a testcase that will perform A/B Testing, and the Test_2 suite has a testcase that will check the login functionality, similarly, we can include more testcases into these test suites and create other test suite files as well.  

Execution:

To execute a specific test suite, we can use the command mentioned below: 

Execution command

This command will execute the Test_1.robot test suite 

In order to execute multiple test suites, we can use the below command: 

This command will execute all the test suite files with names starting with ‘Test’ in common sequentially. Here it will execute the Test_1.robot and Test_2.robot both suit one after the other.  

Or we can also execute the test suites without having any common name using the command below: 

This command will execute all the test suite files available in the Testsuite directory. 

Note: While creating test suite files, It is recommended that the name provided to the suites should have a common pattern so that we can easily run them all using a single command with the help of regular expression. 

Another way to run the test suites is to run them in parallel: 

To run the test suites in parallel we need to have ‘pabot library’, which is the parallel executor for tests in the robot framework. 

In order to install the ‘pabot library’ we can use the command given below: 

To ensure that the required library is installed, use pip list command, or manually navigate to: 

File > settings > project: Robot_Automation> python interpreter 

After installing the pabot library we can run the test suites in parallel using the syntax below: 

Syntax: pabot –processes <n> –outputdir Results <name>*.robot 

In this syntax ‘–processes’ represents the number of processes i.e. How many parallel executions need to be performed, ‘– outputdir’ represents the directory where we want to store the results/reports followed by the name of the test suite. 

E.g.: pabot –processes 2 –outputdir Results Test*.robot 

We can also store the above command inside Bat File instead of manually typing it in the terminal so that we can execute our test suite with a single click. 

In the above image, a Bat file has been created with the path of the project and the command that is needed to be executed. We can run this Bat file from any location, even without having PyCharm. After creating this file, to execute the tests right click on the Bat file and click on the ‘Run cmd script’ option.  

Another approach to execute the test suite is by configuring PyCharm to run the test suite using an external tool instead of passing the execution command in the terminal. We can simply right-click on the test suite and get the run option through which the test can be executed.  

To configure the run options in PyCharm IDE we need to do the following steps: 

Go to File > settings > Tools> External Tools> Click on + > Enter the Name> Enter Program> Enter Arguments> Enter Working directory> Click ok 

The Name section contains the name of the tool that we want to create, the program field contains the path of pybot bat file, the parameter field contains the file path of the test suite and the working directory field contains the path of the file directory. 

Next, we need to go to File > Settings > Editor > General > Smart Keys > Check Surround selection on typing quote or brace > click ok 

To execute the tests right click on the test suite, select external tool, and choose the created tool name

Execution Results: 

After execution, we should get the result as a pass. 

Reports: 

Upon execution, the reports will be stored in the Results directory. The Results directory will contain the detailed HTML reports created by the robot framework and the auto-generated ‘pabot_results’ directory consisting of output.xml file for every test suite, with each suite having a separate directory as shown in the below image: 

Log.html result: 

Test Suites log

Report.html result: 

Test Suites report