Robot Framework

How to handle checkbox in Robot Framework 

Checkbox in robot framework

In Testing, it is essential to understand how to interact with the browser and locate the DOM elements. It is quite easy to handle checkbox buttons with Robot Framework. In this blog, we will learn about how to handle checkbox buttons using the Selenium library. 

To work with the checkbox buttons in Robot Framework – we need the locator, which is the main unique identifier for that checkbox button, and it can be id, name, or class, and then we provide the value of the checkbox button to select a specific checkbox button. 

In this blog, we will discuss- 

  • Project setup for checkbox button 
  • Verify checkbox 
  • Select checkbox 
  • Unselect checkbox 
  • Verification of checkbox selected or not 

Project setup for textbox:

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/

After this, we can check our libraries by pip list command, or we can check manually also 

File > settings > project: robot-automation > python interpreter 

Project setup

After verifying the prerequisite part create a file checkbox.robot in the TestCases directory. 

— go to the new file > give a name of with .robot >Enter 

File name
Checkbox new file

Keywords: 

  • Page Should Contain Checkbox – Verifies checkbox locator is found on the current page 
  • Page Should Not Contain Checkbox – Verifies checkbox locator is not found on the current page 
  • Select Checkbox – Selects the checkbox identified by the locator 
  • Checkbox Should Be Selected – Verifies checkbox locator is selected/checked 
  • Unselect Checkbox – Removes the selection of checkbox identified by the locator 

Now let us write a test case for a better understanding of checkbox Keywords. 

Testcase:

Step1: Go to https://demo.automationtesting.in/Register.html  

Step2: Collect the specific locator for the checkbox from DOM 

Step3: Verifies page contains a checkbox  

Step4: Select the checkbox with id and its value  

Step5: Verify checkbox is selected or not 

Code:

*** Settings ***
Library    SeleniumLibrary
*** Variables ***
${browser}      chrome
${url}      http://demo.automationtesting.in/Register.html
*** Test Cases ***
Testing  Check Boxes
    open browser    ${url}      ${browser}
    maximize browser window
    set selenium speed    2seconds
#checkbox
    page should contain checkbox    id:checkbox1
    page should not contain checkbox    id:checkboox1
    select checkbox    checkbox2
    checkbox should be selected     checkbox2
    select checkbox    checkbox1
    select checkbox    checkbox3
    unselect checkbox    checkbox1
    checkbox should not be selected     checkbox1
    close browser
*** Keywords ***

***Settings*** – To work with robot framework keywords we need to import the selenium library because all these keywords are built in the selenium library-

Library SeleniumLibrary 

Same as this we can import other libraries here according to the requirement of the project, and the resources file. 

***Variables***– Here we declared variables of browser and URL so we can reuse these variables.

${url} ${browser}

In the same way, we can create more variables like this according to the requirement of the project. 

***Test Cases***- 

Checkbox in Robot framework code

Code Explanation: 

The first step in this test is to visit the URL  https://demo.automationtesting.in/Register.html in Chrome. We have already set up variables for this. Then we gave a name to our test Testing Radio Buttons. Open Browser * This keyword opens a specific browser for the given URL and maximizes browser window for maximizes the current browser window. While performing executions, we are also able to specify the speed of the Selenium process using the keyword set selenium speed. Using this keyword, we can see the execution part. We set a 2-second delay for each selenium command. 

We begin by checking if the page contains a checkbox or not. To do this, we are using the Selenium keyword Page Should Contain Checkbox with an id of the checkbox. If at this id there is a checkbox. It will pass, same as if at some other id there is no checkbox, we can also validate this by Page Should Not Contain Checkbox This keyword will also pass if there is no checkbox in the id. 

Now let us talk about some other action keywords like Select checkbox, which selects the checkbox value based on their id like checkbox1, checkbox2, checkbox3. 

Now we are verifying whether checkbox2 is selected or not for this we are using the keyword Checkbox Should Be Selected. It verifies checkbox locator is selected/checked. 

Similarly, we can also unselect the checkbox by unselect checkbox keyword and validate this by checkbox should not be selected. 

After executing the all-possible keywords for checkbox now we can go for a close browser to close browser and end the test. 

Execution: 

To execute the test case we need to use the command on the terminal: 

Execution command

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

Pass result

Report- 

Checkbox in Robot framework Checkbox report
Checkbox in Robot framework log

Conclusion-

In this Blog, we learned how to interact with the checkbox using the Selenium library in the Robot Framework. With the keywords provided by the robot framework and the library imported, we can do actions with checkboxes and verify them as well.

Read our next blog on “How to handle dropdowns in Robot Framework”.