Merge changes Ib4430bf2,Icc0bdb9e,I2736b984
[vid.git] / vid-automation / src / main / java / org / onap / sdc / ci / tests / utilities / GeneralUIUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.ci.tests.utilities;
22
23 //import com.automation.common.report_portal_integration.annotations.Step;
24 import com.aventstack.extentreports.Status;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.Assert;
27 import org.onap.sdc.ci.tests.datatypes.DataTestIdEnum;
28 import org.onap.sdc.ci.tests.datatypes.DataTestIdEnum.DashboardCardEnum;
29 import org.onap.sdc.ci.tests.execute.setup.DriverFactory;
30 import org.onap.sdc.ci.tests.execute.setup.SetupCDTest;
31 import org.openqa.selenium.*;
32 import org.openqa.selenium.firefox.FirefoxDriver;
33 import org.openqa.selenium.interactions.Actions;
34 import org.openqa.selenium.support.ui.*;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.awt.*;
39 import java.awt.event.KeyEvent;
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.List;
44 import java.util.Random;
45 import java.util.UUID;
46 import java.util.concurrent.TimeUnit;
47 import java.util.function.Supplier;
48
49 import static org.hamcrest.Matchers.is;
50
51
52 public final class GeneralUIUtils {
53
54         public static final String FILE_NAME = "Valid_tosca_Mycompute.yml";
55         static final Logger logger = LoggerFactory.getLogger(GeneralUIUtils.class);
56
57         private static int timeOut=90;
58
59 //      public static void setTimeOut(int time) {
60 //              if (time>0) {
61 //                      timeOut=time;
62 //              }
63 //              else {
64 //                      timeOut=timeOut;
65 //              }
66 //      }
67
68         /**************** DRIVER ****************/
69
70         public static WebDriver getDriver() {
71                 try{
72                         return DriverFactory.getDriver();
73                 }
74                 catch(Exception e){
75                         e.printStackTrace();
76                 }
77                 return null;
78         }
79         /****************************************/
80
81         public static List<WebElement> getElemenetsFromTable(By by) {
82                 return getDriver().findElements(by);
83         }
84
85         public static File takeScreenshot(String screenshotFilename, String dir, String testName) throws IOException {
86                 if (screenshotFilename == null) {
87                         if (testName != null){
88                                 screenshotFilename = testName;
89                         }
90                         else
91                         {
92                                 screenshotFilename = UUID.randomUUID().toString();
93                         }
94                 }
95                 try {
96                         File scrFile = ((TakesScreenshot) getDriver()).getScreenshotAs(OutputType.FILE);
97                         File filePath = new File(String.format("%s/%s.png", dir, screenshotFilename));
98                         new File(dir).mkdirs();
99                         FileUtils.copyFile(scrFile, filePath);
100                         return filePath;
101                 } catch (IOException e1) {
102                         e1.printStackTrace();
103                 }
104                 return null;
105         }
106
107         public static File takeScreenshot(String screenshotFilename, String dir) throws IOException{
108                 return takeScreenshot(screenshotFilename, dir, null);
109         }
110
111
112         public static void scrollDown() {
113                 try{
114                         Robot robot = new Robot();
115                         robot.keyPress(KeyEvent.VK_DOWN);
116                         robot.keyRelease(KeyEvent.VK_DOWN);
117                         GeneralUIUtils.waitForLoader();
118                 }
119                 catch(Exception e){
120                         e.printStackTrace();
121                 }
122         }
123
124
125         public static WebElement getWebElementByTestID(String dataTestId) {
126                 return getWebElementByTestID(dataTestId, timeOut);
127         }
128
129         public static WebElement getWebElementByTestID(String dataTestId, int timeout) {
130                 WebDriverWait wait = newWait(timeout);
131                 return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@data-tests-id='" + dataTestId + "']")));
132         }
133
134         public static boolean isWebElementExistByTestId(String dataTestId) {
135                 if(getDriver().findElements(By.xpath("//*[@data-tests-id='" + dataTestId + "']")).size() == 0) {
136                         return false;
137                 }
138                 return true;
139         }
140
141         public static WebElement getInputElement(String dataTestId) {
142                 try{
143                         ultimateWait();
144                         return getDriver().findElement(By.xpath("//*[@data-tests-id='" + dataTestId + "']"));
145                 }
146                 catch(Exception e){
147                         return null;
148                 }
149         }
150
151         public static List<WebElement> getInputElements(String dataTestId) {
152                 ultimateWait();
153                 return getDriver().findElements(By.xpath("//*[@data-tests-id='" + dataTestId + "']"));
154
155         }
156
157         public static WebElement getWebElementBy(By by) {
158                 return getWebElementBy(by, timeOut);
159         }
160
161         public static WebElement getWebElementBy(By by, int timeoutInSeconds) {
162                 WebDriverWait wait = newWait(timeoutInSeconds);
163                 return wait.until(ExpectedConditions.visibilityOfElementLocated(by));
164         }
165
166         public static List<String> getWebElementListText(List<WebElement>elements) {
167                 List<String>Text=new ArrayList<>();
168                 for (WebElement webElement : elements) {
169                         Text.add(webElement.getText());
170                 }
171                 return Text;
172         }
173
174
175         public static List<WebElement> getWebElementsListBy(By by) {
176                 return getWebElementsListBy(by, timeOut);
177         }
178
179         public static List<WebElement> getWebElementsListBy(By by, int timeOut) {
180                 WebDriverWait wait = newWait(timeOut);
181                 return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by));
182         }
183
184         public static List<WebElement> getWebElementsListByContainTestID(String dataTestId) {
185                 try{
186                         WebDriverWait wait = newWait(10);
187                         return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[contains(@data-tests-id, '"+dataTestId+"')]")));
188                 }
189                 catch(Exception e){
190                         return new ArrayList<WebElement>();
191                 }
192         }
193
194         public static List<WebElement> getWebElementsListByContainsClassName(String containedText) {
195                 return getWebElementsListByContainsClassName(containedText, timeOut);
196         }
197
198         public static List<WebElement> getWebElementsListByContainsClassName(String containedText, int timeoutInSeconds) {
199                 WebDriverWait wait = newWait(timeoutInSeconds);
200                 return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[contains(@class, '"+containedText+"')]")));
201         }
202
203         public static WebElement getWebElementByContainsClassName(String containedText) {
204                 return getWebElementByContainsClassName(containedText, timeOut);
205         }
206
207         public static WebElement getWebElementByContainsClassName(String containedText, int timeoutInSeconds) {
208                 return getWebElementBy(By.xpath("//*[contains(@class, '"+containedText+"')]"), timeoutInSeconds);
209         }
210
211         public static WebElement getWebElementByClassName(String className) {
212                 return getWebElementByClassName(className, timeOut);
213         }
214
215         public static WebElement getWebElementByClassName(String className, int timeoutInSeconds) {
216                 WebDriverWait wait = newWait(timeoutInSeconds);
217                 return wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(className)));
218         }
219
220     public static List<WebElement> getWebElementsListByContainsClassNameAndText(String cssName, String text, int timeoutInSeconds) {
221         WebDriverWait wait = newWait(timeoutInSeconds);
222         String xpath = String.format("//*[contains(@class, '%s') and contains(text(),'%s')]", cssName, text);
223         return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(
224                 By.xpath(xpath)));
225     }
226
227         public static WebElement getWebElementByLinkText(String linkText) {
228                 WebDriverWait wait = newWait(timeOut);
229                 return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@text='" + linkText + "']")));
230         }
231
232
233         public static List<WebElement> getWebElementsListByTestID(String dataTestId) {
234                 WebDriverWait wait = newWait(timeOut);
235                 return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[@data-tests-id='" + dataTestId + "']")));
236         }
237
238         public static List<WebElement> getWebElementsListByClassName(String className) {
239                 WebDriverWait wait = newWait(timeOut);
240                 return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className(className)));
241         }
242
243
244
245
246         public static Boolean isElementInvisibleByTestId(String dataTestId) {
247                 WebDriverWait wait = newWait(timeOut);
248                 return wait.until(
249                                 ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@data-tests-id='" + dataTestId + "']")));
250         }
251
252         public static Boolean isElementVisibleByTestId(String dataTestId) {
253                 try{
254                         WebDriverWait wait = newWait(timeOut);
255                         if(wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//*[@data-tests-id='" + dataTestId + "']")))).isDisplayed()){
256                                 return true;
257                         }else {
258                                 return false;
259                         }
260                 }
261                 catch(Exception e){
262                         return false;
263                 }
264         }
265
266         public static void clickOnElementByTestId(String dataTestId) {
267                 clickOnElementByTestIdWithoutWait(dataTestId);
268                 ultimateWait();
269         }
270
271         public static void clickOnElementByTestIdWithoutWait(String dataTestId) {
272                 WebDriverWait wait = newWait(timeOut);
273                 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@data-tests-id='" + dataTestId + "']"))).click();
274         }
275
276         public static void clickOnElementByTestId(String dataTestId, int customTimeout) {
277                 WebDriverWait wait = newWait(customTimeout);
278                 wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@data-tests-id='" + dataTestId + "']"))).click();
279         }
280
281         public static WebElement waitForElementVisibilityByTestId(String dataTestId) {
282                 WebDriverWait wait = newWait(timeOut);
283                 return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@data-tests-id='" + dataTestId + "']")));
284         }
285
286         public static Boolean waitForElementInVisibilityByTestId(String dataTestId) {
287                 return waitForElementInVisibilityByTestId(dataTestId, timeOut);
288         }
289
290         public static Boolean waitForElementInVisibilityByTestId(String dataTestId, int timeOut) {
291                 WebDriverWait wait = newWait(timeOut);
292                 boolean displayed = getDriver().findElements(By.xpath("//*[@data-tests-id='" + dataTestId + "']")).isEmpty();
293                 if (!displayed){
294                         Boolean until = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@data-tests-id='" + dataTestId + "'])")));
295                         ultimateWait();
296                         return until;
297                 }
298                 return false;
299         }
300
301         public static Boolean waitForElementInVisibilityByTestId(By by) {
302                 return waitForElementInVisibilityBy(by, timeOut);
303         }
304
305
306         public static Boolean waitForElementInVisibilityBy(By by, int timeOut) {
307                 WebDriverWait wait = newWait(timeOut);
308                 boolean displayed = getDriver().findElements(by).isEmpty();
309                 if (!displayed){
310                         Boolean until = wait.until(ExpectedConditions.invisibilityOfElementLocated(by));
311                         sleep(1);
312                         return until;
313                 }
314                 return false;
315         }
316
317
318         public static void setWebElementByTestId(String elementID, String value) {
319                 WebElement resourceDescriptionTextbox = GeneralUIUtils.getWebElementByTestID(elementID);
320                 resourceDescriptionTextbox.clear();
321                 resourceDescriptionTextbox.sendKeys(value);
322
323         }
324
325         public static WebElement hoverOnAreaByTestId(String areaId) {
326                 Actions actions = new Actions(getDriver());
327                 WebElement area = getWebElementByTestID(areaId);
328                 actions.moveToElement(area).perform();
329                 ultimateWait();
330                 return area;
331         }
332
333         public static WebElement hoverOnAreaByClassName(String className) {
334                 Actions actions = new Actions(getDriver());
335                 WebElement area = getWebElementByClassName(className);
336                 actions.moveToElement(area).perform();
337                 GeneralUIUtils.ultimateWait();
338                 return area;
339         }
340
341         public static void clickElementUsingActions(WebElement element){
342                 Actions actions = new Actions(getDriver());
343
344                 actions.moveToElement(element);
345                 actions.perform();
346
347                 actions.click();
348                 actions.perform();
349
350                 ultimateWait();
351         }
352
353         public static void waitForLoader() {
354                 waitForLoader(timeOut);
355         }
356
357         public static void waitForLoader(int timeOut) {
358                 sleep(1);
359                 waitForElementInVisibilityBy(By.className("tlv-loader"), timeOut);
360         }
361
362         public static void findComponentAndClick(String resourceName) throws Exception {
363                 SetupCDTest.getExtendTest().log(Status.INFO, "Searching for " + resourceName + " in homepage");
364                 WebElement searchTextbox = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.MainMenuButtons.SEARCH_BOX.getValue());
365                 try{
366                         searchTextbox.clear();
367                         searchTextbox.sendKeys(resourceName);
368                         ultimateWait();
369                 }
370                 catch(Exception e){
371                         SetupCDTest.getExtendTest().log(Status.INFO, "Can't interact with search bar");
372                         e.printStackTrace();
373                 }
374
375
376                 try{
377                         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Clicking on the %s component from home screen", resourceName));
378                         clickOnElementByTestId(resourceName);
379                         GeneralUIUtils.ultimateWait();
380                         getWebElementByTestID(DataTestIdEnum.GeneralElementsEnum.LIFECYCLE_STATE.getValue());
381                 }
382                 catch(Exception e){
383                         SetupCDTest.getExtendTest().log(Status.INFO, "Can't click on component named " + resourceName);
384                         e.printStackTrace();
385                 }
386         }
387
388
389         public static String getComponentVersion(String componentName) {
390                 return GeneralUIUtils.getWebElementByTestID(componentName + "Version").getText();
391         }
392
393         public static void windowZoomOut() {
394                         final int zoomOutFactor = 3;
395                         for (int i = 0; i < zoomOutFactor; i++) {
396                                 if(getDriver() instanceof FirefoxDriver) {
397                                         getDriver().findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
398                                 }
399                 }
400         }
401
402         public static void resetZoom(){
403                 getDriver().findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));
404         }
405
406         public static void windowZoomOutUltimate(){
407                 resetZoom();
408                 windowZoomOut();
409 //              JavascriptExecutor js = (JavascriptExecutor) driver;
410 //              js.executeScript("document.body.style.zoom='90%'");
411         }
412
413         public static void clickASDCLogo() {
414                 WebDriverWait wait = newWait(15);
415                 wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("ASDC")));
416                 WebElement ClickASDCLogo = getDriver().findElement(By.linkText("ASDC"));
417                 ClickASDCLogo.click();
418                 GeneralUIUtils.waitForLoader();
419         }
420
421         public static void sleep(int millis) {
422                 try {
423                         Thread.sleep(millis);
424                 } catch (InterruptedException e) {
425                         throw new RuntimeException(e);
426                 }
427         }
428
429         public static void moveToStep(DataTestIdEnum.StepsEnum Stepname) {
430                 moveToStep(Stepname.getValue());
431                 SetupCDTest.getExtendTest().log(Status.INFO, String.format("Going to %s page ", Stepname.toString()));
432         }
433
434         public static void moveToStep(String dataTestId) {
435                 clickOnElementByTestId(dataTestId);
436                 ultimateWait();
437         }
438
439
440         public static Select getSelectList(String item, String datatestsid) {
441                 Select selectlist = new Select(getWebElementByTestID(datatestsid));
442                 if (item != null) {
443                         selectlist.selectByVisibleText(item);
444                         Assert.assertThat(selectlist.getFirstSelectedOption().getText(), is(item));
445                 }
446                 return selectlist;
447         }
448
449         public static List<WebElement> waitForElementsListVisibilityTestMethod(DashboardCardEnum dataTestId) {
450                 GeneralUIUtils.waitForLoader();
451                 return getDriver().findElements(By.xpath("//*[@data-tests-id='" + dataTestId.getValue() + "']"));
452         }
453
454     public static List<WebElement> getElementsByCSS(String cssString) throws InterruptedException {
455                 GeneralUIUtils.waitForLoader();
456                 List<WebElement> assets = getDriver().findElements(By.cssSelector(cssString));
457                 return assets;
458         }
459
460     public static WebElement getElementfromElementByCSS(WebElement parentElement, String cssString){
461         WebDriverWait wait = newWait(timeOut);
462         GeneralUIUtils.waitForLoader();
463         return parentElement.findElement(By.cssSelector(cssString));
464     }
465
466     public static WebElement getElementfromElementByXPATH(WebElement parentElement, DashboardCardEnum dataTestId){
467         WebDriverWait wait = newWait(timeOut);
468         GeneralUIUtils.waitForLoader();
469         return HighlightMyElement( parentElement.findElement(By.xpath("//*[@data-tests-id='" + dataTestId.getValue() + "']")));
470     }
471
472     public static WebElement HighlightMyElement(WebElement element) {
473            JavascriptExecutor javascript = (JavascriptExecutor) getDriver();
474            javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 4px solid yellow;");
475            return element;
476     }
477
478     public static WebElement getSelectedElementFromDropDown(String dataTestId){
479         GeneralUIUtils.ultimateWait();;
480         WebElement selectedElement = new Select (getDriver().findElement(By.xpath("//*[@data-tests-id='" + dataTestId + "']"))).getFirstSelectedOption();
481         return selectedElement;
482     }
483
484
485     public static void waitForPageLoadByReadyState() {
486         newWait(30).until((ExpectedCondition<Boolean>) wd ->
487                 ((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
488     }
489
490
491         public static boolean checkElementsCountInTable(int expectedElementsCount, Supplier<List<WebElement>> func) {
492                 int maxWaitingPeriodMS = 10 * 1000;
493                 int napPeriodMS = 100;
494                 int sumOfWaiting = 0;
495                 List<WebElement> elements = null;
496                 boolean isKeepWaiting = false;
497                 while (!isKeepWaiting) {
498                         elements = func.get();
499                         isKeepWaiting = (expectedElementsCount == elements.size());
500                         sleep(isKeepWaiting ? napPeriodMS : 0);
501                         sumOfWaiting += napPeriodMS;
502                         if (sumOfWaiting > maxWaitingPeriodMS)
503                                 return false;
504                 }
505                 return true;
506         }
507
508         public static String getActionDuration(Runnable func) throws Exception{
509                 long startTime = System.nanoTime();
510                 func.run();
511                 long estimateTime = System.nanoTime();
512             long duration = TimeUnit.NANOSECONDS.toSeconds(estimateTime - startTime);
513             String durationString = String.format("%02d:%02d", duration / 60, duration % 60);
514             return durationString;
515         }
516
517     public static WebElement clickOnAreaJS(String areaId) {
518         return clickOnAreaJS(areaId, timeOut);
519     }
520
521
522     public static WebElement clickOnAreaJS(String areaId, int timeout) {
523         try{
524                 ultimateWait();
525                 WebElement area = getWebElementByTestID(areaId);
526                 JavascriptExecutor javascript = (JavascriptExecutor) getDriver();
527                 //HighlightMyElement(area);
528                 Object executeScript = javascript.executeScript("arguments[0].click();", area, "color: yellow; border: 4px solid yellow;");
529                 waitForLoader(timeout);
530                 return area;
531         }
532         catch (Exception e){
533                 e.printStackTrace();
534         }
535                 return null;
536     }
537
538
539
540     public static WebElement clickOnAreaJS(WebElement areaId) throws InterruptedException {
541         JavascriptExecutor javascript = (JavascriptExecutor) getDriver();
542         //HighlightMyElement(area);
543         javascript.executeScript("arguments[0].click();", areaId, "color: yellow; border: 4px solid yellow;");
544         return areaId;
545     }
546
547
548
549     public static void clickSomewhereOnPage() {
550         getDriver().findElement(By.cssSelector(".asdc-app-title")).click();
551         }
552
553     public static void findComponentAndClickInCatalog(String resourceName) throws Exception {
554         // This method will find element by element name, don't use data-tests-id argument
555                 WebElement searchTextbox = GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.MainMenuButtons.SEARCH_BOX.getValue());
556                 searchTextbox.clear();
557                 searchTextbox.sendKeys(resourceName);
558                 ultimateWait();
559                 clickOnElementByText(resourceName);
560                 ultimateWait();
561         }
562
563         public static void clickOnElementByText(String textInElement) {
564                 logger.info("clickOnElementByText: {}", textInElement);
565                 WebDriverWait wait = newWait(timeOut);
566                 HighlightMyElement(wait.until(
567                                 ExpectedConditions.elementToBeClickable(findByText(textInElement)))).click();
568         }
569
570         public static void clickOnElementByText(String textInElement, int customTimeout) {
571                 logger.info("clickOnElementByText: {}", textInElement);
572                 WebDriverWait wait = newWait(customTimeout);
573                 HighlightMyElement(wait.until(ExpectedConditions.elementToBeClickable(searchByTextContaining(textInElement)))).click();
574         }
575
576         private static WebDriverWait newWait(int timeoutInSeconds) {
577                 final WebDriver driver = getDriver();
578                 driver.manage().timeouts().setScriptTimeout(timeoutInSeconds, TimeUnit.SECONDS);
579                 return new WebDriverWait(driver, timeoutInSeconds, 120);
580         }
581
582         public static void clickJSOnElementByText(String textInElement) throws Exception {
583                 WebDriverWait wait = newWait(timeOut);
584                 clickOnAreaJS(wait.until(
585                                 ExpectedConditions.elementToBeClickable(findByText(textInElement))));
586         }
587
588     public static void fluentWaitTestID(String dataTestId, String text) {
589         FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(getDriver())
590                                 .withTimeout(30, TimeUnit.SECONDS)
591                                 .pollingEvery(50, TimeUnit.MILLISECONDS)
592                                 .ignoring(NoSuchElementException.class);
593
594                 fluentWait.until(ExpectedConditions.refreshed(
595                                 ExpectedConditions.textToBePresentInElementValue(By.xpath("//*[@data-tests-id='" + dataTestId + "']"), text)));
596     }
597
598     public static void regularWait(WebElement element, String text){
599         WebDriverWait wait = newWait(timeOut);
600
601                 wait.until(ExpectedConditions.textToBePresentInElementValue(element, text));
602     }
603
604         //@Step
605     public static void waitForAngular(){
606         WebDriverWait wait = newWait(90);
607         wait.until(AdditionalConditions.pageLoadWait());
608         wait.until(AdditionalConditions.angularHasFinishedProcessing());
609     }
610
611         //@Step
612     public static void waitForAngular2(){
613         WebDriverWait wait = newWait(90);
614         wait.until(AdditionalConditions.pageLoadWait());
615         try {
616                         WebDriverWait briefWait = newWait(2);
617                         briefWait.until(AdditionalConditions.angular2HasFinishedProcessing());
618                 } catch (TimeoutException | org.openqa.selenium.ScriptTimeoutException e) {
619                         logger.info("Ignoring TimeoutException while waiting for angular2: {}", e, e);
620                 }
621     }
622
623         public static Object getAllElementAttributes(WebElement element) {
624                 return ((JavascriptExecutor)getDriver()).executeScript("var s = []; var attrs = arguments[0].attributes; for (var l = 0; l < attrs.length; ++l) { var a = attrs[l]; s.push(a.name + ':' + a.value); } ; return s;", element);
625         }
626
627     public static boolean isElementReadOnly(WebElement element){
628         try {
629                 HighlightMyElement(element).clear();
630                         return false;
631                 } catch (Exception e) {
632                         return true;
633                 }
634     }
635
636     public static boolean isElementReadOnly(String dataTestId){
637         return isElementReadOnly(
638                         waitForElementVisibilityByTestId(dataTestId));
639     }
640
641     public static boolean isElementDisabled(WebElement element){
642         return HighlightMyElement(element).getAttribute("class").contains("view-mode") ||
643                         element.getAttribute("class").contains("disabled");
644     }
645
646     public static boolean isElementDisabled(String dataTestId){
647         return isElementDisabled(
648                         waitForElementVisibilityByTestId(dataTestId));
649     }
650
651     //@Step
652     public static void ultimateWait(){
653                 logger.info("ultimateWait: starting");
654         long startTime = System.nanoTime();
655
656                 GeneralUIUtils.waitForAngular();
657                 logger.info("ultimateWait: waited for angular: {} ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
658
659                 GeneralUIUtils.waitForAngular2();
660                 logger.info("ultimateWait: waited for angular2: {} ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
661
662                 long estimateTime = System.nanoTime();
663                 long duration = TimeUnit.NANOSECONDS.toSeconds(estimateTime - startTime);
664                 if(duration > timeOut){
665                         SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Delays on page, %d seconds", duration));
666                 }
667
668                 logger.info("ultimateWait: done");
669     }
670
671     public static WebElement makeElementVisibleWithJS(WebElement element){
672         String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
673         ((JavascriptExecutor) getDriver()).executeScript(js, element);
674         return element;
675     }
676
677     public static WebElement unhideElement(WebElement element, String attributeValue){
678         String js = "arguments[0].setAttribute('class','" + attributeValue + "');";
679         ((JavascriptExecutor) getDriver()).executeScript(js, element);
680         return element;
681     }
682
683     public static WebElement findByText(String textInElement){
684         return getDriver().findElement(searchByTextContaining(textInElement));
685     }
686
687         public static List<WebElement> findElementsByText(String textInElement){
688                 return getDriver().findElements(searchByTextContaining(textInElement));
689         }
690
691     public static By searchByTextContaining(String textInElement) {
692                 return By.xpath("//*[contains(text(),'" + textInElement + "')]");
693         }
694
695
696     public static boolean findAndWaitByText(String textInElement, int timeout){
697                 logger.info("findAndWaitByText: {}", textInElement);
698         try{
699                 WebDriverWait wait = newWait(timeout);
700                 wait.until(ExpectedConditions.presenceOfElementLocated(searchByTextContaining(textInElement)));
701                 return true;
702         }
703         catch(Exception e){
704                 return false;
705         }
706     }
707
708     public static WebElement getClickableButtonBy(By by, int timout){
709         try{
710                 WebDriverWait wait = newWait(timout);
711                 WebElement element = wait.until(ExpectedConditions.elementToBeClickable(by));
712                 return element;
713         }
714         catch(Exception e){
715                 return null;
716         }
717     }
718
719
720
721     public static WebElement getButtonWithText(String textInButton){
722         try{
723                 return getDriver().findElement(By.xpath("//button[contains(text(),'" + textInButton + "')]"));
724         }
725         catch(Exception e)
726         {
727                 return null;
728         }
729     }
730
731
732     public static List<WebElement> getElementsByDataTestsIdStartWith(String startWithString){
733         ultimateWait();
734         return getDriver().findElements(By.xpath("//*[starts-with(@data-tests-id,'" + startWithString + "')]"));
735     }
736
737         public static void closeErrorMessage() {
738                 WebElement okWebElement = getButtonWithText("OK");
739                 if (okWebElement != null){
740                         okWebElement.click();
741                         ultimateWait();
742                 }
743         }
744
745     public static WebElement getElementByCSS(String cssString) throws InterruptedException {
746                 ultimateWait();
747                 return getDriver().findElement(By.cssSelector(cssString));
748         }
749
750     public static String getDataTestIdAttributeValue(WebElement element) {
751                 return element.getAttribute("data-tests-id");
752         }
753
754     public static String getTextContentAttributeValue(WebElement element) {
755                 return element.getAttribute("textContent");
756         }
757
758     public static WebElement getElementInsideElementByDataTestsId(WebElement element, String dataTestId) {
759         try{
760                 return element.findElement(By.xpath("//*[@data-tests-id='" + dataTestId + "']"));
761         }
762         catch(Exception e){
763                 return null;
764         }
765         }
766
767     public static void clickOnElementByCSS(String cssString) throws Exception {
768                 WebDriverWait wait = newWait(timeOut);
769                 wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssString))).click();
770                 ultimateWait();
771         }
772         public static String getRandomComponentName(String prefix) {
773                 return prefix + GeneralUIUtils.randomNumber();
774         }
775         public static int randomNumber() {
776                 Random r = new Random();
777                 return r.nextInt(10000);
778         }
779
780         public static void waitForUINotification() {
781                 List<WebElement> notificationElements = getDriver().findElements(By.className("ui-notification"));
782                 if (!notificationElements.isEmpty()){
783                         notificationElements.forEach(WebElement::click);
784                 }
785         }
786
787         public static boolean checkForDisabledAttribute(String  dataTestId){
788                 Object elementAttributes = getAllElementAttributes(waitForElementVisibilityByTestId(dataTestId));
789                 return elementAttributes.toString().contains("disabled");
790         }
791
792         public static void dragAndDropElementByY(WebElement area, int yOffset) {
793                 Actions actions = new Actions(getDriver());
794                 actions.dragAndDropBy(area, 10, yOffset).perform();
795                 ultimateWait();
796         }
797
798         public static void waitForBackLoader() {
799                 waitForBackLoader(timeOut);
800         }
801
802         public static void waitForBackLoader(int timeOut) {
803                 sleep(1);
804                 waitForElementInVisibilityBy(By.className("tlv-loader-back"), timeOut);
805         }
806
807 }