diff --git a/src/pages/webelements/Frame.java b/src/pages/webelements/Frame.java new file mode 100644 index 0000000..eef4184 --- /dev/null +++ b/src/pages/webelements/Frame.java @@ -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(); + } +} diff --git a/src/pages/webelements/FramesAndWindowsPage.java b/src/pages/webelements/FramesAndWindowsPage.java new file mode 100644 index 0000000..2f3d7fb --- /dev/null +++ b/src/pages/webelements/FramesAndWindowsPage.java @@ -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(); + } +} diff --git a/src/pages/webelements/NewWindow.java b/src/pages/webelements/NewWindow.java new file mode 100644 index 0000000..544b6b5 --- /dev/null +++ b/src/pages/webelements/NewWindow.java @@ -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 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); + } +} diff --git a/src/tests/webelements/FramesAndWindowsTests.java b/src/tests/webelements/FramesAndWindowsTests.java new file mode 100644 index 0000000..2e84817 --- /dev/null +++ b/src/tests/webelements/FramesAndWindowsTests.java @@ -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(); + } +} diff --git a/src/webelements/FramesAndWindows.java b/src/webelements/FramesAndWindows.java deleted file mode 100644 index 2162a63..0000000 --- a/src/webelements/FramesAndWindows.java +++ /dev/null @@ -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 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(); - } -}