Apply POM to FramesAndWindows
This commit is contained in:
parent
0701dd75f0
commit
a41d058e0a
|
|
@ -0,0 +1,31 @@
|
|||
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 Frame
|
||||
{
|
||||
@FindBy(id = "frame-button")
|
||||
private WebElement button;
|
||||
|
||||
private WebDriver driver = null;
|
||||
|
||||
public Frame(WebDriver driver, WebElement frame)
|
||||
{
|
||||
this.driver = driver;
|
||||
PageFactory.initElements(this.driver, this);
|
||||
driver.switchTo().frame(frame);
|
||||
}
|
||||
|
||||
public void clickButton()
|
||||
{
|
||||
button.click();
|
||||
}
|
||||
|
||||
public void dismiss()
|
||||
{
|
||||
driver.switchTo().defaultContent();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
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 FramesAndWindowsPage
|
||||
{
|
||||
@FindBy(xpath = "//button[contains(text(),'Frames and Windows')]")
|
||||
private WebElement accordionItem;
|
||||
|
||||
@FindBy(id = "custom-iframe")
|
||||
private WebElement frame;
|
||||
|
||||
@FindBy(id = "new-window-button")
|
||||
private WebElement newWindowButton;
|
||||
|
||||
private WebDriver driver = null;
|
||||
|
||||
public FramesAndWindowsPage(WebDriver driver)
|
||||
{
|
||||
this.driver = driver;
|
||||
PageFactory.initElements(this.driver, this);
|
||||
}
|
||||
|
||||
public void clickOnAccordionItem()
|
||||
{
|
||||
accordionItem.click();
|
||||
}
|
||||
|
||||
public WebElement frame()
|
||||
{
|
||||
return frame;
|
||||
}
|
||||
|
||||
public void clickNewWindowButton()
|
||||
{
|
||||
newWindowButton.click();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package pages.webelements;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
|
||||
public class NewWindow
|
||||
{
|
||||
private static void sleep(long milliseconds)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(milliseconds);
|
||||
} catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private String originalWindowHandle;
|
||||
|
||||
private WebDriver driver = null;
|
||||
|
||||
public NewWindow(WebDriver driver)
|
||||
{
|
||||
this.driver = driver;
|
||||
|
||||
// Get the handle (or identifier) of the original window:
|
||||
originalWindowHandle = driver.getWindowHandle();
|
||||
|
||||
// Most likely the new window won't finish loading its content before its title is queried by the
|
||||
// driver, so we sleep the execution a little bit to give it enough time:
|
||||
sleep(500);
|
||||
|
||||
// Get the handles of all the windows (i.e. handle of the original window + handle of the new window):
|
||||
Set<String> windowHandles = driver.getWindowHandles();
|
||||
|
||||
// Figure out the handle of the new window:
|
||||
String newWindowHandle = "";
|
||||
|
||||
for (String windowHandle : windowHandles)
|
||||
{
|
||||
if (!originalWindowHandle.contentEquals(windowHandle))
|
||||
{
|
||||
newWindowHandle = windowHandle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Switch to the new window:
|
||||
driver.switchTo().window(newWindowHandle);
|
||||
}
|
||||
|
||||
public String title()
|
||||
{
|
||||
return driver.getTitle();
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
driver.close();
|
||||
driver.switchTo().window(originalWindowHandle);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package tests.webelements;
|
||||
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||
|
||||
import pages.webelements.Frame;
|
||||
import pages.webelements.FramesAndWindowsPage;
|
||||
import pages.webelements.NewWindow;
|
||||
|
||||
public class FramesAndWindowsTests
|
||||
{
|
||||
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:
|
||||
FramesAndWindowsPage page = new FramesAndWindowsPage(driver);
|
||||
|
||||
// Perform actions on the page:
|
||||
page.clickOnAccordionItem();
|
||||
|
||||
Frame frame = new Frame(driver, page.frame());
|
||||
frame.clickButton();
|
||||
frame.dismiss();
|
||||
|
||||
page.clickNewWindowButton();
|
||||
NewWindow window = new NewWindow(driver);
|
||||
System.out.println("The title of the new window is: " + window.title());
|
||||
window.close();
|
||||
|
||||
//
|
||||
|
||||
// This is commented out so you can actually see what happened in the web page:
|
||||
// driver.quit();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
package webelements;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.openqa.selenium.By;
|
||||
import org.openqa.selenium.WebDriver;
|
||||
import org.openqa.selenium.WebElement;
|
||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||
|
||||
public class FramesAndWindows
|
||||
{
|
||||
private static void sleep(long milliseconds)
|
||||
{
|
||||
try
|
||||
{
|
||||
Thread.sleep(milliseconds);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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 'Frames and Windows' accordion item:
|
||||
driver.findElement(By.xpath("//button[contains(text(),'Frames and Windows')]")).click();
|
||||
|
||||
//
|
||||
// FRAMES
|
||||
//
|
||||
|
||||
// Switch to the iframe:
|
||||
WebElement frame = driver.findElement(By.id("custom-iframe"));
|
||||
driver.switchTo().frame(frame);
|
||||
|
||||
// Perform the actions we need to do:
|
||||
driver.findElement(By.id("frame-button")).click();
|
||||
|
||||
// Switch back to the default document:
|
||||
driver.switchTo().defaultContent();
|
||||
|
||||
//
|
||||
// WINDOWS
|
||||
//
|
||||
|
||||
// Get the handle (or identifier) of the original window:
|
||||
String originalWindowHandle = driver.getWindowHandle();
|
||||
|
||||
// Click the button that opens a new window:
|
||||
driver.findElement(By.id("new-window-button")).click();
|
||||
|
||||
// Most likely the new window won't finish loading its content before its title is queried by the
|
||||
// driver, so we sleep the execution a little bit to give it enough time:
|
||||
sleep(500);
|
||||
|
||||
// Get the handles of all the windows (i.e. handle of the original window + handle of the new window):
|
||||
Set<String> windowHandles = driver.getWindowHandles();
|
||||
|
||||
// Figure out the handle of the new window:
|
||||
String newWindowHandle = "";
|
||||
|
||||
for (String windowHandle : windowHandles)
|
||||
{
|
||||
if (!originalWindowHandle.contentEquals(windowHandle))
|
||||
{
|
||||
newWindowHandle = windowHandle;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Switch to the new window:
|
||||
driver.switchTo().window(newWindowHandle);
|
||||
|
||||
// Perform the actions we need to do:
|
||||
System.out.println("The title of the new window is: " + driver.getTitle());
|
||||
|
||||
// Close the window:
|
||||
driver.close();
|
||||
|
||||
// Switch back to the default document:
|
||||
driver.switchTo().window(originalWindowHandle);
|
||||
|
||||
//
|
||||
|
||||
// This is commented out so you can actually see what happened in the web page:
|
||||
// driver.quit();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue