Apply POM to JavaScriptPopupMessages

This commit is contained in:
Ramon Caballero 2024-07-25 18:11:09 +01:00
parent cdd8d98acc
commit 0701dd75f0
6 changed files with 184 additions and 65 deletions

View File

@ -0,0 +1,27 @@
package pages.webelements;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
public class AlertPanel
{
private Alert alert = null;
private WebDriver driver = null;
public AlertPanel(WebDriver driver)
{
this.driver = driver;
alert = driver.switchTo().alert();
}
public String getText()
{
return alert.getText();
}
public void accept()
{
alert.accept();
driver.switchTo().defaultContent();
}
}

View File

@ -0,0 +1,27 @@
package pages.webelements;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
public class ConfirmPanel
{
private Alert confirm = null;
private WebDriver driver = null;
public ConfirmPanel(WebDriver driver)
{
this.driver = driver;
confirm = driver.switchTo().alert();
}
public String getText()
{
return confirm.getText();
}
public void dismiss()
{
confirm.dismiss();
driver.switchTo().defaultContent();
}
}

View File

@ -0,0 +1,49 @@
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 JavaScriptPopupMessagesPage
{
@FindBy(xpath = "//button[contains(text(),'JavaScript Popup Messages')]")
private WebElement accordionItem;
@FindBy(id = "alert-button")
private WebElement alertButton;
@FindBy(id = "confirm-button")
private WebElement confirmButton;
@FindBy(id = "prompt-button")
private WebElement promptButton;
private WebDriver driver;
public JavaScriptPopupMessagesPage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(this.driver, this);
}
public void clickOnAccordionItem()
{
accordionItem.click();
}
public void clickAlertButton()
{
alertButton.click();
}
public void clickConfirmButton()
{
confirmButton.click();
}
public void clickPromptButton()
{
promptButton.click();
}
}

View File

@ -0,0 +1,32 @@
package pages.webelements;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
public class PromptPanel
{
private Alert prompt = null;
private WebDriver driver = null;
public PromptPanel(WebDriver driver)
{
this.driver = driver;
prompt = driver.switchTo().alert();
}
public void setText(String text)
{
prompt.sendKeys(text);
}
public String getText()
{
return prompt.getText();
}
public void dismiss()
{
prompt.dismiss();
driver.switchTo().defaultContent();
}
}

View File

@ -0,0 +1,49 @@
package tests.webelements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import pages.webelements.AlertPanel;
import pages.webelements.ConfirmPanel;
import pages.webelements.JavaScriptPopupMessagesPage;
import pages.webelements.PromptPanel;
public class JavaScriptPopupMessagesTests
{
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:
JavaScriptPopupMessagesPage page = new JavaScriptPopupMessagesPage(driver);
// Perform actions on the page:
page.clickOnAccordionItem();
page.clickAlertButton();
AlertPanel alert = new AlertPanel(driver);
System.out.println("Alert text: " + alert.getText());
alert.accept();
page.clickConfirmButton();
ConfirmPanel confirm = new ConfirmPanel(driver);
System.out.println("Confirm text: " + confirm.getText());
confirm.dismiss();
page.clickPromptButton();
PromptPanel prompt = new PromptPanel(driver);
System.out.println("Prompt text: " + prompt.getText());
prompt.dismiss();
//
// This is commented out so you can actually see what happened in the web page:
// driver.quit();
}
}

View File

@ -1,65 +0,0 @@
package webelements;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JavaScriptPopupMessages
{
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 'JavaScript Popup Messages' accordion item:
driver.findElement(By.xpath("//button[contains(text(),'JavaScript Popup Messages')]")).click();
//
// ALERT
//
driver.findElement(By.id("alert-button")).click();
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept();
System.out.println("Alert text: " + alertText);
//
// CONFIRM
//
driver.findElement(By.id("confirm-button")).click();
Alert confirm = driver.switchTo().alert();
String confirmText = confirm.getText();
confirm.dismiss();
System.out.println("Confirm text: " + confirmText);
//
// PROMPT
//
driver.findElement(By.id("prompt-button")).click();
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("QA");
String promptText = prompt.getText();
prompt.dismiss();
System.out.println("Prompt text: " + promptText);
//
// This is commented out so you can actually see what happened in the web page:
// driver.quit();
}
}