diff --git a/docker-compose.yml b/docker-compose.yml index 9687c35f..8b983402 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" diff --git a/src/main/java/pickleib/web/interactions/WebInteractions.java b/src/main/java/pickleib/web/interactions/WebInteractions.java index 52cc635a..30136ecc 100644 --- a/src/main/java/pickleib/web/interactions/WebInteractions.java +++ b/src/main/java/pickleib/web/interactions/WebInteractions.java @@ -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) @@ -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) @@ -850,7 +850,7 @@ public void bundleInteraction(List 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(), @@ -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 getBoundingClientRect() + * + * @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; + } } \ No newline at end of file diff --git a/src/main/java/pickleib/web/utilities/WebUtilities.java b/src/main/java/pickleib/web/utilities/WebUtilities.java index c8b5c875..6b0fea2c 100644 --- a/src/main/java/pickleib/web/utilities/WebUtilities.java +++ b/src/main/java/pickleib/web/utilities/WebUtilities.java @@ -651,8 +651,34 @@ public WebElement scrollInContainer(WebElement container, List 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 getBoundingClientRect() + * + * @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()); + } + } diff --git a/src/test/java/AppTest.java b/src/test/java/AppTest.java index 460b6dce..b350d7b8 100644 --- a/src/test/java/AppTest.java +++ b/src/test/java/AppTest.java @@ -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; @@ -13,6 +16,7 @@ import utils.Printer; import utils.StringUtilities; import utils.arrays.ArrayUtilities; + import java.util.List; import java.util.Map; @@ -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 categories = reflections.getElementsFromPage("categories", "homePage"); + WebElement interactions = ElementAcquisition.acquireNamedElementAmongst(categories, "Interactions"); + webInteractions.clickElement(interactions); + List 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 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 categories = pageObjectReflections.getElementsFromPage("clickMeButton", "pages.PageClass"); diff --git a/src/test/java/common/ObjectRepository.java b/src/test/java/common/ObjectRepository.java index f3f8568f..f38c9d70 100644 --- a/src/test/java/common/ObjectRepository.java +++ b/src/test/java/common/ObjectRepository.java @@ -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(); } diff --git a/src/test/java/pages/DropDownPage.java b/src/test/java/pages/DropDownPage.java new file mode 100644 index 00000000..e879332c --- /dev/null +++ b/src/test/java/pages/DropDownPage.java @@ -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 countriesList; + + @FindBy(css = "#countriesDropDown .vs__selected") + WebElement selection; +} diff --git a/src/test/java/pages/InteractionsPage.java b/src/test/java/pages/InteractionsPage.java new file mode 100644 index 00000000..7bac6d9a --- /dev/null +++ b/src/test/java/pages/InteractionsPage.java @@ -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 tools; +}