Apply POM to WaitingStrategies

This commit is contained in:
Ramon Caballero 2024-07-25 21:06:31 +01:00
parent a41d058e0a
commit 9ef157f877
4 changed files with 173 additions and 107 deletions

View File

@ -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")));
}
}

View File

@ -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();
}
}

View File

@ -1,12 +1,13 @@
package waits; package tests.waits;
import java.time.Duration; import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
public class ImplicitWait import pages.waits.WaitingStrategiesPage;
public class ImplicitWaitTests
{ {
private static void sleep(long milliseconds) private static void sleep(long milliseconds)
{ {
@ -25,14 +26,17 @@ public class ImplicitWait
// Specify path to WebDriver: // Specify path to WebDriver:
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver"); 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(); WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize(); driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000)); driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));
driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/"); driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/");
// Click on the Waiting Strategies accordion item: // Instantiate the page model:
driver.findElement(By.xpath("//button[contains(text(),'Waiting Strategies')]")).click(); 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: page.clickEnableOrDisableButton();
driver.findElement(By.id("disabled-input-toggle")).click();
// implicitlyWait is used when trying to find an element or elements that are not immediately available. // 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 // In this case, the disabled input is available (i.e. exists in the DOM), so we sleep the execution
// before continuing: // before continuing:
sleep(1000); sleep(1000);
// Enter some text in the input: page.setEnabledOrDisabledInput("Implicit Wait with disabled elements");
driver.findElement(By.id("disabled-input")).sendKeys("Implicit Wait with disabled elements");
// ======================================================================================== // // ======================================================================================== //
// //
@ -57,16 +59,14 @@ public class ImplicitWait
// //
// ======================================================================================== // // ======================================================================================== //
// Click on the Hide button that will show the hidden input: page.clickShowOrHideButton();
driver.findElement(By.id("hidden-input-toggle")).click();
// implicitlyWait is used when trying to find an element or elements that are not immediately available. // 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 // In this case, the hidden input is available (i.e. exists in the DOM), so we sleep the execution
// before continuing: // before continuing:
sleep(1000); sleep(1000);
// Enter some text in the input: page.setShownOrHiddenInput("Implicit Wait with hidden elements");
driver.findElement(By.id("hidden-input")).sendKeys("Implicit Wait with hidden elements");
// ======================================================================================== // // ======================================================================================== //
// //
@ -74,14 +74,12 @@ public class ImplicitWait
// //
// ======================================================================================== // // ======================================================================================== //
// Click on the Add button that will add a new input: page.clickAddOrRemoveButton();
driver.findElement(By.id("add-remove-input-button")).click();
// We don't need to sleep here, as the dynamic input doesn't exist in the DOM and implicitlyWait // 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. // will keep trying for the specified amount of time until the input is available.
// Enter some text in the input: page.setDynamicInput("Implicit Wait with dynamic elements");
driver.findElement(By.id("dynamic-input")).sendKeys("Implicit Wait with dynamic elements");
// //

View File

@ -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();
}
}