To interact with the "offre" radio buttons (e.g., "BASIQUE 01", "BASIQUE 02", "BASIQUE 03") using Selenium in Java, you can utilize the Page Object Model (POM) with the PageFactory pattern. Below, I'll guide you through creating a robust and maintainable approach to select these options based on their labels.
1. Understanding the HTML Structure
From the provided HTML, each "offre" option is encapsulated within a <label> element containing:
- An
<input> of type radio with the name attribute set to "offre".
- A
<span> with the class "titre" that contains the offer name (e.g., "BASIQUE 01").
This structure allows us to locate the radio buttons either by their value attributes or by the visible text labels.
2. Setting Up the Page Object
First, create a Page Object class that represents the page containing the "offre" options. Here's how you can structure it:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
// Assuming you have a DriverManager class to manage WebDriver instances
public class OffrePage {
private WebDriver driver;
private WebDriverWait wait;
// Constructor
public OffrePage(WebDriver driver) {
this.driver = driver;
// Initialize PageFactory elements
PageFactory.initElements(driver, this);
// Initialize WebDriverWait with a 30-second timeout
this.wait = new WebDriverWait(driver, 30);
}
// Locator for all "offre" radio buttons
@FindBy(name = "offre")
private java.util.List<WebElement> offreRadioButtons;
// Method to select an "offre" by visible text (e.g., "BASIQUE 01")
public void selectOffreByName(String offreName) {
// Construct XPath to locate the label containing the specific offre name
String xpathExpression = "//label[contains(., '" + offreName + "')]//input[@name='offre']";
// Wait until the desired radio button is visible and clickable
WebElement desiredRadio = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath(xpathExpression))
);
// Click the radio button if not already selected
if (!desiredRadio.isSelected()) {
desiredRadio.click();
}
}
// Alternatively, select "offre" by value attribute (e.g., "35" for BASIQUE 01)
public void selectOffreByValue(String value) {
// Construct XPath to locate the input with the specific value
String xpathExpression = "//input[@name='offre' and @value='" + value + "']";
// Wait until the desired radio button is visible and clickable
WebElement desiredRadio = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath(xpathExpression))
);
// Click the radio button if not already selected
if (!desiredRadio.isSelected()) {
desiredRadio.click();
}
}
// Additional methods can be added here, such as verifying selection, etc.
}
3. Using the Page Object in Your Test
Here's how you can utilize the OffrePage in your test cases:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OffreTest {
public static void main(String[] args) {
// Set up WebDriver (Ensure ChromeDriver path is set in system properties)
WebDriver driver = new ChromeDriver();
try {
// Navigate to the target page
driver.get("https://tretr.net"); // Replace with the actual URL containing the offres
// Initialize the OffrePage
OffrePage offrePage = new OffrePage(driver);
// Example: Select "BASIQUE 01" by name
offrePage.selectOffreByName("BASIQUE 01");
// Alternatively, select "BASIQUE 02" by value
// offrePage.selectOffreByValue("36");
// Add assertions or further interactions as needed
} finally {
// Close the browser after the test
driver.quit();
}
}
}
4. Explanation of the Approach
-
Dynamic Locators:
- By Name: The
selectOffreByName method locates the radio button based on the visible text (e.g., "BASIQUE 01"). It constructs an XPath that searches for a <label> containing the specified text and then finds the <input> within it.
- By Value: The
selectOffreByValue method locates the radio button based on the value attribute of the <input> element.
-
Waiting Mechanism:
- The
WebDriverWait ensures that Selenium waits until the desired radio button is both visible and clickable before attempting to interact with it. This helps in handling dynamic page loads and reduces the chances of ElementNotInteractableException.
-
Selection Logic:
- Before clicking, the method checks if the radio button is already selected to avoid unnecessary clicks.
-
PageFactory Initialization:
- The
PageFactory.initElements method initializes all the @FindBy annotated elements when the OffrePage object is created.
5. Enhancements and Best Practices
-
Error Handling: You can enhance the methods to throw custom exceptions if the desired "offre" is not found, making debugging easier.
public void selectOffreByName(String offreName) throws NoSuchElementException {
String xpathExpression = "//label[contains(., '" + offreName + "')]//input[@name='offre']";
WebElement desiredRadio = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath(xpathExpression))
);
if (!desiredRadio.isSelected()) {
desiredRadio.click();
}
}
-
Logging: Integrate logging to record which "offre" was selected, aiding in test traceability.
-
Parameterization: If you have multiple similar pages or dynamic elements, consider parameterizing the locators or using more advanced strategies like using @FindAll or custom locators.
-
Reusable Components: If selecting "offre" is a common action across multiple tests, keeping it within the OffrePage ensures reusability and cleaner test code.
6. Alternative Approach: Using Labels Directly
Another effective strategy is to click on the <label> itself, which inherently toggles the corresponding radio button. This approach can sometimes be more reliable, especially if the input elements are hidden or not directly interactable.
public void selectOffreByLabel(String offreName) {
// Construct XPath to locate the label containing the specific offre name
String xpathExpression = "//label[contains(., '" + offreName + "')]";
// Wait until the desired label is visible and clickable
WebElement desiredLabel = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath(xpathExpression))
);
// Click the label to select the radio button
desiredLabel.click();
}
You can use this method in your Page Object similarly:
// Example usage
offrePage.selectOffreByLabel("BASIQUE 03");
7. Conclusion
By utilizing dynamic locators based on either the visible text or specific attributes, you can create flexible and maintainable Selenium tests to interact with the "offre" radio buttons. The provided Page Object structure with corresponding methods ensures that your test code remains clean and easy to manage.
If you encounter any specific issues or have further customization needs, feel free to ask!