General webpage functionality : Verify radio and checkbox selection

The user should be able to select only one radio option and any combination for checkboxes.

Language: Java

Framework: Selenium 4

copy
1/​/​Assumptions:2/​/​1. The webpage contains multiple radio and checkbox options3/​/​2. Radio options and checkboxes are identified by unique IDs or XPaths45/​/​Local driver implementation6import org.openqa.selenium.By;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.chrome.ChromeDriver;1011public class GeneralWebpageFunctionality {1213 public static void main(String[] args) {14 /​/​Connect to local driver15 System.setProperty("webdriver.chrome.driver", "path/​to/​chromedriver");16 WebDriver driver = new ChromeDriver();17 18 /​/​Navigate to webpage19 driver.get("https:/​/​example.com");20 21 /​/​Verify radio and checkbox selection22 WebElement radioOption1 = driver.findElement(By.id("radio1"));23 WebElement radioOption2 = driver.findElement(By.id("radio2"));24 WebElement checkBox1 = driver.findElement(By.xpath("/​/​input[@type='checkbox' and @id='checkbox1']"));25 WebElement checkBox2 = driver.findElement(By.xpath("/​/​input[@type='checkbox' and @id='checkbox2']"));26 27 /​/​Select radio option 128 radioOption1.click();29 if (radioOption1.isSelected() && !radioOption2.isSelected()) {30 System.out.println("Radio option 1 is selected");31 } else {32 System.out.println("Radio option 1 is not selected");33 }34 35 /​/​Select multiple checkbox options36 checkBox1.click();37 checkBox2.click();38 if (checkBox1.isSelected() && checkBox2.isSelected()) {39 System.out.println("Checkbox options 1 and 2 are selected");40 } else {41 System.out.println("Checkbox options 1 and 2 are not selected");42 }43 44 /​/​Close driver45 driver.quit();46 }47 48 /​/​Remote client implementation {49/​/​ import org.openqa.selenium.By;50/​/​ import org.openqa.selenium.WebDriver;51/​/​ import org.openqa.selenium.WebElement;52/​/​ import org.openqa.selenium.remote.CapabilityType;53/​/​ import org.openqa.selenium.remote.DesiredCapabilities;54/​/​ import org.openqa.selenium.remote.RemoteWebDriver;55/​/​ 56/​/​ import java.net.URL;57/​/​ 58/​/​ public class GeneralWebpageFunctionality {59/​/​ 60/​/​ public static void main(String[] args) throws Exception {61/​/​ /​/​Connect to remote client with desired capabilities62/​/​ DesiredCapabilities capabilities = new DesiredCapabilities();63/​/​ capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");64/​/​ capabilities.setCapability(CapabilityType.VERSION, "91.0");65/​/​ capabilities.setCapability(CapabilityType.PLATFORM_NAME, "Windows 10");66/​/​ WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);67/​/​ 68/​/​ /​/​Navigate to webpage69/​/​ driver.get("https:/​/​example.com");70/​/​ 71/​/​ /​/​Verify radio and checkbox selection72/​/​ WebElement radioOption1 = driver.findElement(By.id("radio1"));73/​/​ WebElement radioOption2 = driver.findElement(By.id("radio2"));74/​/​ WebElement checkBox1 = driver.findElement(By.xpath("/​/​input[@type='checkbox' and @id='checkbox1']"));75/​/​ WebElement checkBox2 = driver.findElement(By.xpath("/​/​input[@type='checkbox' and @id='checkbox2']"));76/​/​ 77/​/​ /​/​Select radio option 178/​/​ radioOption1.click();79/​/​ if (radioOption1.isSelected() && !radioOption2.isSelected()) {80/​/​ System.out.println("Radio option 1 is selected");81/​/​ } else {82/​/​ System.out.println("Radio option 1 is not selected");83/​/​ }84/​/​ 85/​/​ /​/​Select multiple checkbox options86/​/​ checkBox1.click();87/​/​ checkBox2.click();88/​/​ if (checkBox1.isSelected() && checkBox2.isSelected()) {89/​/​ System.out.println("Checkbox options 1 and 2 are selected");90/​/​ } else {91/​/​ System.out.println("Checkbox options 1 and 2 are not selected");92/​/​ }93/​/​ 94/​/​ /​/​Close driver95/​/​ driver.quit();96/​/​ }97/​/​ }98}

Language: Python

Framework: Selenium 4

copy
1#Assuming the web page has one radio button group and two checkbox groups2#Assuming the radio button group id is 'radio-group' and the checkbox groups ids are 'checkbox-group1' and 'checkbox-group2'3#Assuming we are using a local driver45from selenium.webdriver import Chrome6from selenium.webdriver.common.by import By7from selenium.webdriver.support.ui import WebDriverWait 8from selenium.webdriver.support import expected_conditions as EC 910PATH = "path/​to/​chromedriver"11driver = Chrome(PATH)1213#Connecting to a remote client with desired capabilities14'''15from selenium import webdriver16from selenium.webdriver.common.desired_capabilities import DesiredCapabilities1718capabilities = DesiredCapabilities.CHROME.copy()19capabilities['platform'] = "WINDOWS"20capabilities['version'] = "10"2122driver = webdriver.Remote(23 command_executor='http:/​/​192.168.0.1:4444/​wd/​hub',24 desired_capabilities=capabilities)25'''2627#Load the page28driver.get("https:/​/​example.com")2930#Wait for radio group and checkboxes to load31radio_group = WebDriverWait(driver, 10).until(32 EC.presence_of_element_located((By.ID, "radio-group"))33)34checkbox_group1 = WebDriverWait(driver, 10).until(35 EC.presence_of_element_located((By.ID, "checkbox-group1"))36)37checkbox_group2 = WebDriverWait(driver, 10).until(38 EC.presence_of_element_located((By.ID, "checkbox-group2"))39)4041#Select a radio button option42radio_button_options = radio_group.find_elements_by_xpath("./​/​input[@type='radio']")43radio_button_options[0].click()4445#Verify radio button option is selected46assert radio_button_options[0].is_selected()4748#Select checkbox options49checkbox_options_group1 = checkbox_group1.find_elements_by_xpath("./​/​input[@type='checkbox']")50checkbox_options_group2 = checkbox_group2.find_elements_by_xpath("./​/​input[@type='checkbox']")51checkbox_options_group1[0].click()52checkbox_options_group1[1].click()53checkbox_options_group2[0].click()5455#Verify checkbox options are selected56assert checkbox_options_group1[0].is_selected()57assert checkbox_options_group1[1].is_selected()58assert checkbox_options_group2[0].is_selected()5960#Verify no more than one radio button is selected61assert sum([option.is_selected() for option in radio_button_options]) == 16263#Close the browser64driver.quit()

Language: Javascript

Framework: Cypress

copy
1/​/​Assumptions:2/​/​1. The webpage has at least one radio button and checkbox.3/​/​2. The radio button and checkbox are identifiable by unique CSS selectors.4/​/​3. The Cypress environment is set up with the correct dependencies and testing infrastructure.56/​/​Code to connect to remote client with desired capabilities7const { remote } = require('webdriverio');8const options = {9 capabilities: {10 browserName: 'chrome',11 browserVersion: 'latest',12 platformName: 'Windows 10',13 'sauce:options': {14 username: '<SAUCE-USERNAME>',15 accessKey: '<SAUCE-ACCESS-KEY>',16 tunnelIdentifier: '<SAUCE-TUNNEL-ID>',17 build: '<BUILD-NUMBER>',18 name: 'Cypress Test'19 }20 }21};22const client = await remote(options);2324/​/​Code to test radio and checkbox selection using Cypress framework25describe('General Webpage Functionality', () => {26 it('should select only one radio option and any combination for checkboxes', () => {27 cy.visit('https:/​/​example.com') /​/​replace with the actual URL of the webpage28 cy.get('#radio-btn-1').click() /​/​replace #radio-btn-1 with the unique CSS selector for the radio button29 cy.get('#radio-btn-1').should('be.checked')30 cy.get('#radio-btn-2').should('not.be.checked')31 /​/​the above lines test if only one radio button is selected at a time3233 cy.get('#checkbox-1').check() /​/​replace #checkbox-1 with the unique CSS selector for the checkbox34 cy.get('#checkbox-1').should('be.checked')35 cy.get('#checkbox-2').check()36 cy.get('#checkbox-2').should('be.checked')37 /​/​the above lines test if multiple checkboxes can be selected simultaneously3839 cy.get('#checkbox-1').uncheck()40 cy.get('#checkbox-1').should('not.be.checked')41 cy.get('#checkbox-2').should('be.checked')42 /​/​the above lines test if checkboxes can be unselected4344 cy.get('#checkbox-3').should('not.exist')45 /​/​replace #checkbox-3 with the unique CSS selector for a non-existent checkbox46 /​/​the above line tests if a non-existent checkbox is not found on the webpage47 });48});

Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing