Robot Framework

How to handle radio buttons in Robot Framework 

Radio buttons in robot framework

In testing, it is essential to understand how to interact with the browser and locate the DOM elements. It is very easy to handle radio buttons with Robot Framework. In this blog, we will learn about how to handle radio buttons using the selenium library. 

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

In this blog, we will discuss- 

  • Project setup for Radio button 
  • Verify radio button 
  • Select radio button 
  • Verifying the value of the radio button 

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 radiobutton.robot in the TestCases directory. 

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

Project file
New file

Keywords: 

  • Page Should Contain Radio Button- verifies radio button locator found from the current page. 
  • Page Should Not Contain Radio Button– Verifies radio button locator is not found on the current page 
  • Radio Button Should Not Be Selected – Verifies radio button Group group_name has no selection  
  • Select Radio Button – Sets the radio button group Group_name to value   
  • Radio Button Should Be Set To – Verifies set radio button Group group_name to value 

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

TestCase:

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

Step2: Collect the specific locator for radio buttons from DOM 

Step3: Verifies page contains a radio button 

Step4: Select the radio button with id and its value ‘FeMale’ or ‘Male’ 

Step5: Verify radio button is selected or not 

*** Settings ***
Library    SeleniumLibrary
*** Variables ***
${browser}      chrome
${url}      http://demo.automationtesting.in/Register.html
*** Test Cases ***
Testting Radio Buttons and Check Boxes
    open browser    ${url}      ${browser}
    maximize browser window
    set selenium speed    2seconds
#Radio Buttons
    page should contain radio button    xpath:(//input[@name='radiooptions'])
    page should not contain radio button    xpath:(//input[@name='radiooptions123'])
    radio button should not be selected    radiooptions
    select radio button    radiooptions   FeMale
    select radio button    radiooptions   Male
    radio button should be set to    radiooptions   Male
    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***- 

Radio buttons in robot framework code

Code Explanation: 

The first step in this test is to open the URL https://demo.automationtesting.in/Register.html in Chrome for this we already created variables. Then we gave a name to our test Testing Radio Buttons. Now Open Browser − This keyword opens the specific browser for the given URL and maximize browser windowmaximizes the current browser window. While performing executions, also here we can set selenium speed by this we can see the execution part after applying this keyword here we set 2 seconds delay for each selenium command. 

Our first step is to check if the page contains a radio button. To do this, we are using the Selenium keyword Page Should Contain Radio Button with XPath of radio buttons. If at this XPath there is a radio button it will pass, same as if at some other XPath there is no radio buttons we can also validate this by Page Should Not Contain Radio Button This keyword will also pass if there are no radio buttons in the XPath. 

After these Verification keywords let us discuss about some other action keywords like Select Radio Button this keyword will select the radio button value like here in code we executed with both values ‘Male ‘and ‘FeMale’. 

We can verify this also that radio button is selected or not as we can see in code after selecting ‘Male’ radio button we are using keyword Radio Button Should Be Set To to check whether the selected radio button is ‘Male’ or not. 

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

Execution: 

To execute the test case we need to use the command below: 

Execution command

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

Pass result

Report:

Radio buttons in robot framework report
Radio buttons in robot framework log

Conclusion:

In this Blog, we learned how to interact with the radio buttons 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 radio buttons and verify them as well. 

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