diff --git a/src/pages/waits/WaitingStrategiesPage.java b/src/pages/waits/WaitingStrategiesPage.java new file mode 100644 index 0000000..beb7836 --- /dev/null +++ b/src/pages/waits/WaitingStrategiesPage.java @@ -0,0 +1,96 @@ +package pages.waits; + +import java.time.Duration; + +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; + +public class WaitingStrategiesPage +{ + @FindBy(xpath = "//button[contains(text(),'Waiting Strategies')]") + private WebElement accordionItem; + + @FindBy(id = "disabled-input-toggle") + private WebElement enableOrDisableButton; + + @FindBy(id = "disabled-input") + private WebElement enabledOrDisabledInput; + + @FindBy(id = "hidden-input-toggle") + private WebElement showOrHideButton; + + @FindBy(id = "hidden-input") + private WebElement shownOrHiddenInput; + + @FindBy(id = "add-remove-input-button") + private WebElement addOrRemoveButton; + + @FindBy(id = "dynamic-input") + private WebElement dynamicInput; + + private WebDriver driver = null; + + public WaitingStrategiesPage(WebDriver driver) + { + this.driver = driver; + PageFactory.initElements(this.driver, this); + } + + public void clickOnAccordionItem() + { + accordionItem.click(); + } + + public void clickEnableOrDisableButton() + { + enableOrDisableButton.click(); + } + + public void setEnabledOrDisabledInput(String text) + { + enabledOrDisabledInput.sendKeys(text); + } + + public void clickShowOrHideButton() + { + showOrHideButton.click(); + } + + public void setShownOrHiddenInput(String text) + { + shownOrHiddenInput.sendKeys(text); + } + + public void clickAddOrRemoveButton() + { + addOrRemoveButton.click(); + } + + public void setDynamicInput(String text) + { + dynamicInput.sendKeys(text); + } + + public void waitForEnabledOrDisabledInputToBeClickable(long millis) + { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(millis)); + wait.until(ExpectedConditions.elementToBeClickable(enabledOrDisabledInput)); + } + + public void waitForShownOrHiddenInputToBeClickable(long millis) + { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(millis)); + wait.until(ExpectedConditions.elementToBeClickable(shownOrHiddenInput)); + } + + public void waitForDynamicInputToBePresent(long millis) + { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(millis)); + wait.until(ExpectedConditions.presenceOfElementLocated(By.id("dynamic-input"))); + } +} diff --git a/src/tests/waits/ExplicitWaitTests.java b/src/tests/waits/ExplicitWaitTests.java new file mode 100644 index 0000000..5e8e218 --- /dev/null +++ b/src/tests/waits/ExplicitWaitTests.java @@ -0,0 +1,61 @@ +package tests.waits; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +import pages.waits.WaitingStrategiesPage; + +public class ExplicitWaitTests +{ + 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: + WaitingStrategiesPage page = new WaitingStrategiesPage(driver); + + // Perform actions on the page: + page.clickOnAccordionItem(); + + // ======================================================================================== // + // + // DISABLED INPUT + // + // ======================================================================================== // + + page.clickEnableOrDisableButton(); + page.waitForEnabledOrDisabledInputToBeClickable(1000); // Wait for the JavaScript to enable the input. + page.setEnabledOrDisabledInput("Explicit Wait with disabled elements"); + + // ======================================================================================== // + // + // HIDDEN INPUT + // + // ======================================================================================== // + + page.clickShowOrHideButton(); + page.waitForShownOrHiddenInputToBeClickable(1000); // Wait for the JavaScript to show the input. + page.setShownOrHiddenInput("Explicit Wait with hidden elements"); + + // ======================================================================================== // + // + // DYNAMIC INPUT + // + // ======================================================================================== // + + page.clickAddOrRemoveButton(); + page.waitForDynamicInputToBePresent(1000); + page.setDynamicInput("Explicit Wait with dynamic elements"); + + // + + // This is commented out so you can actually see what happened in the web page: + // driver.quit(); + } +} diff --git a/src/waits/ImplicitWait.java b/src/tests/waits/ImplicitWaitTests.java similarity index 67% rename from src/waits/ImplicitWait.java rename to src/tests/waits/ImplicitWaitTests.java index c7a836d..5d7740b 100644 --- a/src/waits/ImplicitWait.java +++ b/src/tests/waits/ImplicitWaitTests.java @@ -1,12 +1,13 @@ -package waits; +package tests.waits; import java.time.Duration; -import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; -public class ImplicitWait +import pages.waits.WaitingStrategiesPage; + +public class ImplicitWaitTests { private static void sleep(long milliseconds) { @@ -25,14 +26,17 @@ public class ImplicitWait // Specify path to WebDriver: System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver"); - // Launch browser, set implicit waiting time, and navigate to test page: + // Launch browser and navigate to test page: WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000)); driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/"); - // Click on the Waiting Strategies accordion item: - driver.findElement(By.xpath("//button[contains(text(),'Waiting Strategies')]")).click(); + // Instantiate the page model: + WaitingStrategiesPage page = new WaitingStrategiesPage(driver); + + // Perform actions on the page: + page.clickOnAccordionItem(); // ======================================================================================== // // @@ -40,16 +44,14 @@ public class ImplicitWait // // ======================================================================================== // - // Click on the Enable button that will enable the disabled input: - driver.findElement(By.id("disabled-input-toggle")).click(); + page.clickEnableOrDisableButton(); // implicitlyWait is used when trying to find an element or elements that are not immediately available. // In this case, the disabled input is available (i.e. exists in the DOM), so we sleep the execution // before continuing: sleep(1000); - // Enter some text in the input: - driver.findElement(By.id("disabled-input")).sendKeys("Implicit Wait with disabled elements"); + page.setEnabledOrDisabledInput("Implicit Wait with disabled elements"); // ======================================================================================== // // @@ -57,16 +59,14 @@ public class ImplicitWait // // ======================================================================================== // - // Click on the Hide button that will show the hidden input: - driver.findElement(By.id("hidden-input-toggle")).click(); + page.clickShowOrHideButton(); // implicitlyWait is used when trying to find an element or elements that are not immediately available. // In this case, the hidden input is available (i.e. exists in the DOM), so we sleep the execution // before continuing: sleep(1000); - // Enter some text in the input: - driver.findElement(By.id("hidden-input")).sendKeys("Implicit Wait with hidden elements"); + page.setShownOrHiddenInput("Implicit Wait with hidden elements"); // ======================================================================================== // // @@ -74,14 +74,12 @@ public class ImplicitWait // // ======================================================================================== // - // Click on the Add button that will add a new input: - driver.findElement(By.id("add-remove-input-button")).click(); + page.clickAddOrRemoveButton(); // We don't need to sleep here, as the dynamic input doesn't exist in the DOM and implicitlyWait // will keep trying for the specified amount of time until the input is available. - // Enter some text in the input: - driver.findElement(By.id("dynamic-input")).sendKeys("Implicit Wait with dynamic elements"); + page.setDynamicInput("Implicit Wait with dynamic elements"); // diff --git a/src/waits/ExplicitWait.java b/src/waits/ExplicitWait.java deleted file mode 100644 index 7c9d26f..0000000 --- a/src/waits/ExplicitWait.java +++ /dev/null @@ -1,89 +0,0 @@ -package waits; - -import java.time.Duration; - -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.support.ui.ExpectedConditions; -import org.openqa.selenium.support.ui.WebDriverWait; - -public class ExplicitWait -{ - 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 Waiting Strategies accordion item: - driver.findElement(By.xpath("//button[contains(text(),'Waiting Strategies')]")).click(); - - // ======================================================================================== // - // - // DISABLED INPUT - // - // ======================================================================================== // - - // Get a reference to the disabled input (we can do that because the input is in the DOM): - WebElement disabledInput = driver.findElement(By.id("disabled-input")); - - // Click on the Enable button that will enable the disabled input: - driver.findElement(By.id("disabled-input-toggle")).click(); - - // Wait for the JavaScript to enable the input: - WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(1000)); - wait.until(ExpectedConditions.elementToBeClickable(disabledInput)); - - // Enter some text in the input: - disabledInput.sendKeys("Explicit Wait with disabled elements"); - - // ======================================================================================== // - // - // HIDDEN INPUT - // - // ======================================================================================== // - - // Get a reference to the hidden input (we can do that because the input is in the DOM): - WebElement hiddenInput = driver.findElement(By.id("hidden-input")); - - // Click on the Hide button that will show the hidden input: - driver.findElement(By.id("hidden-input-toggle")).click(); - - // Wait for the JavaScript to show the input: - wait = new WebDriverWait(driver, Duration.ofMillis(1000)); - wait.until(ExpectedConditions.visibilityOf(hiddenInput)); - - // Enter some text in the input: - hiddenInput.sendKeys("Explicit Wait with hidden elements"); - - // ======================================================================================== // - // - // DYNAMIC INPUT - // - // ======================================================================================== // - - // We cannot get a reference to the dynamic input because it doesn't exist in the DOM yet! - // WebElement dynamicInput = driver.findElement(By.id("dynamic-input")); - - // Click on the Add button that will add a new input: - driver.findElement(By.id("add-remove-input-button")).click(); - - // Wait for the JavaScript to add the input: - wait = new WebDriverWait(driver, Duration.ofMillis(1000)); - wait.until(ExpectedConditions.presenceOfElementLocated(By.id("dynamic-input"))); - - // Enter some text in the input: - driver.findElement(By.id("dynamic-input")).sendKeys("Explicit Wait with dynamic elements"); - - // - - // This is commented out so you can actually see what happened in the web page: - // driver.quit(); - } -}