From be4ca55fad6ff114190207b4c1241477f5613a73 Mon Sep 17 00:00:00 2001 From: Ekaterina Semenova1 Date: Tue, 2 Jul 2024 17:33:28 +0400 Subject: [PATCH] #4926: Tests refactoring: Paginator element. PaginatorSelector.java --- .../elements/complex/PaginatorTests.java | 13 +-- .../angular/asserts/PaginatorAssert.java | 43 +-------- .../angular/elements/complex/Paginator.java | 96 +++++-------------- .../elements/complex/PaginatorSelector.java | 19 ++++ 4 files changed, 55 insertions(+), 116 deletions(-) create mode 100644 jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/PaginatorSelector.java diff --git a/jdi-light-angular-tests/src/test/java/io/github/epam/angular/tests/elements/complex/PaginatorTests.java b/jdi-light-angular-tests/src/test/java/io/github/epam/angular/tests/elements/complex/PaginatorTests.java index 2f8833f7d0..122752da37 100644 --- a/jdi-light-angular-tests/src/test/java/io/github/epam/angular/tests/elements/complex/PaginatorTests.java +++ b/jdi-light-angular-tests/src/test/java/io/github/epam/angular/tests/elements/complex/PaginatorTests.java @@ -23,10 +23,11 @@ import static io.github.com.pages.PaginatorPage.paginatorDisabledOption; import static io.github.com.pages.PaginatorPage.paginatorFirstLastButtons; import static io.github.com.pages.PaginatorPage.paginatorHideSizeOption; +import static java.lang.Integer.parseInt; import static java.lang.String.format; public class PaginatorTests extends TestsInit { - private static final List PAGE_SIZE_OPTIONS = Arrays.asList(1, 5, 10, 25, 100, 500); + private static final List PAGE_SIZE_OPTIONS = Arrays.asList("1", "5", "10", "25", "100", "500"); private static final String OPTIONS = PAGE_SIZE_OPTIONS .stream() @@ -52,7 +53,7 @@ public void labelPaginationTest() { @Test(description = "The test checks length and pageIndex for paginator") public void basicPaginatorTest() { waitCondition(() -> listLengthInput.isVisible()); - listLengthInput.setValue(String.valueOf(LENGTH)); + listLengthInput.setValue( String.valueOf(LENGTH) ); paginatorConfigurable.selectItemPerPageOption(STEP); //First page paginatorConfigurable.has().pageIndexCurrent(0) @@ -149,10 +150,10 @@ public void itemPerPagePaginatorTest() { pageSizeOptionsInput.setValue(OPTIONS); listLengthInput.setValue(String.valueOf(LENGTH)); - for (Integer option : PAGE_SIZE_OPTIONS) { - paginatorConfigurable.selectItemPerPageOption(option); - final String rangeLabel = format(RANGE_PATTERN, 1, Math.min(option, LENGTH), LENGTH); - paginatorConfigurable.has().itemsPerPageSelected(option) + for (String option : PAGE_SIZE_OPTIONS) { + paginatorConfigurable.selectItemPerPageOption(parseInt(option)); + final String rangeLabel = format(RANGE_PATTERN, 1, Math.min(parseInt(option), LENGTH), LENGTH); + paginatorConfigurable.has().itemsPerPageSelected(parseInt(option)) .and().has().rangeLabel(rangeLabel); } } diff --git a/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/asserts/PaginatorAssert.java b/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/asserts/PaginatorAssert.java index 61b2fc5cff..6d4cd824c0 100644 --- a/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/asserts/PaginatorAssert.java +++ b/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/asserts/PaginatorAssert.java @@ -25,8 +25,9 @@ public PaginatorAssert itemsPerPageSelected(final int number) { } @JDIAction(value = "Assert that '{0}' options for '{name}'", isAssert = true) - public PaginatorAssert itemsPerPageList(final List options) { - jdiAssert(element().options(), Matchers.equalTo(options)); + public PaginatorAssert itemsPerPageList(final List options) { + element().ItemPerPageSelect.expand(); + jdiAssert(element().ItemPerPageSelect.list().values(), Matchers.equalTo(options)); return this; } @@ -37,42 +38,6 @@ public PaginatorAssert rangeLabel(final String label) { return this; } -// @JDIAction(value = "Assert that '{name}' has '{0}' color theme", isAssert = true) -// public PaginatorAssert colorTheme(final AngularColors value) { -// jdiAssert(AngularColors.fromType(element().colorTheme()), Matchers.equalTo(value)); -// return this; -// } - -// @JDIAction(value = "Assert that '{name}' has '{0}' color theme", isAssert = true) -// public PaginatorAssert colorTheme(final String value) { -// jdiAssert(element().colorTheme(), Matchers.equalToIgnoringCase(value)); -// return this; -// } - -// @JDIAction(value = "Assert that '{name}' has '{0}' color of the boarder", isAssert = true) -// public PaginatorAssert borderColor(final AngularColors value) { -// jdiAssert(AngularColors.fromType(element().boarderColor()), Matchers.equalTo(value)); -// return this; -// } - -// @JDIAction(value = "Assert that '{name}' has '{0}' color of the boarder", isAssert = true) -// public PaginatorAssert borderColor(final String value) { -// jdiAssert(element().boarderColor(), Matchers.equalTo(value)); -// return this; -// } - - @JDIAction(value = "Assert that '{name}' has '{0}' color of the selected option", isAssert = true) - public PaginatorAssert selectedOptionColor(final AngularColors value) { - jdiAssert((element().selectedOptionColor()), Matchers.equalTo(value)); - return this; - } - - @JDIAction(value = "Assert that '{name}' has '{0}' color of the selected option", isAssert = true) - public PaginatorAssert selectedOptionColor(final String value) { - jdiAssert(element().selectedOptionColor(), Matchers.equalTo(value)); - return this; - } - @JDIAction(value = "Assert that '{name} has firstPageLabel='{0}'", isAssert = true) public PaginatorAssert firstPageLabel(final String label) { jdiAssert(element().firstPageLabel(), Matchers.equalTo(label)); @@ -145,7 +110,7 @@ public PaginatorAssert nextPageButtonDisabled() { @JDIAction(value = "Assert that item per page selector is disabled for '{name}'", isAssert = true) public PaginatorAssert itemPerPageSelectorDisabled() { - jdiAssert(element().itemPerPageSelector().attr("aria-disabled"), Matchers.equalTo("true"), + jdiAssert(element().ItemPerPageSelect.attr("aria-disabled"), Matchers.equalTo("true"), "item per page selector should be DISABLED"); return this; } diff --git a/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/Paginator.java b/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/Paginator.java index c35ac5d185..325557c755 100644 --- a/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/Paginator.java +++ b/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/Paginator.java @@ -4,19 +4,13 @@ import com.epam.jdi.light.angular.elements.common.Button; import com.epam.jdi.light.common.JDIAction; import com.epam.jdi.light.elements.base.UIBaseElement; -import com.epam.jdi.light.elements.common.UIElement; -import com.epam.jdi.light.elements.complex.WebList; import com.epam.jdi.light.elements.interfaces.base.HasColor; -import org.openqa.selenium.By; -import org.openqa.selenium.Point; +import com.epam.jdi.light.elements.pageobjects.annotations.locators.JDropdown; -import java.util.List; -import java.util.NoSuchElementException; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.Integer.parseInt; -import static java.util.stream.Collectors.toList; /** * To see an example of Paginator web element please visit .... @@ -29,14 +23,13 @@ public class Paginator extends UIBaseElement implements HasColo private static final String FIRST_PAGE_BUTTON_LOCATOR = "button.mat-mdc-paginator-navigation-first"; private static final String LAST_PAGE_BUTTON_LOCATOR = "button.mat-mdc-paginator-navigation-last"; private static final String PREVIOUS_PAGE_BUTTON_LOCATOR = "button.mat-mdc-paginator-navigation-previous"; + private static final String NEXT_PAGE_BUTTON_LOCATOR = "button.mat-mdc-paginator-navigation-next"; private static final String BOARDER_LOCATOR = ".mdc-notched-outline__leading"; private static final String PAGINATOR_PAGE_SIZE_SECTION_LOCATOR = ".mat-mdc-paginator-page-size"; - private static final String ITEM_PER_PAGE_SELECTOR_LOCATOR = "mat-select"; - private static final String SELECT_PANEL_LOCATOR = "div.mat-mdc-select-panel"; - private static final String ITEM_PER_PAGE_OPTIONS_LOCATOR = "mat-option"; - private static final String ARIA_EXPANDED_ATTR = "aria-expanded"; + @JDropdown(root = "mat-form-field.mat-mdc-form-field-type-mat-select", value = "mat-select", list = "//ancestor::body//mat-option") + public PaginatorSelector ItemPerPageSelect; private Pattern rangeLabelPattern = Pattern.compile("^(\\d+)( . (\\d+))? .+ (\\d+)"); @JDIAction("Set pattern for '{name}' range label") @@ -91,53 +84,14 @@ public Button lastPageButton() { return new Button().setCore(Button.class, core().find(LAST_PAGE_BUTTON_LOCATOR)); } - @JDIAction("Get item per page selector for '{name}'") - public UIElement itemPerPageSelector() { - return core().find(ITEM_PER_PAGE_SELECTOR_LOCATOR); - } - - // @JDIAction("Get item per page selector for '{name}'") -// public UIElement materiialSelector() { -// return new Dropdown().setCore(core().find(ITEM_PER_PAGE_SELECTOR_LOCATOR)); -// } - @JDIAction("Get WebList of items per page options for '{name}'") - private WebList itemsPerPageOptionsWebList() { - return new WebList(By.cssSelector(ITEM_PER_PAGE_OPTIONS_LOCATOR)); - } - - @JDIAction("Get options for '{name}'") - public List options() { - expandItemPerPageOptions(); - final List collect = itemsPerPageOptionsWebList().values().stream().map(Integer::parseInt).collect(toList()); - collapseItemPerPageOptions(); - return collect; - } - - @JDIAction("Expand options for '{name}'") - private void expandItemPerPageOptions() { - if (itemPerPageSelector().attr(ARIA_EXPANDED_ATTR).equals("false")) { - itemPerPageSelector().click(); - } - } - - @JDIAction("Collapse items per page options for '{name}'") - private void collapseItemPerPageOptions() { - if (itemPerPageSelector().attr(ARIA_EXPANDED_ATTR).equals("true")) { - UIElement uiElement = new UIElement(By.cssSelector(SELECT_PANEL_LOCATOR)); - Point pointOutsidePanel = new Point(uiElement.core().getRect().getWidth() + 2, uiElement.core().getRect().getHeight() + 2); - uiElement.core().click(pointOutsidePanel.getX(), pointOutsidePanel.getY()); - } - } - @JDIAction("Select items per page option '{0}' for '{name}'") public void selectItemPerPageOption(int number) { - expandItemPerPageOptions(); - itemsPerPageOptionsWebList().select(String.valueOf(number)); + ItemPerPageSelect.select(" " + number + " "); } @JDIAction("Get selected option for '{name}'") public int selected() { - return parseInt(itemPerPageSelector().getText()); + return parseInt(ItemPerPageSelect.selected()); } @JDIAction("Get range for '{name}'") @@ -161,30 +115,30 @@ public String color() { if (core().hasAttribute("color")) { return core().attr("color"); } - expandItemPerPageOptions(); + ItemPerPageSelect.expand(); final String cssValue = core().find(ITEM_PER_PAGE_FIELD_LOCATOR).find(BOARDER_LOCATOR).getCssValue("border-color"); - collapseItemPerPageOptions(); + ItemPerPageSelect.close(); return cssValue; } - @JDIAction("Get color for selected value in the list of options for '{name}'") - public String selectedOptionColor() { - expandItemPerPageOptions(); - String cssValue = selectedOptionFromItemsPerPageList().find("span").getCssValue("COLOR_ATT"); - collapseItemPerPageOptions(); - return cssValue; - } +// @JDIAction("Get color for selected value in the list of options for '{name}'") +// public String selectedOptionColor() { +// expandItemPerPageOptions(); +// String cssValue = selectedOptionFromItemsPerPageList().find("span").getCssValue("COLOR_ATT"); +// collapseItemPerPageOptions(); +// return cssValue; +// } - @JDIAction("Get selected option from items per page list for '{name}'") - private UIElement selectedOptionFromItemsPerPageList() { - return itemsPerPageOptionsWebList().stream() - .filter(el -> el - .attr("aria-selected").equals("true")) - .findFirst() - .orElseThrow( - () -> new NoSuchElementException("No element with attribute aria-selected = true") - ); - } +// @JDIAction("Get selected option from items per page list for '{name}'") +// private UIElement selectedOptionFromItemsPerPageList() { +// return itemsPerPageOptionsWebList().stream() +// .filter(el -> el +// .attr("aria-selected").equals("true")) +// .findFirst() +// .orElseThrow( +// () -> new NoSuchElementException("No element with attribute aria-selected = true") +// ); +// } @JDIAction("Get '{name}' firstPageLabel") public String lastPageLabel() { diff --git a/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/PaginatorSelector.java b/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/PaginatorSelector.java new file mode 100644 index 0000000000..9a986d3a28 --- /dev/null +++ b/jdi-light-angular/src/main/java/com/epam/jdi/light/angular/elements/complex/PaginatorSelector.java @@ -0,0 +1,19 @@ +package com.epam.jdi.light.angular.elements.complex; + +import com.epam.jdi.light.elements.complex.dropdown.Dropdown; + +import static org.openqa.selenium.Keys.ESCAPE; + +public class PaginatorSelector extends Dropdown { + @Override + public void close() { + if (isExpanded()) { + press(ESCAPE); + } + } + + @Override + public boolean isExpanded() { + return expander().attr("aria-expanded").equals("true"); + } +}