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