Apply POM to MostCommonInputs

This commit is contained in:
Ramon Caballero 2024-07-25 15:27:50 +01:00
parent c3bb40338e
commit 7d4096225a
4 changed files with 178 additions and 79 deletions

View File

@ -0,0 +1,26 @@
package pages.webelements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class ModalDialogPage
{
@FindBy(how = How.ID, using = "form-interactions-modal-close-button")
private WebElement closeButton;
private WebDriver driver = null;
public ModalDialogPage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(this.driver, this);
}
public void close()
{
closeButton.click();
}
}

View File

@ -0,0 +1,98 @@
package pages.webelements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class MostCommonInputsPage
{
@FindBy(xpath = "//button[contains(text(),'Most Common Inputs')]")
private WebElement accordionItem;
@FindBy(id = "text-input")
private WebElement textInput;
@FindBy(id = "readonly-input")
private WebElement readonlyInput;
@FindBy(id = "email-input")
private WebElement emailInput;
@FindBy(id = "password-input")
private WebElement passwordInput;
@FindBy(id = "textarea")
private WebElement textarea;
@FindBy(id = "form-interactions-reset-button")
private WebElement resetButton;
@FindBy(id = "form-interactions-submit-button")
private WebElement submitButton;
private WebDriver driver = null;
public MostCommonInputsPage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(this.driver, this);
}
public void clickOnAccordionItem()
{
accordionItem.click();
}
public void setText(String text)
{
textInput.sendKeys(text);
}
public void setEmail(String text)
{
emailInput.sendKeys(text);
}
public void setPassword(String text)
{
passwordInput.sendKeys(text);
}
public void setTextarea(String text)
{
textarea.sendKeys(text);
}
public void clearText()
{
textInput.clear();
}
public void reset()
{
resetButton.click();
}
public void submit()
{
submitButton.click();
}
public String getReadonlyValue()
{
return readonlyInput.getAttribute("value");
}
public void sleep(long millis)
{
try
{
Thread.sleep(millis);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,54 @@
package tests.webelements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import pages.webelements.ModalDialogPage;
import pages.webelements.MostCommonInputsPage;
public class MostCommonInputsTests
{
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:
MostCommonInputsPage page = new MostCommonInputsPage(driver);
// Perform actions on the page:
page.clickOnAccordionItem();
page.setText("Selenium WebDriver");
page.setEmail("hello@ramoncaballero.dev");
page.setPassword("YouDontNeedToKnowThis");
page.setTextarea("This is a textarea and I can write inside it.");
page.clearText();
page.setText("Interacting with WebElements");
page.reset();
page.setText("Interacting with WebElements with Selenium WebDriver");
page.setEmail("hello@example.com");
page.setPassword("YouMustNotKnowThis");
page.setTextarea("Let's try to write something again.");
page.submit();
page.sleep(500);
ModalDialogPage modal = new ModalDialogPage(driver);
modal.close();
System.out.println("The read-only input text says '" + page.getReadonlyValue() + "'");
//
// This is commented out so you can actually see what happened in the web page:
// driver.quit();
}
}

View File

@ -1,79 +0,0 @@
package webelements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MostCommonInputs
{
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 'Most Common Inputs' accordion item:
driver.findElement(By.xpath("//button[contains(text(),'Most Common Inputs')]")).click();
// Text input:
WebElement textInput = driver.findElement(By.id("text-input"));
textInput.sendKeys("Selenium WebDriver");
// Read-only input:
WebElement readonlyInput = driver.findElement(By.id("readonly-input"));
String readonlyText = readonlyInput.getAttribute("value");
System.out.println("The read-only input text says '" + readonlyText + "'");
// Email input:
WebElement emailInput = driver.findElement(By.id("email-input"));
emailInput.sendKeys("hello@ramoncaballero.dev");
// Password input:
WebElement passwordInput = driver.findElement(By.id("password-input"));
passwordInput.sendKeys("YouDontNeedToKnowThis");
// Textarea:
WebElement textarea = driver.findElement(By.id("textarea"));
textarea.sendKeys("This is a textarea and I can write inside it.");
// Clear text input and write something else:
textInput.clear();
textInput.sendKeys("Interacting with WebElements");
// Click Reset button:
driver.findElement(By.id("form-interactions-reset-button")).click();
// Write again on the fields (no need to find the elements as we already have them!):
textInput.sendKeys("Interacting with WebElements with Selenium WebDriver");
emailInput.sendKeys("hello@example.com");
passwordInput.sendKeys("YouMustNotKnowThis");
textarea.sendKeys("Let's try to write something again.");
// Click Submit button:
WebElement submitButton = driver.findElement(By.id("form-interactions-submit-button"));
submitButton.click();
// The modal dialog might take a few milliseconds to appear, so let's wait in a simple way:
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
// Close the modal dialog:
driver.findElement(By.id("form-interactions-modal-close-button")).click();
//
// This is commented out so you can actually see what happened in the web page:
// driver.quit();
}
}