Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Tests for Alerts & Windows Page #112

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import collections.Bundle;
import collections.Pair;
import utils.Printer;
import utils.StringUtilities;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

Expand Down Expand Up @@ -894,6 +896,7 @@ protected ObjectRepository getObjectRepository(){
* @return returns the element
*/
public WebElement getElementFromPage(String elementFieldName, String pageName){
pageName = StringUtilities.firstLetterDeCapped(pageName);
Map<String, Object> pageFields;
Object pageObject = getFields(getObjectRepository()).get(pageName);
if (pageObject != null) pageFields = getFields(pageObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,6 @@ public void toPage(String page) {
* Switch to the next tab
*/
public void switchToNextTab() {
log.info("Switching to the next tab ... ");
String parentHandle = super.switchWindowByHandle(null);
ContextStore.put("parentHandle", parentHandle);
}
Expand Down
68 changes: 66 additions & 2 deletions src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import pages.ElementsPage;
import pages.FormsPage;
import pickleib.enums.Direction;
import pickleib.enums.ElementState;
import pickleib.utilities.element.acquisition.ElementAcquisition;
import pickleib.web.driver.PickleibWebDriver;
import pickleib.web.driver.WebDriverFactory;
Expand Down Expand Up @@ -194,8 +196,8 @@ public void scrollInDirectionTest(){
}

@Test
public void centerElementTest(){
ElementAcquisition.Reflections< ObjectRepository > reflections = new ElementAcquisition.Reflections<>(ObjectRepository.class);
public void centerElementTest() {
ElementAcquisition.Reflections<ObjectRepository> reflections = new ElementAcquisition.Reflections<>(ObjectRepository.class);
List<WebElement> categories = reflections.getElementsFromPage("categories", "homePage");
WebElement interactions = ElementAcquisition.acquireNamedElementAmongst(categories, "Interactions");
webInteractions.clickElement(interactions);
Expand All @@ -208,6 +210,68 @@ public void centerElementTest(){
Assert.assertTrue("Logo is not in view!", webInteractions.elementIsInView(logo));
}

@Test
public void openNewTabTest(){
ElementAcquisition.Reflections< ObjectRepository > reflections = new ElementAcquisition.Reflections<>(ObjectRepository.class);
List<WebElement> categories = reflections.getElementsFromPage("categories", "homePage");
WebElement alertsAndWindows = ElementAcquisition.acquireNamedElementAmongst(categories, "Alerts, Frame & Windows");
webInteractions.clickElement(alertsAndWindows);
List<WebElement> buttons = reflections.getElementsFromPage("buttons", "AlertAndWindowsPage");
WebElement newTabButton = ElementAcquisition.acquireNamedElementAmongst(buttons, "New Tab");
webInteractions.clickElement(newTabButton);
webInteractions.switchToNextTab();
webInteractions.verifyCurrentUrl(testWebsiteUrl);
log.success("openNewTabTest() pass!");
}

@Test
public void openNewWindowTest(){
ElementAcquisition.Reflections< ObjectRepository > reflections = new ElementAcquisition.Reflections<>(ObjectRepository.class);
List<WebElement> categories = reflections.getElementsFromPage("categories", "homePage");
WebElement alertsAndWindows = ElementAcquisition.acquireNamedElementAmongst(categories, "Alerts, Frame & Windows");
webInteractions.clickElement(alertsAndWindows);
List<WebElement> buttons = reflections.getElementsFromPage("buttons", "AlertAndWindowsPage");
WebElement newWindowButton = ElementAcquisition.acquireNamedElementAmongst(buttons, "New Window");
webInteractions.clickElement(newWindowButton);
webInteractions.switchWindowByIndex(1);
webInteractions.verifyCurrentUrl(testWebsiteUrl);
log.success("openNewWindowTest() pass!");
}

@Test
public void dismissAlertTest(){
ElementAcquisition.Reflections< ObjectRepository > reflections = new ElementAcquisition.Reflections<>(ObjectRepository.class);
List<WebElement> categories = reflections.getElementsFromPage("categories", "homePage");
WebElement alertsAndWindows = ElementAcquisition.acquireNamedElementAmongst(categories, "Alerts, Frame & Windows");
webInteractions.clickElement(alertsAndWindows);
List<WebElement> buttons = reflections.getElementsFromPage("buttons", "AlertAndWindowsPage");
WebElement newWindowsWithMessageButton = ElementAcquisition.acquireNamedElementAmongst(buttons, "New Window Message");
webInteractions.clickElement(newWindowsWithMessageButton);
webInteractions.getAlert().getText().equals("New window message!");
webInteractions.getAlert().dismiss();
webInteractions.switchToNextTab();
webInteractions.verifyCurrentUrl(testWebsiteUrl + "alerts");
log.success("dismissAlertTest() pass!");
}

@Test
public void acceptAlertTest(){
ElementAcquisition.Reflections< ObjectRepository > reflections = new ElementAcquisition.Reflections<>(ObjectRepository.class);
List<WebElement> categories = reflections.getElementsFromPage("categories", "homePage");
WebElement alertsAndWindows = ElementAcquisition.acquireNamedElementAmongst(categories, "Alerts, Frame & Windows");
webInteractions.clickElement(alertsAndWindows);
List<WebElement> buttons = reflections.getElementsFromPage("buttons", "AlertAndWindowsPage");
WebElement newWindowsWithMessageButton = ElementAcquisition.acquireNamedElementAmongst(buttons, "New Window Message");
webInteractions.clickElement(newWindowsWithMessageButton);
webInteractions.getAlert().getText().equals("New window message!");
webInteractions.getAlert().accept();
webInteractions.waitUntilPageLoads(5);
webInteractions.switchToNextTab();
webInteractions.verifyCurrentUrl(testWebsiteUrl);
log.success("acceptAlertTest() pass!");
}


// @Test
// public void clickTest() {
// List<WebElement> categories = pageObjectReflections.getElementsFromPage("clickMeButton", "pages.PageClass");
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/common/ObjectRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
import pickleib.utilities.interfaces.repository.PageRepository;

public class ObjectRepository implements PageRepository {

HomePage homePage = new HomePage();
ElementsPage elementsPage = new ElementsPage();
FormsPage formsPage = new FormsPage();
InteractionsPage interactionsPage = new InteractionsPage();
DropDownPage dropDownPage = new DropDownPage();
TallPage tallPage = new TallPage();
AlertAndWindowsPage alertAndWindowsPage = new AlertAndWindowsPage();

}
14 changes: 14 additions & 0 deletions src/test/java/pages/AlertAndWindowsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import pickleib.web.PickleibPageObject;

import java.util.List;

public class AlertAndWindowsPage extends PickleibPageObject {

@FindBy(css = ".button")
public List<WebElement> buttons;

}
20 changes: 20 additions & 0 deletions src/test/java/pages/ElementsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import pickleib.web.PickleibPageObject;

import java.util.List;

public class ElementsPage extends PickleibPageObject {

@FindBy(css = ".question-box h2")
public WebElement questionBoxTitle;

@FindBy(name = ".question-box form p")
public WebElement checkMarkMessage;

@FindBy(css = ".radio-group label")
public List<WebElement> checkMarks;

}