How to handle checkbox in Robot Framework 

HOME

In this tutorial, we will automate the execution of the CheckBox in Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Step 1 – Create a new Project

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create a new directory in the new project

Right-Click on the project, select New->Directory and provide name as Tests

Below is the image of the new directory.

Step 3 – Create a robot Test File

Right-click on the new directory and select New File and provide the name as CheckBox_Demo.robot as shown below:

Step 4 – Download ChromeBinaries from the below location

The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.

https://chromedriver.chromium.org/

The chromedriver and geckodriver are placed in a folder name drivers in the RobotFramework_Demo project. I have renamed chromedriver to Chrome and geckodriver to Firefox.

Step 5 – Automate the selection of options for CheckBox

We are now going to write test cases. The test case details will be as follows −

  • Open the browser and copy URL − https://demo.automationtesting.in/Register.html in Chrome.
  • Verify that the page contains CheckBox.
  • Verify that the page does not contain CheckBox. Provided the locator for Radio button.
  • Check “Cricket” and “Hockey” options of CheckBox.
  • Uncheck “Hockey” option of CheckBox.
  • Verify that Cricket option of CheckBox is checked.
  • Verify that Hockey option of CheckBox is not checked.

To work with the Radio Button, we need a locator. A locator is an identifier for the textbox like id, name, class, xpath, css selector, etc.

To know more about locators, refer to these Selenium Tutorials:

 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText

Dynamic XPath  in Selenium WebDriver

CSS Selector in Selenium WebDriver

The below page shows that we have 3 CheckBoxes – Cricket, Movies, and Hockey.

Let us inspect the locator of the CheckBox.

Below is an example of selecting the “Cricket” and “Hockey” options in CheckBox.

*** Settings ***
Documentation    To validate the Login Form
Library     SeleniumLibrary
Test Teardown    Close Browser

*** Test Cases ***
Select an option of Radio Button
    Open the Browser with URL
    Verifies page contains a checkbox
    Select the checkbox options Cricket and Hockey
    Unselect the checkbox option Hockey
    Verify Checkbox option Cricket is selected
    Verify Checkbox option Hockey is not selected


*** Keywords ***
Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework/drivers/chromedriver_linux64
    Go To    https://demo.automationtesting.in/Register.html
    Maximize Browser Window
    Set Selenium Implicit Wait    2


Verifies page contains a checkbox
    Page Should Contain Checkbox    id:checkbox1
    Page Should Not Contain Checkbox    name:radiooptions   #This is the locator for Radio Button


Select the checkbox options Cricket and Hockey
    Select Checkbox    id:checkbox1
    Select Checkbox    id:checkbox3

Unselect the checkbox option Hockey
    Unselect Checkbox    id:checkbox3

Verify Checkbox option Cricket is selected
    Checkbox Should Be Selected      id:checkbox1

Verify Checkbox option Hockey is not selected
    Checkbox Should Not Be Selected    id:checkbox3

All the below-mentioned keywords are derived from SeleniumLibrary except the last one. The functionality of keywords mentioned above:

1. Create Webdriver − The keyword creates an instance of Selenium WebDriver.

2. Go To – This keyword navigates the current browser window to the provided URL – https://demo.automationtesting.in/Register.html.

3. Maximize Browser Window – This keyword maximizes the current browser window.

4. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

5. Page Should Contain Checkbox − This keyword is used to find the checkbox that is located on the current page.

6. Page Should Not Contain Checkbox – This keyword is used to find that the checkbox is not locator on the current page.

7. Select Checkbox – This keyword is used to select options “Cricket” and “Hockey” of the CheckBox.

8. Unselect Checkbox – This keyword is used to unselect the option “Hockey” of the CheckBox.

9. Checkbox Should Be Selected – This keyword verifies if the checkbox “Cricket” is checked.

10. Checkbox Should Not Be Selected – This keyword verifies if the checkbox “Hockey” is not checked.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.htm

To run this script, go to the command line and go to directory tests.

cd Tests

Step 6 – Execute the tests

We need the below command to run the Robot Framework script.

robot CheckBox_Demo.robot

The output of the above program is

Step 7 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Robot Framework Tutorials

HOME

Robot Framework is a generic open-source automation framework. It can be used for test automation and robotic process automation (RPA). RPA is extensively used for Web Application Automation, API Automation, RPA, and Database Testing.

Robot Framework has an easy syntax, utilizing human-readable keywords. Its capabilities can be extended by libraries implemented with Python, Java, or many other programming languages.

Chapter 1 Robot Framework Features – Settings, Libraries, Variables, Keywords, Resources, Reports, Logs
Chapter 2 What are variables in Robot Framework?
Chapter 3 How to handle text box in Robot Framework
Chapter 4 How to handle radio buttons in Robot Framework
Chapter 5 How to handle checkbox in Robot Framework
Chapter 6 How to handle dropdowns in Robot Framework
Chapter 7 How to handle multiple windows in Robot Framework
Chapter 8 How to handle alerts in Robot Framework
Chapter 9 What is Resource File in Robot Framework 
Chapter 10 How to run all the tests from the folder in Robot Framework
Chapter 11 How to implement tagging in Robot Framework
Chapter 12 How to rerun failed tests in Robot Framework
Chapter 13 How to use Drag and Drop in Robot Framework?
Chapter 14 How to set variable values from Runtime command in Robot Framework
Chapter 15 Page Object Model in Robot Framework with Selenium and Python
Chapter 16 Parallel Testing in Robot Framework
Chapter 17 How to write tests in Robot Framework in BDD Format

