We’ll walk you through a hands-on example that demonstrates how to programmatically select and unselect checkboxes using GenAI or ChatGPT, a task that forms the backbone of many web testing and automation projects. Our comprehensive guide covers everything from setting up your Selenium environment to crafting and dissecting the automation code. So, let’s dive in:
ChatGPT or GenAI Prompt for HAndling Checkboxes
Write Java Selenium automation code to select and unselect the checkboxes on the webpage located at https://testautomationcentral.com/demo/checkboxes.html with the following HTML DOM:
<div class="bg-white p-6 rounded-lg shadow-md">
<h2 class="text-2xl font-bold mb-4">Checkboxes</h2>
<!-- Simple Checkboxes -->
<div class="mb-6">
<h3 class="text-xl font-semibold mb-2">Simple Checkboxes</h3>
<div class="space-y-2">
<label class="simple-checkbox-container">
<input type="checkbox">
<span>Option 1</span>
</label>
<label class="simple-checkbox-container">
<input type="checkbox">
<span>Option 2</span>
</label>
<label class="simple-checkbox-container">
<input type="checkbox">
<span>Option 3</span>
</label>
</div>
</div>
<!-- Styled Checkboxes -->
<div>
<h3 class="text-xl font-semibold mb-2">Styled Checkboxes</h3>
<div class="space-y-4">
<label class="checkbox-container flex items-start">
<input type="checkbox" class="form-checkbox text-green-600">
<span class="ml-2">Styled Option 1</span>
<p class="text-gray-500 text-sm ml-6 mt-1">This is a more detailed description for option 1.</p>
</label>
<label class="checkbox-container flex items-start">
<input type="checkbox" class="form-checkbox text-red-600">
<span class="ml-2">Styled Option 2</span>
<p class="text-gray-500 text-sm ml-6 mt-1">This is a more detailed description for option 2.</p>
</label>
<label class="checkbox-container flex items-start">
<input type="checkbox" class="form-checkbox text-yellow-600">
<span class="ml-2">Styled Option 3</span>
<p class="text-gray-500 text-sm ml-6 mt-1">This is a more detailed description for option 3.</p>
</label>
</div>
</div>
</div>
GENAI or ChatGPT Response:
Here’s the Java Selenium automation code to select and unselect the checkboxes on the provided webpage:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class CheckboxAutomation {
public static void main(String[] args) {
// Set the path to the chromedriver executable
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
// Initialize the WebDriver
WebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
// Implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Navigate to the webpage
driver.get("https://testautomationcentral.com/demo/checkboxes.html");
// Locate all checkboxes in the Simple Checkboxes section
List<WebElement> simpleCheckboxes = driver.findElements(By.cssSelector(".simple-checkbox-container input[type='checkbox']"));
// Select all simple checkboxes
for (WebElement checkbox : simpleCheckboxes) {
if (!checkbox.isSelected()) {
checkbox.click();
}
}
// Unselect all simple checkboxes
for (WebElement checkbox : simpleCheckboxes) {
if (checkbox.isSelected()) {
checkbox.click();
}
}
// Locate all checkboxes in the Styled Checkboxes section
List<WebElement> styledCheckboxes = driver.findElements(By.cssSelector(".checkbox-container input[type='checkbox']"));
// Select all styled checkboxes
for (WebElement checkbox : styledCheckboxes) {
if (!checkbox.isSelected()) {
checkbox.click();
}
}
// Unselect all styled checkboxes
for (WebElement checkbox : styledCheckboxes) {
if (checkbox.isSelected()) {
checkbox.click();
}
}
// Close the browser
driver.quit();
}
}
Explanation:
Explanation:
- Setup WebDriver: The code initializes the Chrome WebDriver and sets up the browser window to maximize.
- Implicit Wait: An implicit wait is set to handle delays in finding elements.
- Navigate to URL: The WebDriver navigates to the specified URL.
- Locate Simple Checkboxes: All checkboxes in the “Simple Checkboxes” section use a CSS selector.
- Select and Unselect Simple Checkboxes: Each checkbox is selected if it is not already selected, and then unselected if it is selected.
- Locate Styled Checkboxes: All checkboxes in the “Styled Checkboxes” section use a CSS selector.
- Select and Unselect Styled Checkboxes: Each checkbox is selected if it is not already selected and then unselected if it is selected.
- Close the Browser: The browser is closed after the operations are completed.