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

ScrollInContainerTest #108

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.8'

services:
vue-test-website:
image: umutayb/vue-test-site:latest
image: umutayb/vue-test-site:0.0.3
restart: always
ports:
- "8080:8080"
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/pickleib/web/interactions/WebInteractions.java
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ public void fillInputWithFile(WebElement inputElement, String inputName, String
public void fillInputElement(WebElement inputElement, String elementName, String pageName, String inputText, boolean scroll, boolean clear, boolean verify) {
log.info("Filling " +
highlighted(BLUE, elementName) +
highlighted(GRAY," on the ") +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName) +
highlighted(GRAY, " with the text: ") +
highlighted(BLUE, inputText)
Expand All @@ -796,7 +796,7 @@ public void fillInputElement(WebElement inputElement, String elementName, String
public void fillInputElement(WebElement inputElement, String elementName, String pageName, String inputText, boolean clear, boolean verify) {
log.info("Filling " +
highlighted(BLUE, elementName) +
highlighted(GRAY," on the ") +
highlighted(GRAY, " on the ") +
highlighted(BLUE, pageName) +
highlighted(GRAY, " with the text: ") +
highlighted(BLUE, inputText)
Expand Down Expand Up @@ -850,7 +850,7 @@ public void bundleInteraction(List<Bundle<String, WebElement, Map<String, String
switch (interactionType) {
case click -> clickElement(bundle.beta(), bundle.alpha(), pageName, scroll);
case fill ->
clearFillInput(bundle.beta(), bundle.alpha(), pageName, bundle.theta().get("Input"), scroll,false);
clearFillInput(bundle.beta(), bundle.alpha(), pageName, bundle.theta().get("Input"), scroll, false);
case center -> centerElement(bundle.beta(), bundle.alpha(), pageName);
case verify -> verifyElementContainsAttribute(
bundle.beta(),
Expand Down Expand Up @@ -1231,4 +1231,23 @@ public void uploadFile(@NotNull WebElement fileUploadInput, String elementName,
);
super.uploadFile(fileUploadInput, directory, fileName);
}

/**
* Checks if the specified WebElement is fully in view within the current browser window.
* * The script calculates the element's bounding rectangle and checks if its
* * top, left, bottom, and right coordinates are within the viewport.
* * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect">getBoundingClientRect()</a>
*
* @param element The WebElement to be checked for visibility.
* @return {@code true} if the element is fully in view, {@code false} otherwise.
* @throws org.openqa.selenium.JavascriptException If a JavaScript error occurs during the execution of the script.
* @throws java.lang.ClassCastException If the WebDriver is not able to execute JavaScript.
* @throws java.lang.NullPointerException If the provided WebElement is null.
* @since 2.0.0
*/
public boolean elementIsInView(WebElement element) {
boolean isElementInView = super.elementIsInView(element);
log.info("Element is in view ? " + highlighted(BLUE, String.valueOf(isElementInView)));
return isElementInView;
}
}
28 changes: 27 additions & 1 deletion src/main/java/pickleib/web/utilities/WebUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,34 @@ public WebElement scrollInContainer(WebElement container, List<WebElement> eleme
log.info("Scrolling " + targetElementText + " in view");
WebElement targetElement = ElementAcquisition.acquireNamedElementAmongst(elements, targetElementText);
WebElement firstElement = elements.get(0);
double distance = firstElement.getLocation().getY() - targetElement.getLocation().getY();
double distance = Math.abs(firstElement.getLocation().getY() - targetElement.getLocation().getY());
((JavascriptExecutor) driver).executeScript("arguments[0].scrollBy(0, "+distance+");", container);
return targetElement;
}

/**
* Checks if the specified WebElement is fully in view within the current browser window.
* * The script calculates the element's bounding rectangle and checks if its
* * top, left, bottom, and right coordinates are within the viewport.
* * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect">getBoundingClientRect()</a>
*
* @param element The WebElement to be checked for visibility.
* @return {@code true} if the element is fully in view, {@code false} otherwise.
* @throws org.openqa.selenium.JavascriptException If a JavaScript error occurs during the execution of the script.
* @throws java.lang.ClassCastException If the WebDriver is not able to execute JavaScript.
* @throws java.lang.NullPointerException If the provided WebElement is null.
* @since 2.0.0
*/
public boolean elementIsInView(WebElement element) {
String script = "var rect = arguments[0].getBoundingClientRect();" +
" return (" +
" rect.top >= 0 &&" +
" rect.left >= 0 &&" +
" rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&\n" +
" rect.right <= (window.innerWidth || document.documentElement.clientWidth)\n" +
" );";

return Boolean.parseBoolean(((JavascriptExecutor) driver).executeScript(script, element).toString());
}

}
27 changes: 26 additions & 1 deletion src/test/java/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import common.ObjectRepository;
import context.ContextStore;
import org.junit.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand All @@ -13,6 +16,7 @@
import utils.Printer;
import utils.StringUtilities;
import utils.arrays.ArrayUtilities;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -152,6 +156,27 @@ public void completeFormSubmissionTest(){//TODO: Try soft assertions
log.success("The completeFormSubmissionTest() passed!");
}

@Test
public void scrollInContainerTest(){//TODO: Try soft assertions
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);
List<WebElement> dropDown = reflections.getElementsFromPage("tools", "interactionsPage");
WebElement selectable = ElementAcquisition.acquireNamedElementAmongst(dropDown, "DropDown");
webInteractions.clickElement(selectable);
WebElement countriesDropDown = reflections.getElementFromPage("countriesDropDown", "dropDownPage");
webInteractions.clickElement(countriesDropDown);
WebElement countriesContainer = reflections.getElementFromPage("countriesContainer", "dropDownPage");
List<WebElement> countriesList = reflections.getElementsFromPage("countriesList", "dropDownPage");
String countrySelection = "Ukraine";
WebElement preSelection = ElementAcquisition.acquireNamedElementAmongst(countriesList, countrySelection);
Assert.assertFalse("Selected country is already in view!!", webInteractions.elementIsInView(preSelection));
WebElement country = webInteractions.scrollInContainer(countriesContainer, countriesList, countrySelection);
Assert.assertTrue("Selected country is not in view!!", webInteractions.elementIsInView(country));
log.success("scrollInContainerTest() 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 @@ -2,9 +2,13 @@

import pages.FormsPage;
import pages.HomePage;
import pages.InteractionsPage;
import pages.DropDownPage;
import pickleib.utilities.interfaces.repository.PageRepository;

public class ObjectRepository implements PageRepository {
HomePage homePage = new HomePage();
FormsPage formsPage = new FormsPage();
InteractionsPage interactionsPage = new InteractionsPage();
DropDownPage dropDownPage = new DropDownPage();
}
25 changes: 25 additions & 0 deletions src/test/java/pages/DropDownPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pages;

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

import java.util.List;

public class DropDownPage extends PickleibPageObject {

@FindBy(id = "title")
WebElement title;

@FindBy(css = "#countriesDropDown")
WebElement countriesDropDown;

@FindBy(css = "[role='listbox']")
WebElement countriesContainer;

@FindBy(css = "#vs1__listbox .vs__dropdown-option")
List<WebElement> countriesList;

@FindBy(css = "#countriesDropDown .vs__selected")
WebElement selection;
}
16 changes: 16 additions & 0 deletions src/test/java/pages/InteractionsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pages;

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

import java.util.List;

public class InteractionsPage extends PickleibPageObject {

@FindBy(id = "title")
WebElement title;

@FindBy(css = "tools a")
List<WebElement> tools;
}