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