Skip to content

Commit

Permalink
Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
egecansen committed Mar 5, 2024
1 parent 21cedbe commit ce0e33d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 4 deletions.
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
70 changes: 67 additions & 3 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 All @@ -24,7 +26,7 @@
import static pickleib.utilities.screenshot.ScreenCaptureUtility.captureScreen;

public class AppTest {
String testWebsiteUrl = "http://127.0.0.1:8080/";
String testWebsiteUrl = "http://127.0.0.1:8081/";
WebDriver driver;
WebInteractions webInteractions;
Printer log = new Printer(AppTest.class);
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;

}

0 comments on commit ce0e33d

Please sign in to comment.