To interact with the offer radio buttons (e.g., "BASIQUE 01", "BASIQUE 02", "BASIQUE 03") using Java Selenium with the Page Object Model (POM) and Page Factory, you can follow these steps:
-
Identify the Locator Strategy:
- Since each offer is associated with a label containing the offer name, you can use XPath to locate the corresponding radio button based on the label text.
-
Implement the Page Object Class:
- Create methods within your Page Object class to select an offer based on its name.
- Use dynamic XPath expressions to locate and click the desired offer.
-
Example Implementation:
Here's a comprehensive example based on your provided HTML and existing Java Selenium code structure.
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 java.time.Duration;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class OfferSelectionPage {
private WebDriver driver;
private WebDriverWait wait;
// Initialize WebElements using Page Factory
@FindBy(name = "offre")
private List<WebElement> offerRadioButtons;
// Constructor
public OfferSelectionPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
// Initialize explicit wait
this.wait = new WebDriverWait(driver, Duration.ofSeconds(30));
}
/**
* Selects an offer based on the offer name.
*
* @param offerName The name of the offer to select (e.g., "BASIQUE 01")
* @throws NoSuchElementException if the offer with the specified name is not found
*/
public void selectOfferByName(String offerName) {
// Construct XPath to locate the label containing the offer name
String xpathExpression = "//label[contains(.,'" + offerName + "')]/input[@name='offre']";
// Wait for the element to be present and clickable
WebElement offerRadio = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpathExpression)));
// Click the radio button
offerRadio.click();
}
/**
* Alternative method: Clicks the label associated with the offer name.
* This ensures that any JavaScript listeners on the label are also triggered.
*
* @param offerName The name of the offer to select (e.g., "BASIQUE 01")
* @throws NoSuchElementException if the offer with the specified name is not found
*/
public void selectOfferByNameLabel(String offerName) {
// Construct XPath to locate the label containing the offer name
String xpathExpression = "//label[contains(.,'" + offerName + "')]";
// Wait for the label to be present and clickable
WebElement offerLabel = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpathExpression)));
// Click the label to select the radio button
offerLabel.click();
}
/**
* Selects an offer based on the radio button value.
*
* @param offerValue The value attribute of the radio button to select (e.g., "35" for BASIQUE 01)
* @throws NoSuchElementException if the offer with the specified value is not found
*/
public void selectOfferByValue(String offerValue) {
// Locate the radio button by its value attribute
String xpathExpression = "//input[@name='offre' and @value='" + offerValue + "']";
// Wait for the radio button to be present and clickable
WebElement offerRadio = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpathExpression)));
// Click the radio button
offerRadio.click();
}
// Additional methods related to offer selection can be added here
}
Explanation of the Implementation:
-
Page Object Class (OfferSelectionPage):
- WebDriver and WebDriverWait: These are initialized in the constructor for interacting with the browser and handling explicit waits.
- @FindBy Annotation: Although we have a list of all radio buttons (
offerRadioButtons), we're primarily using dynamic XPath within methods for flexibility.
-
Method selectOfferByName:
-
Method selectOfferByNameLabel:
-
Method selectOfferByValue:
Integrating with Existing Code:
Assuming you have a test case where you need to select an offer after logging in, here's how you can integrate the OfferSelectionPage:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class OfferSelectionTest {
public static void main(String[] args) {
// Setup WebDriver (example with ChromeDriver)
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
// Navigate to the target URL
driver.get("https://tretr.net");
// Initialize and perform login (assuming you have a LoginPage class)
LoginPage loginPage = new LoginPage(driver);
loginPage.loginWithUsernameAndPassword("ertretre", "retter");
// Initialize OfferSelectionPage
OfferSelectionPage offerPage = new OfferSelectionPage(driver);
// Select "BASIQUE 01" offer
offerPage.selectOfferByName("BASIQUE 01");
// Alternatively, select by label click
// offerPage.selectOfferByNameLabel("BASIQUE 02");
// Or select by value
// offerPage.selectOfferByValue("37");
// Continue with further actions, e.g., submitting the selection
// ...
} finally {
// Clean up and close the browser
driver.quit();
}
}
}
Best Practices:
-
Dynamic Locators: Using dynamic XPath expressions allows your tests to be more flexible and maintainable, especially when dealing with multiple similar elements.
-
Explicit Waits: Always use explicit waits (WebDriverWait) to handle dynamic content and ensure elements are interactable before performing actions.
-
Encapsulation: Keep your Page Object classes focused on interacting with specific pages or components, promoting reuse and readability.
-
Error Handling: Consider adding error handling or logging within your methods to better diagnose issues during test execution.
Additional Tips:
-
CSS Selectors: While XPath is powerful, sometimes CSS selectors can be more performant and readable. For example:
String cssSelector = "label:contains('" + offerName + "') > input[name='offre']";
However, note that Selenium's support for :contains in CSS selectors is limited, so XPath might still be the better choice for text-based matching.
-
Parameterization: For better scalability, consider parameterizing your test data (e.g., using external files or data providers) to handle multiple offer selections without hardcoding values.
By following this approach, you can effectively interact with the offer radio buttons in your web application using Java Selenium and the Page Object Model.