Apply POM to CheckboxesAndRadioButtons
This commit is contained in:
parent
92d6619c5e
commit
c4da04e4de
|
|
@ -0,0 +1,70 @@
|
|||
package pages.webelements;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.support.FindBy;
|
||||
import org.openqa.selenium.support.PageFactory;
|
||||
|
||||
public class CheckboxesAndRadioButtonsPage
|
||||
{
|
||||
@FindBy(xpath = "//button[contains(text(),'Checkboxes and Radio Buttons')]")
|
||||
private WebElement accordionItem;
|
||||
|
||||
@FindBy(xpath = "//div[@id='checkboxes']//input")
|
||||
private List<WebElement> checkboxes;
|
||||
|
||||
@FindBy(xpath = "//div[@id='radiobuttons']//input")
|
||||
private List<WebElement> radioButtons;
|
||||
|
||||
private WebDriver driver = null;
|
||||
|
||||
public CheckboxesAndRadioButtonsPage(WebDriver driver)
|
||||
{
|
||||
this.driver = driver;
|
||||
PageFactory.initElements(this.driver, this);
|
||||
}
|
||||
|
||||
public void clickOnAccordionItem()
|
||||
{
|
||||
accordionItem.click();
|
||||
}
|
||||
|
||||
public void selectRandomCheckboxes(int howMany)
|
||||
{
|
||||
// Collections.shuffle(checkboxes) is not modifying 'checkboxes' !?
|
||||
// So I shuffle a list of indices instead...
|
||||
|
||||
List<Integer> indices = new ArrayList<>();
|
||||
for (int i = 0; i < checkboxes.size(); i++)
|
||||
{
|
||||
indices.add(i);
|
||||
}
|
||||
|
||||
Collections.shuffle(indices);
|
||||
|
||||
for (int i = 0; i < howMany; i++)
|
||||
{
|
||||
checkboxes.get(indices.get(i)).click();
|
||||
}
|
||||
}
|
||||
|
||||
public void selectRandomRadioButton()
|
||||
{
|
||||
// Collections.shuffle(radioButtons) is not modifying 'radioButtons' !?
|
||||
// So I shuffle a list of indices instead...
|
||||
|
||||
List<Integer> indices = new ArrayList<>();
|
||||
for (int i = 0; i < radioButtons.size(); i++)
|
||||
{
|
||||
indices.add(i);
|
||||
}
|
||||
|
||||
Collections.shuffle(indices);
|
||||
|
||||
radioButtons.get(indices.get(0)).click();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package tests.webelements;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||
|
||||
import pages.webelements.CheckboxesAndRadioButtonsPage;
|
||||
|
||||
public class CheckboxesAndRadioButtonsTests
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Specify path to WebDriver:
|
||||
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver");
|
||||
|
||||
// Launch browser and navigate to test page:
|
||||
WebDriver driver = new FirefoxDriver();
|
||||
driver.manage().window().maximize();
|
||||
driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/");
|
||||
|
||||
// Instantiate the page model:
|
||||
CheckboxesAndRadioButtonsPage page = new CheckboxesAndRadioButtonsPage(driver);
|
||||
|
||||
// Perform actions on the page:
|
||||
page.clickOnAccordionItem();
|
||||
page.selectRandomCheckboxes(3);
|
||||
page.selectRandomRadioButton();
|
||||
|
||||
//
|
||||
|
||||
// This is commented out so you can actually see what happened in the web page:
|
||||
// driver.quit();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
package webelements;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||
|
||||
public class CheckboxesAndRadioButtons
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Specify path to WebDriver:
|
||||
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver");
|
||||
|
||||
// Launch browser and navigate to test page:
|
||||
WebDriver driver = new FirefoxDriver();
|
||||
driver.manage().window().maximize();
|
||||
driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/");
|
||||
|
||||
// Click on the 'Checkboxes and Radio Buttons' accordion item:
|
||||
driver.findElement(By.xpath("//button[contains(text(),'Checkboxes and Radio Buttons')]")).click();
|
||||
|
||||
// Get checkboxes and radio buttons by XPath:
|
||||
List<WebElement> checkboxes = driver.findElements(By.xpath("//div[@id='checkboxes']//input"));
|
||||
List<WebElement> radioButtons = driver.findElements(By.xpath("//div[@id='radiobuttons']//input"));
|
||||
|
||||
// Shuffle the lists to get random order:
|
||||
Collections.shuffle(checkboxes);
|
||||
Collections.shuffle(radioButtons);
|
||||
|
||||
// Click on the first 3 checkboxes:
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
checkboxes.get(i).click();
|
||||
}
|
||||
|
||||
// Click on the first radio button:
|
||||
radioButtons.get(0).click();
|
||||
|
||||
//
|
||||
|
||||
// This is commented out so you can actually see what happened in the web page:
|
||||
// driver.quit();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue