I am building three test cases with a page object that contains three ID locators. I am wondering if I can pass a locator to a single method for all three cases.
Page Object:
public class SomeClass {
@FindBy(how=How.CSS, using="label[for='yes']")
private WebElement yes;
@FindBy(how=How.CSS, using="label[for='maybe']")
private WebElement maybe;
@FindBy(how=How.CSS, using="label[for='no']")
private WebElement no;
public SomeClass(WebDriver driver) {
super(driver);
}
public SomeClass clicksButtons() {
*some locator*.click();
return new someClass(this.driver);
}
}
Test Case:
public class SomeTest {
@Test
public void willClickAButton() {
SomeClass someClass = new SomeClass(this.getDriver());
SomeClass.clicksButtons();
Assert.assertTrue(true);
}
I would like to pass a parameter (yes, maybe, or no) into the clicksButtons method so that I can reuse the method in the other two test cases without have to hardcode it. I've searched Google and couldn't find a clear answer.
I found the answer in Selenium Testing Tools Cookbook 2nd edition.
public void select(String label) {
//I found the buttons by class and stored them in a List<>. In my case there were 3.
List<WebElement> like = this.driver.findElements(By.className("custom-control-label"));
//iterate the buttons contained in the List<>
for(WebElement button : like)
{
//if the button's "for" attribute contains the given label...
if (button.getAttribute("for").equals(label))
{
//...and if it is not selected...
if (!button.isSelected())
{
//click the button, according to the label
button.click();
}
}
}
}