Data-Driven Testing

Chapter 1 Data-Driven Testing in Robot Framework 
Chapter 2 How to load data from CSV files in the Robot Framework?

API Testing

Chapter 1 How to perform API Testing in Robot Framework using RequestLibrary
Chapter 2 How to Implement Basic Auth in Robot Framework – NEW
Chapter 3 How to pass authorization token in header in Robot Framework – NEW
Chapter 4 Verifying Status Code and Status Line in Robot Framework – NEW

CI/CD

Chapter 1 Run Robot Framework Tests in GitLab CI/CD
Chapter 2How to run Robot Framework in GitHub Actions

Jenkins

Chapter 1 How to integrate Robot Framework with Jenkins
Chapter 2 How to run parameterized Robot Framework tests in Jenkins

How to automate selecting Checkbox and Radio Buttons in Selenium WebDriver

 
 

In this tutorial, we will see how to identify the form elements like CheckBox and Radio Button

Toggling a CheckBox or Radio Button on/off is also done using the click() method.

IsSelected – IsSelected method, let us know that the element selected or not. Assume there are two Radio Buttons/Check Boxes, one selected by default and you want to select the other one for your test. Then, we use IsSelected method. When there is a group of Radio Buttons/Check Boxes on the page then, most probably, their names are same, but values are different. Then we use the Webdriver findElements command to get the list of web elements.

Below is the working example of CheckBox.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class Checkbox_Test {
     
      public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");

          WebDriver driver = new ChromeDriver();
          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
          driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
 
           //Single option selection
           System.out.println("*******Single option selection *********");
           driver.findElement(By.id("isAgeSelected")).click();
           String Message = driver.findElement(By.id("txtAge")).getText();
           System.out.println("Message is :"+Message);    
 
       // close the web browser
        driver.close();   
    }
}

Output
*******Single option selection *********
Message is :Success - Check box is checked

How to click all options in the Checkbox

In the below image, there are 4 checkboxes. Initially, when checkboxes were not selected, the test mentioned on the button is – Check All and once all the options are checked, the text on button changes to – Uncheck All.

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public classCheckbox_Test {   
     
     public static voidmain(String[] args) throws Exception { 

 System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
          WebDriver driver = newChromeDriver();
          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
          driver.get("https://www.seleniumeasy.com/test/basic-checkbox-demo.html");
 
          //Display the value of Button before clicking the options of checkbox
          String buttontext_beforeclick = driver.findElement(By.xpath(".//*[@id='check1']")).getAttribute("value");
          System.out.println("Text before click :"+buttontext_beforeclick);

          // Find the CheckBox by its classname
          List list = driver.findElements(By.xpath("//input[@type ='checkbox' and @class='cb1-element']") );            
             
          // Get the number of CheckBoxes available
          int CheckBox_Size = list.size();
          System.out.println("Number of Checkbox options :"+CheckBox_Size);             
            
          // Iterate the loop from first CheckBox to last Checkbox
          for(int i=0;i<CheckBox_Size;i++)
          {     
                list.get(i).click();               
           }
          String buttontext_afterclick = driver.findElement(By.xpath(".//*[@id='check1']")).getAttribute("value");
          System.out.println("Text after click :"+buttontext_afterclick); 
    driver.quit();      
     }
}

Output
Text before click :Check All
Number of Checkbox options :4
Text after click :Uncheck All

Similarly, below I have explained Radio Button.

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
 
 public class Radio_Test {

       public static void main(String[] args) {      
 
     System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.24.0-win64\\geckodriver.exe");

          FirefoxDriver driver = new FirefoxDriver();
          driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
          driver.get("https://www.seleniumeasy.com/test/basic-radiobutton-demo.html");

          List<WebElement> Radio_Options = driver.findElements(By.name("optradio"));    
  
          // Create a boolean variable which will hold the value (True/False)
          boolean radio_value = false;

          // This statement will return True, in case of first Radio button is selected
         radio_value = Radio_Options.get(0).isSelected();

         //Identify if Radio Button 1 is selected or not. If Button 1 is already selected, then select Button 2
         if(radio_value==true)
         {
              Radio_Options.get(1).click();
              System.out.println("Button Selected is :"+ Radio_Options.get(1).getAttribute("value"));  
         }
         else
         {
               Radio_Options.get(0).click();
               System.out.println("Button Selected is :"+ Radio_Options.get(0).getAttribute("value"));  
          }

          driver.findElement(By.id("buttoncheck")).click();
          String Button_Selected = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[1]/div[2]/p[3]")).getText();
          System.out.println("Get Checked Value is :"+ Button_Selected); 
       
         //Group Radio Button Selection
         driver.findElement(By.xpath(".//input[@name='gender'and @value='Female']")).click();
         driver.findElement(By.xpath(".//input[@name='ageGroup' and @value='15 - 50']")).click();
         driver.findElement(By.xpath(".//*[@id='easycont']/div/div[2]/div[2]/div[2]/button")).click();
          String Group_Radio_Message = driver.findElement(By.xpath("//*[@id='easycont']/div/div[2]/div[2]/div[2]/p[2]")).getText();

          System.out.println("Get Values are :"+Group_Radio_Message);

         // close the web browser
        driver.close();
      }
}
 
Output
Button Selected is :Male
Get Checked Value is :Radio button 'Male' is checked
Get Values are :Sex : Female
Age group: 15 - 50