From c4da04e4de2d4ccd18a1c3fe9750e9147b7bd5bd Mon Sep 17 00:00:00 2001 From: Ramon Caballero Date: Thu, 25 Jul 2024 15:49:37 +0100 Subject: [PATCH] Apply POM to CheckboxesAndRadioButtons --- .../CheckboxesAndRadioButtonsPage.java | 70 +++++++++++++++++++ .../CheckboxesAndRadioButtonsTests.java | 33 +++++++++ .../CheckboxesAndRadioButtons.java | 48 ------------- 3 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 src/pages/webelements/CheckboxesAndRadioButtonsPage.java create mode 100644 src/tests/webelements/CheckboxesAndRadioButtonsTests.java delete mode 100644 src/webelements/CheckboxesAndRadioButtons.java diff --git a/src/pages/webelements/CheckboxesAndRadioButtonsPage.java b/src/pages/webelements/CheckboxesAndRadioButtonsPage.java new file mode 100644 index 0000000..f8ca5e3 --- /dev/null +++ b/src/pages/webelements/CheckboxesAndRadioButtonsPage.java @@ -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 checkboxes; + + @FindBy(xpath = "//div[@id='radiobuttons']//input") + private List 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 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 indices = new ArrayList<>(); + for (int i = 0; i < radioButtons.size(); i++) + { + indices.add(i); + } + + Collections.shuffle(indices); + + radioButtons.get(indices.get(0)).click(); + } +} diff --git a/src/tests/webelements/CheckboxesAndRadioButtonsTests.java b/src/tests/webelements/CheckboxesAndRadioButtonsTests.java new file mode 100644 index 0000000..9621044 --- /dev/null +++ b/src/tests/webelements/CheckboxesAndRadioButtonsTests.java @@ -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(); + } +} diff --git a/src/webelements/CheckboxesAndRadioButtons.java b/src/webelements/CheckboxesAndRadioButtons.java deleted file mode 100644 index ed124a7..0000000 --- a/src/webelements/CheckboxesAndRadioButtons.java +++ /dev/null @@ -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 checkboxes = driver.findElements(By.xpath("//div[@id='checkboxes']//input")); - List 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(); - } -}