22
33import com .capgemini .framework .logger .AllureStepLogger ;
44import com .capgemini .framework .logger .Logger ;
5+ import com .microsoft .playwright .Dialog ;
6+ import com .microsoft .playwright .ElementHandle ;
57import com .microsoft .playwright .Locator ;
68import com .microsoft .playwright .Page ;
79import com .microsoft .playwright .options .LoadState ;
10+ import com .microsoft .playwright .options .WaitForSelectorState ;
811import io .qameta .allure .Step ;
12+ import org .assertj .core .api .Assertions ;
13+ import org .assertj .core .api .Fail ;
914
15+ import java .util .List ;
16+ import java .util .function .Consumer ;
17+
18+ import static com .capgemini .framework .playwright .PlaywrightFactory .getBrowserContext ;
1019import static com .capgemini .framework .playwright .PlaywrightFactory .getPage ;
20+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
21+ import static org .junit .jupiter .api .Assertions .fail ;
1122
1223public final class ActionGui {
1324 @ Step ("Fill text field \" {fieldName}\" with \" {inputValue}\" " )
1425 public static void fillTextBox (String fieldName , String inputValue , Locator locator ) {
1526 locator .click ();
1627 locator .fill (inputValue );
28+ AllureStepLogger .info (inputValue + " in textbox " + fieldName + " is typed" );
1729 }
1830
1931 public static void fillTextBox (String fieldName , String inputValue , String selector ) {
@@ -23,6 +35,7 @@ public static void fillTextBox(String fieldName, String inputValue, String selec
2335 @ Step ("Set radiobutton \" {fieldName}\" " )
2436 public static void setRadioButton (String fieldName , Locator locator ) {
2537 locator .click ();
38+ AllureStepLogger .info (fieldName + " is selected" );
2639 }
2740
2841 public static void setRadioButton (String fieldName , String selector ) {
@@ -31,29 +44,111 @@ public static void setRadioButton(String fieldName, String selector) {
3144
3245 @ Step ("Click \" {fieldName}\" " )
3346 public static void click (String fieldName , Locator locator ) {
34- locator .click ();
47+ if (!locator .isVisible ()){
48+ Fail .fail (fieldName + " is not visible and cannot be clicked." );
49+ }
50+ if (!locator .isEnabled ()){
51+ Fail .fail (fieldName + " is not enabled and cannot be clicked." );
52+ }
53+ try {
54+ locator .click (new Locator .ClickOptions ().setTimeout (20000 ));
55+ } catch (Exception e ) {
56+ Logger .logError ("Error while clicking on " + fieldName + ": " + e .getMessage ());
57+ Fail .fail (fieldName + " cannot be clicked." );
58+ }
59+ AllureStepLogger .info (fieldName + " is clicked" );
60+ }
61+
62+ public static void click (String fieldName , Locator locator , double timeout ) {
63+ if (!locator .isVisible ()){
64+ Fail .fail (fieldName + " is not visible and cannot be clicked." );
65+ }
66+ if (!locator .isEnabled ()){
67+ Fail .fail (fieldName + " is not enabled and cannot be clicked." );
68+ }
69+ try {
70+ locator .click (new Locator .ClickOptions ().setTimeout (timeout ));
71+ } catch (Exception e ) {
72+ Logger .logError ("Error while clicking on " + fieldName + ": " + e .getMessage ());
73+ Fail .fail (fieldName + " cannot be clicked. " + e .getMessage ());
74+ }
75+ AllureStepLogger .info (fieldName + " is clicked" );
76+ }
77+
78+ public static void click (String fieldName , Locator locator , boolean waitForPageLoadingToFinish ) {
79+ click (fieldName , locator );
80+ if (waitForPageLoadingToFinish ) {
81+ ActionGui .waitForPageLoadingFinish ();
82+ }
3583 }
3684
3785 public static void pressEnter (Locator locator ) {
3886 locator .press ("Enter" );
3987 }
40-
88+ @ Step ("Select value \" {value}\" in Dropdown \" {fieldName}\" " )
89+ public static void selectDropdownValue (String value , String fieldName , Locator locator ) {
90+ locator .click ();
91+ locator .selectOption (value );
92+ AllureStepLogger .info (value + " in Dropdown " + fieldName + " is selected" );
93+ }
4194 @ Step ("Verify {fieldName} is visible" )
4295 public static void verifyElementVisible (String fieldName , Locator locator ) {
43- locator .isVisible ();
96+ Assertions . assertThat ( locator .isVisible ()). as ( fieldName + " visible" ). withFailMessage ( fieldName + " is not visible" ). isTrue ();
4497 }
4598
4699 public static void verifyElementVisible (String fieldName , String selector ) {
47100 verifyElementVisible (fieldName , getPage ().locator (selector ));
48101 }
49-
102+ @ Step ("Wait for {fieldName} to disappear" )
103+ public static void waitForElementToDisappear (String fieldName , Locator locator ) {
104+ locator .waitFor (new Locator .WaitForOptions ().setState (WaitForSelectorState .HIDDEN ));
105+ }
106+
50107 @ Step ("Open page {url}" )
51108 public static void navigate (String url , int pageLoadingTimeout ) {
52109 getPage ().navigate (url , new Page .NavigateOptions ().setTimeout (pageLoadingTimeout ));
53110 getPage ().onLoad (p -> AllureStepLogger .info ("Page loaded!" ));
54111 }
55-
112+ @ Step ("Check {fieldName} checkbox" )
113+ public static void checkCheckBox (String fieldName , Locator locator ) {
114+ locator .check ();
115+ AllureStepLogger .info (fieldName + " checkbox is checked" );
116+ }
56117
118+ public static void clickIfVisible (String fieldName , Locator locator , boolean b ) {
119+ if (locator .isVisible ()) {
120+ click (fieldName , locator , b );
121+ }
122+ }
123+
124+ @ Step ("Check Banner has text {bannerText}" )
125+ public static void checkBannerText (String bannerText , Locator locator ) {
126+ AllureStepLogger .info ("Banner text is: " + locator .textContent ());
127+ assertThat (locator .textContent ()).withFailMessage ("Text on banner is " + locator .textContent () + " but it should be " + bannerText )
128+ .contains (bannerText );
129+ }
130+
131+ @ Step ("Get Banner text" )
132+ public static String getBannerText () {
133+ Locator banner ;
134+ try { banner = getPage ().locator ("div[class*='PageControllerStatusLine']" );}
135+ catch (Exception e ){
136+ return "" ;
137+ };
138+ AllureStepLogger .info ("Banner text is: " + banner .textContent ());
139+ return banner .textContent ();
140+ }
141+ @ Step ("Check that {element} has text {expectedText}" )
142+ public static void verifyElementHasText (String element , String expectedText , Locator locator ) {
143+ AllureStepLogger .info (element + " text is: " + locator .textContent ());
144+ assertThat (locator .textContent ()).withFailMessage ("Text is " + locator .textContent () + " but it should be " + expectedText )
145+ .contains (expectedText );
146+ }
147+
148+ public static boolean isElementPresent (Locator locator ) {
149+ return locator .isVisible ();
150+ }
151+
57152 public void waitForPageToLoad () {
58153 getPage ().waitForLoadState (LoadState .NETWORKIDLE );
59154 }
@@ -72,4 +167,68 @@ public static void waitMilliseconds(Integer milliseconds) {
72167 }
73168 }
74169
170+ @ Step ("Wait for page loading to finish" )
171+ public static void waitForPageLoadingFinish (){
172+ String loadingAnimation = "div[id^='RadAjaxLoadingPanel']" ;
173+ waitForElementToDisappear ("Loading animation" , getPage ().locator (loadingAnimation ).first ());
174+ //second version: getPage().waitForSelector(loadingAnimation).waitForElementState(ElementState.HIDDEN);
175+ }
176+
177+
178+ @ Step ("Check on column name {columnName} for first row text {searchText}" )
179+ public static boolean isTextPresentInFirstRow (String columnName , String searchText ) {
180+ int columnIndex = getColumnIndex (columnName );
181+ List <ElementHandle > rows = getPage ().querySelectorAll ("table tr" );
182+ if (!rows .isEmpty ()) {
183+ ElementHandle firstRow = rows .get (0 );
184+ String cellText = firstRow .querySelectorAll ("td span" ).get (columnIndex ).textContent ();
185+ return cellText .contains (searchText );
186+ } else {
187+ return false ;
188+ }
189+ }
190+
191+ @ Step ("Find index for column name {columnName}" )
192+ private static int getColumnIndex (String columnName ) {
193+ List <ElementHandle > headers = getPage ().querySelectorAll ("table th a span" );
194+ for (int i = 0 ; i < headers .size (); i ++) {
195+ String headerText = headers .get (i ).textContent ();
196+ if (headerText .equals (columnName )) {
197+ return i ;
198+ }
199+ }
200+ return -1 ;
201+ }
202+
203+ @ Step ("Create a dialog handler that will check message text and press OK/Cancel" )
204+ public static void checkPopupMessageTextAndPressOKCancel (boolean pressOK , String messageText ) {
205+ Consumer <Dialog > handler = dialog -> {
206+ Logger .logInfo ("Popup message: " + dialog .message ());
207+ Logger .logInfo ("Popup message will be OK: " + pressOK );
208+ // Which option you want to click
209+ if (dialog .message ().contains (messageText )) {
210+ if (pressOK ) {
211+ // Click on button "OK"
212+ dialog .accept ();
213+ Logger .logInfo ("Clicked on 'OK'" );
214+ } else {
215+ // Click on button "Cancel"
216+ dialog .dismiss ();
217+ Logger .logInfo ("Clicked on 'Cancel'" );
218+ }
219+ }else {
220+ fail ("Popup message is not as expected " + dialog .message () + " but it should be " + messageText );
221+ }
222+ };
223+ getPage ().onDialog (handler );
224+ getPage ().offDialog (handler );
225+ }
226+ @ Step ("Check that {fieldName} has text {expectedText}" )
227+ public static void checkText (String fieldName , String expectedText , Locator locator ) {
228+ assertThat (locator .textContent ())
229+ .as (fieldName )
230+ .isEqualTo (expectedText );
231+ }
232+
233+
75234}
0 commit comments