44import com .epam .jdi .light .angular .elements .common .Button ;
55import com .epam .jdi .light .common .JDIAction ;
66import com .epam .jdi .light .elements .base .UIBaseElement ;
7+ import com .epam .jdi .light .elements .common .UIElement ;
8+ import com .epam .jdi .light .elements .complex .WebList ;
9+ import org .openqa .selenium .By ;
10+ import org .openqa .selenium .Point ;
711
812import java .util .List ;
13+ import java .util .NoSuchElementException ;
914import java .util .regex .Matcher ;
1015import java .util .regex .Pattern ;
11- import java .util .stream .Collectors ;
1216
1317import static java .lang .Integer .parseInt ;
18+ import static java .util .stream .Collectors .toList ;
1419
1520/**
1621 * To see an example of Paginator web element please visit <a href="https://material.angular.io/components/paginator/overview">...</a>.
@@ -27,12 +32,12 @@ public class Paginator extends UIBaseElement<PaginatorAssert> {
2732 private static final String BOARDER_LOCATOR = ".mdc-notched-outline__leading" ;
2833 private static final String PAGINATOR_PAGE_SIZE_SECTION_LOCATOR = ".mat-mdc-paginator-page-size" ;
2934 private static final String ITEM_PER_PAGE_SELECTOR_LOCATOR = "mat-select" ;
30- private final PaginatorSelector itemPerPageSelector ;
31- private Pattern rangeLabelPattern = Pattern .compile ("^(\\ d+)( . (\\ d+))? .+ (\\ d+)" );
35+ private static final String SELECT_PANEL_LOCATOR = "div.mat-mdc-select-panel" ;
36+ private static final String ITEM_PER_PAGE_OPTIONS_LOCATOR = "mat-option" ;
37+ private static final String ARIA_EXPANDED_ATTR = "aria-expanded" ;
38+ private static final String COLOR_ATT = "color" ;
3239
33- public Paginator () {
34- itemPerPageSelector = new PaginatorSelector ().setCore (PaginatorSelector .class , core ().find (ITEM_PER_PAGE_SELECTOR_LOCATOR ));
35- }
40+ private Pattern rangeLabelPattern = Pattern .compile ("^(\\ d+)( . (\\ d+))? .+ (\\ d+)" );
3641
3742 @ JDIAction ("Set pattern for '{name}' range label" )
3843 public void setRangeLabelPattern (String regex ) {
@@ -87,23 +92,48 @@ public Button lastPageButton() {
8792 }
8893
8994 @ JDIAction ("Get item per page selector for '{name}'" )
90- public PaginatorSelector itemPerPageSelector () {
91- return itemPerPageSelector ;
95+ public UIElement itemPerPageSelector () {
96+ return core ().find (ITEM_PER_PAGE_SELECTOR_LOCATOR );
97+ }
98+
99+ @ JDIAction ("Get WebList of items per page options for '{name}'" )
100+ private WebList itemsPerPageOptionsWebList () {
101+ return new WebList (By .cssSelector (ITEM_PER_PAGE_OPTIONS_LOCATOR ));
92102 }
93103
94104 @ JDIAction ("Get options for '{name}'" )
95105 public List <Integer > options () {
96- return itemPerPageSelector .values ().stream ().map (Integer ::parseInt ).collect (Collectors .toList ());
106+ expandItemPerPageOptions ();
107+ final List <Integer > collect = itemsPerPageOptionsWebList ().values ().stream ().map (Integer ::parseInt ).collect (toList ());
108+ collapseItemPerPageOptions ();
109+ return collect ;
110+ }
111+
112+ @ JDIAction ("Expand options for '{name}'" )
113+ private void expandItemPerPageOptions () {
114+ if (itemPerPageSelector ().attr (ARIA_EXPANDED_ATTR ).equals ("false" )) {
115+ itemPerPageSelector ().click ();
116+ }
117+ }
118+
119+ @ JDIAction ("Collapse items per page options for '{name}'" )
120+ private void collapseItemPerPageOptions () {
121+ if (itemPerPageSelector ().attr (ARIA_EXPANDED_ATTR ).equals ("true" )) {
122+ UIElement uiElement = new UIElement (By .cssSelector (SELECT_PANEL_LOCATOR ));
123+ Point pointOutsidePanel = new Point (uiElement .core ().getRect ().getWidth () + 2 , uiElement .core ().getRect ().getHeight () + 2 );
124+ uiElement .core ().click (pointOutsidePanel .getX (), pointOutsidePanel .getY ());
125+ }
97126 }
98127
99- @ JDIAction ("Select option for '{name}'" )
100- public void select (int number ) {
101- itemPerPageSelector .select (String .valueOf (number ));
128+ @ JDIAction ("Select items per page option '{0}' for '{name}'" )
129+ public void selectItemPerPageOption (int number ) {
130+ expandItemPerPageOptions ();
131+ itemsPerPageOptionsWebList ().select (String .valueOf (number ));
102132 }
103133
104134 @ JDIAction ("Get selected option for '{name}'" )
105135 public int selected () {
106- return parseInt (itemPerPageSelector . toggleValue ());
136+ return parseInt (itemPerPageSelector (). getText ());
107137 }
108138
109139 @ JDIAction ("Get range for '{name}'" )
@@ -123,26 +153,37 @@ public void nextPage() {
123153
124154 @ JDIAction ("Get color theme for '{name}'" )
125155 public String colorTheme () {
126- final String colorAtt = "color" ;
127- return core ().hasAttribute (colorAtt ) ? core ().attr (colorAtt ) : "primary" ;
156+ return core ().hasAttribute (COLOR_ATT ) ? core ().attr (COLOR_ATT ) : "primary" ;
128157 }
129158
130159 @ JDIAction ("Get color of selector`s boarder for '{name}'" )
131160 public String boarderColor () {
132- itemPerPageSelector . expand ();
161+ expandItemPerPageOptions ();
133162 final String cssValue = core ().find (ITEM_PER_PAGE_FIELD_LOCATOR ).find (BOARDER_LOCATOR ).getCssValue ("border-color" );
134- itemPerPageSelector . collapse ();
163+ collapseItemPerPageOptions ();
135164 return cssValue ;
136165 }
137166
167+
138168 @ JDIAction ("Get color for selected value in the list of options for '{name}'" )
139169 public String selectedOptionColor () {
140- itemPerPageSelector . expand ();
141- String cssValue = itemPerPageSelector . selectedOptionFromList ().getCssValue ( "color" );
142- itemPerPageSelector . collapse ();
170+ expandItemPerPageOptions ();
171+ String cssValue = selectedOptionFromItemsPerPageList ().find ( "span" ). getCssValue ( COLOR_ATT );
172+ collapseItemPerPageOptions ();
143173 return cssValue ;
144174 }
145175
176+ @ JDIAction ("Get selected option from items per page list for '{name}'" )
177+ private UIElement selectedOptionFromItemsPerPageList () {
178+ return itemsPerPageOptionsWebList ().stream ()
179+ .filter (el -> el
180+ .attr ("aria-selected" ).equals ("true" ))
181+ .findFirst ()
182+ .orElseThrow (
183+ () -> new NoSuchElementException ("No element with attribute aria-selected = true" )
184+ );
185+ }
186+
146187 @ JDIAction ("Get '{name}' firstPageLabel" )
147188 public String lastPageLabel () {
148189 return core ().attr ("lastPageLabel" );
0 commit comments