Fix to stabilize sanity ui test suite
[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 com.aventstack.extentreports.Status;
24 import org.apache.commons.io.FileUtils;
25 import org.openecomp.sdc.ci.tests.datatypes.DataTestIdEnum;
26 import org.openecomp.sdc.ci.tests.exception.GeneralUiRuntimeException;
27 import org.openecomp.sdc.ci.tests.execute.setup.DriverFactory;
28 import org.openecomp.sdc.ci.tests.pages.HomePage;
29 import org.openecomp.sdc.ci.tests.utils.Utils;
30 import org.openqa.selenium.By;
31 import org.openqa.selenium.JavascriptExecutor;
32 import org.openqa.selenium.Keys;
33 import org.openqa.selenium.NoSuchElementException;
34 import org.openqa.selenium.OutputType;
35 import org.openqa.selenium.TakesScreenshot;
36 import org.openqa.selenium.WebDriver;
37 import org.openqa.selenium.WebElement;
38 import org.openqa.selenium.firefox.FirefoxDriver;
39 import org.openqa.selenium.interactions.Actions;
40 import org.openqa.selenium.support.ui.ExpectedConditions;
41 import org.openqa.selenium.support.ui.Select;
42 import org.openqa.selenium.support.ui.WebDriverWait;
43
44 import java.awt.Toolkit;
45 import java.awt.datatransfer.Clipboard;
46 import java.awt.datatransfer.StringSelection;
47 import java.io.File;
48 import java.io.IOException;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.UUID;
52 import java.util.concurrent.TimeUnit;
53 import java.util.function.Supplier;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 import static org.openecomp.sdc.ci.tests.execute.setup.SetupCDTest.getExtendTest;
58 import static org.testng.AssertJUnit.assertTrue;
59
60
61 public final class GeneralUIUtils {
62
63     private static final Logger LOGGER = LoggerFactory.getLogger(GeneralUIUtils.class);
64
65     private static final String DATA_TESTS_ID = "//*[@data-tests-id='%1$s' or @data-test-id='%1$s']";
66     private static final String COLOR_YELLOW_BORDER_4PX_SOLID_YELLOW = "color: yellow; border: 4px solid yellow;";
67
68     private static final int TIME_OUT = (int) (60 * 1.5);
69     private static final int WEB_DRIVER_WAIT_TIME_OUT = 10;
70     private static final int SLEEP_DURATION = 1000;
71     private static final int MAX_WAITING_PERIOD = 10 * 1000;
72     private static final int NAP_PERIOD = 100;
73     private static final int DURATION_FORMATIN = 60;
74
75     private GeneralUIUtils () {
76
77     }
78
79     public static int getTimeOut() {
80         return TIME_OUT;
81     }
82
83     public static WebDriver getDriver() {
84         return DriverFactory.getDriver();
85     }
86
87     public static List<WebElement> getElementsByLocator(By by) {
88         return getDriver().findElements(by);
89     }
90
91     public static File takeScreenshot(String screenshotFilename, String dir, String testName) throws IOException {
92         if (screenshotFilename == null) {
93             if (testName != null) {
94                 screenshotFilename = testName;
95             } else {
96                 screenshotFilename = UUID.randomUUID().toString();
97             }
98         }
99         try {
100             File scrFile = ((TakesScreenshot) getDriver()).getScreenshotAs(OutputType.FILE);
101             File filePath = new File(String.format("%s/%s.png", dir, screenshotFilename));
102             new File(dir).mkdirs();
103             FileUtils.copyFile(scrFile, filePath);
104             return filePath;
105         } catch (IOException e1) {
106             e1.printStackTrace();
107         }
108         return null;
109     }
110
111     public static File takeScreenshot(String screenshotFilename, String dir) throws IOException {
112         return takeScreenshot(screenshotFilename, dir, null);
113     }
114
115     public static WebElement getWebElementByTestID(String dataTestId) {
116         return getWebElementByTestID(dataTestId, TIME_OUT);
117     }
118
119     public static WebElement getWebElementByTestID(final String dataTestId, final int timeout) {
120         final WebDriverWait wait = new WebDriverWait(getDriver(), timeout);
121         return wait
122             .until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId))));
123     }
124
125     public static boolean isWebElementExistByTestId(String dataTestId) {
126         return getDriver().findElements(By.xpath(String.format(DATA_TESTS_ID, dataTestId))).size() != 0;
127     }
128
129     public static boolean isWebElementExistByClass(String className) {
130         return getDriver().findElements(By.className(className)).size() != 0;
131     }
132
133     public static WebElement getInputElement(String dataTestId) {
134         try {
135             ultimateWait();
136             return getDriver().findElement(By.xpath(String.format(DATA_TESTS_ID, dataTestId)));
137         } catch (Exception e) {
138             return null;
139         }
140     }
141
142     public static List<WebElement> getInputElements(String dataTestId) {
143         ultimateWait();
144         return getDriver().findElements(By.xpath(String.format(DATA_TESTS_ID, dataTestId)));
145
146     }
147
148     public static WebElement getWebElementBy(By by) {
149         return getWebElementBy(by, TIME_OUT);
150     }
151
152     public static WebElement getWebElementBy(By by, int timeOut) {
153         WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
154         return wait.until(ExpectedConditions.visibilityOfElementLocated(by));
155     }
156
157     public static WebElement getWebElementByPresence(By by, int timeOut) {
158         WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
159         return wait.until(ExpectedConditions.presenceOfElementLocated(by));
160     }
161
162     public static List<String> getWebElementListText(List<WebElement> elements) {
163         List<String> Text = new ArrayList<>();
164         for (WebElement webElement : elements) {
165             Text.add(webElement.getText());
166         }
167         return Text;
168     }
169
170     public static List<WebElement> getWebElementsListBy(By by) {
171         return getWebElementsListBy(by, TIME_OUT);
172     }
173
174     public static List<WebElement> getWebElementsListBy(By by, int timeOut) {
175         WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
176         return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by));
177     }
178
179     public static List<WebElement> getWebElementsListByContainTestID(String dataTestId) {
180         try {
181             WebDriverWait wait = new WebDriverWait(getDriver(), WEB_DRIVER_WAIT_TIME_OUT);
182             return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(String.format("//*[contains(@data-tests-id, '%1$s') or contains(@data-test-id, '%1$s')]", dataTestId))));
183         } catch (Exception e) {
184             return new ArrayList<WebElement>();
185         }
186     }
187
188     public static List<WebElement> getWebElementsListByContainsClassName(String containedText) {
189         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
190         return wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[contains(@class, '" + containedText + "')]")));
191     }
192
193     public static WebElement getWebElementByContainsClassName(String containedText) {
194         return getWebElementBy(By.xpath("//*[contains(@class, '" + containedText + "')]"));
195     }
196
197     public static WebElement getWebElementByClassName(String className) {
198         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
199         return wait.until(ExpectedConditions.visibilityOfElementLocated(By.className(className)));
200     }
201
202     public static List<WebElement> getWebElementsListByTestID(String dataTestId) {
203         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
204         return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(String.format(DATA_TESTS_ID, dataTestId))));
205     }
206
207     public static List<WebElement> getWebElementsListByClassName(String className) {
208         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
209         return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.className(className)));
210     }
211
212
213     public static Boolean isElementInvisibleByTestId(String dataTestId) {
214         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
215         return wait.until(
216                 ExpectedConditions.invisibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId))));
217     }
218
219     public static Boolean isElementVisibleByTestId(String dataTestId) {
220         try {
221             WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
222             return wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath(String.format(DATA_TESTS_ID, dataTestId))))).isDisplayed();
223         } catch (Exception e) {
224             return false;
225         }
226     }
227
228     public static void clickOnElementByTestId(String dataTestId) {
229         LOGGER.debug("Clicking on the element by test id " + dataTestId);
230         clickOnElementByTestIdWithoutWait(dataTestId);
231         LOGGER.debug("Waiting after clicking element by test id " + dataTestId);
232         ultimateWait();
233         LOGGER.debug(String.format("Waiting after clicking element by test id '%s' finished", dataTestId));
234     }
235
236     public static void clickOnElementChildByTestId(String dataTestId) {
237         clickOnElementChildByTestIdWithoutWait(dataTestId);
238         ultimateWait();
239     }
240
241     public static void clickOnElementByTestIdWithoutWait(final String dataTestId) {
242         final WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
243         wait
244             .until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(DATA_TESTS_ID, dataTestId)))).click();
245     }
246
247     public static void clickOnElementChildByTestIdWithoutWait(String dataTestId) {
248         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
249         wait.until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(DATA_TESTS_ID, dataTestId) + "//*"))).click();
250     }
251
252     public static void clickOnElementByTestId(String dataTestId, int customTimeout) {
253         WebDriverWait wait = new WebDriverWait(getDriver(), customTimeout);
254         wait.until(ExpectedConditions.elementToBeClickable(By.xpath(String.format(DATA_TESTS_ID, dataTestId)))).click();
255     }
256
257     public static WebElement waitForElementVisibilityByTestId(String dataTestId) {
258         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
259         return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId))));
260     }
261
262     public static Boolean waitForElementInVisibilityByTestId(String dataTestId) {
263         return waitForElementInVisibilityByTestId(dataTestId, TIME_OUT);
264     }
265
266     public static Boolean waitForElementInVisibilityByTestId(String dataTestId, int timeOut) {
267         WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
268         boolean displayed = getDriver().findElements(By.xpath(String.format(DATA_TESTS_ID, dataTestId))).isEmpty();
269         if (!displayed) {
270             Boolean until = wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath(String.format(DATA_TESTS_ID, dataTestId))));
271             ultimateWait();
272             return until;
273         }
274         return false;
275     }
276
277     public static Boolean waitForElementInVisibilityByTestId(By by) {
278         return waitForElementInVisibilityBy(by, TIME_OUT);
279     }
280
281
282     public static Boolean waitForElementInVisibilityBy(By by, int timeOut) {
283         WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
284         boolean displayed = getDriver().findElements(by).isEmpty();
285         if (!displayed) {
286             Boolean until = wait.until(ExpectedConditions.invisibilityOfElementLocated(by));
287             sleep(SLEEP_DURATION);
288             return until;
289         }
290         return false;
291     }
292
293
294     public static void setWebElementByTestId(String elemetID, String value) {
295         WebElement resourceDescriptionTextbox = GeneralUIUtils.getWebElementByTestID(elemetID);
296         resourceDescriptionTextbox.clear();
297         resourceDescriptionTextbox.sendKeys(value);
298
299     }
300
301     public static WebElement hoverOnAreaByTestId(String areaId) {
302         Actions actions = new Actions(getDriver());
303         WebElement area = getWebElementByTestID(areaId);
304         actions.moveToElement(area).perform();
305         ultimateWait();
306         return area;
307     }
308
309     public static WebElement hoverOnAreaByClassName(String className) {
310         Actions actions = new Actions(getDriver());
311         WebElement area = getWebElementByClassName(className);
312         actions.moveToElement(area).perform();
313         GeneralUIUtils.ultimateWait();
314         return area;
315     }
316
317     public static void waitForLoader() {
318         waitForLoader(TIME_OUT);
319     }
320
321     public static void waitForLoader(int timeOut) {
322         final String loaderClass = "tlv-loader";
323         final int sleepDuration = 500;
324         sleep(sleepDuration);
325         LOGGER.debug("Waiting {}s for '.{}'", timeOut, loaderClass);
326         waitForElementInVisibilityBy(By.className(loaderClass), timeOut);
327     }
328
329     public static void findComponentAndClick(final String resourceName) {
330         HomePage.findComponentAndClick(resourceName);
331     }
332
333     public static void windowZoomOut() {
334         final int zoomOutFactor = 3;
335         for (int i = 0; i < zoomOutFactor; i++) {
336             if (getDriver() instanceof FirefoxDriver) {
337                 getDriver().findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
338             }
339         }
340     }
341
342     public static void resetZoom() {
343         getDriver().findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, "0"));
344     }
345
346     public static void windowZoomOutUltimate() {
347         resetZoom();
348         windowZoomOut();
349     }
350
351     public static void sleep(int duration) {
352         try {
353             Thread.sleep(duration);
354         } catch (final InterruptedException e) {
355             Thread.currentThread().interrupt();
356             throw new GeneralUiRuntimeException("The thread was interrupted during a sleep", e);
357         }
358     }
359
360     public static void moveToStep(final DataTestIdEnum.StepsEnum stepName) {
361         getExtendTest().log(Status.INFO, String.format("Going to %s page ", stepName.toString()));
362         moveToStep(stepName.getValue());
363     }
364
365     public static void moveToStep(final String dataTestId) {
366         clickOnElementChildByTestId(dataTestId);
367     }
368
369
370     public static Select getSelectList(String item, String datatestsid) {
371         Select selectList = new Select(getWebElementByTestID(datatestsid));
372         if (item != null) {
373             selectList.selectByVisibleText(item);
374         }
375         return selectList;
376     }
377
378     public static List<WebElement> getElementsByCSS(String cssString) /*throws InterruptedException*/ {
379         GeneralUIUtils.waitForLoader();
380         return getDriver().findElements(By.cssSelector(cssString));
381     }
382
383     public static WebElement getElementfromElementByCSS(WebElement parentElement, String cssString) {
384         GeneralUIUtils.waitForLoader();
385         return parentElement.findElement(By.cssSelector(cssString));
386     }
387
388     private static WebElement highlightMyElement(WebElement element) {
389         JavascriptExecutor javascript = (JavascriptExecutor) getDriver();
390         javascript.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, COLOR_YELLOW_BORDER_4PX_SOLID_YELLOW);
391         return element;
392     }
393
394     public static WebElement getSelectedElementFromDropDown(String dataTestId) {
395         GeneralUIUtils.ultimateWait();
396         return new Select(getDriver().findElement(By.xpath(String.format(DATA_TESTS_ID, dataTestId)))).getFirstSelectedOption();
397     }
398
399     public static boolean checkElementsCountInTable(int expectedElementsCount, Supplier<List<WebElement>> func) {
400         int maxWaitingPeriodMS = MAX_WAITING_PERIOD;
401         int napPeriodMS = NAP_PERIOD;
402         int sumOfWaiting = 0;
403         List<WebElement> elements;
404         boolean isKeepWaiting = false;
405         while (!isKeepWaiting) {
406             elements = func.get();
407             isKeepWaiting = (expectedElementsCount == elements.size());
408             sleep(napPeriodMS);
409             sumOfWaiting += napPeriodMS;
410             if (sumOfWaiting > maxWaitingPeriodMS) {
411                 return false;
412             }
413         }
414         return true;
415     }
416
417     public static String getActionDuration(Runnable func) throws Exception {
418         long startTime = System.nanoTime();
419         func.run();
420         long estimateTime = System.nanoTime();
421         long duration = TimeUnit.NANOSECONDS.toSeconds(estimateTime - startTime);
422         return String.format("%02d:%02d", duration / DURATION_FORMATIN, duration % DURATION_FORMATIN);
423     }
424
425     public static WebElement clickOnAreaJS(String areaId) {
426         return clickOnAreaJS(areaId, TIME_OUT);
427     }
428
429
430     public static WebElement clickOnAreaJS(String areaId, int timeout) {
431         try {
432             ultimateWait();
433             WebElement area = getWebElementByTestID(areaId);
434             JavascriptExecutor javascript = (JavascriptExecutor) getDriver();
435             Object executeScript = javascript.executeScript("arguments[0].click();", area, COLOR_YELLOW_BORDER_4PX_SOLID_YELLOW);
436             waitForLoader(timeout);
437             ultimateWait();
438             return area;
439         } catch (Exception e) {
440             e.printStackTrace();
441         }
442         return null;
443     }
444
445
446     public static WebElement clickOnAreaJS(WebElement areaId) throws InterruptedException {
447         JavascriptExecutor javascript = (JavascriptExecutor) getDriver();
448         javascript.executeScript("arguments[0].click();", areaId, COLOR_YELLOW_BORDER_4PX_SOLID_YELLOW);
449         return areaId;
450     }
451
452
453     public static void clickSomewhereOnPage() {
454         getDriver().findElement(By.cssSelector(".asdc-app-title")).click();
455     }
456
457     public static void clickOnElementByText(String textInElement) {
458         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
459         highlightMyElement(wait.until(
460                 ExpectedConditions.elementToBeClickable(findByText(textInElement)))).click();
461     }
462
463     public static void clickOnElementByText(String textInElement, int customTimeout) {
464         WebDriverWait wait = new WebDriverWait(getDriver(), customTimeout);
465         highlightMyElement(wait.until(
466                 ExpectedConditions.elementToBeClickable(findByText(textInElement)))).click();
467     }
468
469     public static void clickJSOnElementByText(String textInElement) throws Exception {
470         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
471         clickOnAreaJS(wait.until(
472                 ExpectedConditions.elementToBeClickable(findByText(textInElement))));
473     }
474
475     public static void waitForAngular() {
476         LOGGER.debug("Waiting for angular");
477         final int webDriverWaitingTime = 90;
478         WebDriverWait wait = new WebDriverWait(getDriver(), webDriverWaitingTime, NAP_PERIOD);
479         wait.until(AdditionalConditions.pageLoadWait());
480         wait.until(AdditionalConditions.angularHasFinishedProcessing());
481         LOGGER.debug("Waiting for angular finished");
482     }
483
484     public static Object getAllElementAttributes(WebElement element) {
485         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);
486     }
487
488     public static boolean isElementReadOnly(WebElement element) {
489         try {
490             highlightMyElement(element).clear();
491             return false;
492         } catch (Exception e) {
493             return true;
494         }
495     }
496
497     public static boolean isElementReadOnly(String dataTestId) {
498         return isElementReadOnly(
499                 waitForElementVisibilityByTestId(dataTestId));
500     }
501
502     public static boolean isElementDisabled(WebElement element) {
503         return highlightMyElement(element).getAttribute("class").contains("view-mode")
504                 || element.getAttribute("class").contains("disabled") || element.getAttribute("disabled") != null;
505     }
506
507     public static boolean isElementDisabled(String dataTestId) {
508         return isElementDisabled(
509                 waitForElementVisibilityByTestId(dataTestId));
510     }
511
512     public static void ultimateWait() {
513         long startTime = System.nanoTime();
514
515         GeneralUIUtils.waitForLoader();
516         GeneralUIUtils.waitForBackLoader();
517         GeneralUIUtils.waitForAngular();
518
519         long estimateTime = System.nanoTime();
520         long duration = TimeUnit.NANOSECONDS.toSeconds(estimateTime - startTime);
521         if (duration > TIME_OUT) {
522             getExtendTest().log(Status.WARNING, String.format("Delays on page, %d seconds", duration));
523         }
524     }
525
526     public static WebElement unhideElement(WebElement element, String attributeValue) {
527         String js = "arguments[0].setAttribute('class','" + attributeValue + "');";
528         ((JavascriptExecutor) getDriver()).executeScript(js, element);
529         return element;
530     }
531
532     public static WebElement findByText(String textInElement) {
533         return getDriver().findElement(searchByTextContaining(textInElement));
534     }
535
536     public static By searchByTextContaining(String textInElement) {
537         return By.xpath("//*[contains(text(),'" + textInElement + "')]");
538     }
539
540     public static WebElement getClickableButtonBy(By by, int timout) {
541         try {
542             WebDriverWait wait = new WebDriverWait(getDriver(), timout);
543             return wait.until(ExpectedConditions.elementToBeClickable(by));
544         } catch (Exception e) {
545             return null;
546         }
547     }
548
549
550     public static WebElement getButtonWithText(String textInButton) {
551         try {
552             return getDriver().findElement(By.xpath("//button[contains(text(),'" + textInButton + "')]"));
553         } catch (Exception e) {
554             return null;
555         }
556     }
557
558     public static void closeErrorMessage() {
559         WebElement okWebElement = getButtonWithText("OK");
560         if (okWebElement != null) {
561             okWebElement.click();
562             ultimateWait();
563         }
564     }
565
566     public static WebElement getElementByCSS(String cssString) throws InterruptedException {
567         ultimateWait();
568         return getDriver().findElement(By.cssSelector(cssString));
569     }
570
571     public static String getDataTestIdAttributeValue(WebElement element) {
572         return element.getAttribute("data-tests-id");
573     }
574
575     public static String getTextContentAttributeValue(WebElement element) {
576         return element.getAttribute("textContent");
577     }
578
579     public static void clickOnElementByCSS(String cssString) throws Exception {
580         WebDriverWait wait = new WebDriverWait(getDriver(), TIME_OUT);
581         wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(cssString))).click();
582         ultimateWait();
583     }
584
585     public static boolean checkForDisabledAttribute(String dataTestId) {
586         Object elementAttributes = getAllElementAttributes(waitForElementVisibilityByTestId(dataTestId));
587         return elementAttributes.toString().contains("disabled");
588     }
589
590     public static void dragAndDropElementByY(WebElement area, int yOffset) {
591         final int dragAndDropTimeout = 10;
592         Actions actions = new Actions(getDriver());
593         actions.dragAndDropBy(area, dragAndDropTimeout, yOffset).perform();
594         ultimateWait();
595     }
596
597     public static void waitForBackLoader() {
598         waitForBackLoader(TIME_OUT);
599     }
600
601     public static void waitForBackLoader(int timeOut) {
602         sleep(NAP_PERIOD);
603         final String backLoaderClass = "tlv-loader-back";
604         LOGGER.debug("Waiting {}s for '.{}'", timeOut, backLoaderClass);
605         waitForElementInVisibilityBy(By.className(backLoaderClass), timeOut);
606     }
607
608     public static void addStringtoClipboard(String text) {
609         StringSelection selection = new StringSelection(text);
610         Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
611         clipboard.setContents(selection, selection);
612     }
613
614     public static boolean checkForDisabledAttributeInHiddenElement(String cssString) {
615         final int numberOfDisableElements = 3;
616         boolean isDisabled = false;
617         for (int i = 0; i < numberOfDisableElements; i++) {
618             Object elementAttributes = getAllElementAttributes(getWebElementByPresence(By.cssSelector(cssString), TIME_OUT));
619             isDisabled = elementAttributes.toString().contains("disabled");
620             if (isDisabled) {
621                 break;
622             }
623             ultimateWait();
624         }
625         return isDisabled;
626     }
627
628     public static void selectByValueTextContained(String dataTestsId, String value) {
629
630         List<WebElement> options = GeneralUIUtils.getWebElementsListBy(By.xpath(String.format("//select[@data-tests-id='%1$s' or @data-test-id='%1$s']//option[contains(@value,'%2$s')]", dataTestsId, value)));
631
632         boolean matched = false;
633         for (WebElement option : options) {
634             option.click();
635             matched = true;
636         }
637
638         if (!matched) {
639             throw new NoSuchElementException("Cannot locate option with value: " + value);
640         }
641
642         ultimateWait();
643     }
644
645     public static void setTextInElementByXpath(String xPath, String text) {
646         WebElement webElement = GeneralUIUtils.getWebElementBy(By.xpath(xPath));
647         webElement.clear();
648         webElement.click();
649         webElement.sendKeys(text);
650         ultimateWait();
651     }
652
653
654     public static void clickOnElementByXpath(String xPath) {
655         WebElement webElement = GeneralUIUtils.getWebElementBy(By.xpath(xPath));
656         webElement.click();
657         ultimateWait();
658     }
659
660     public static String getTextValueFromWebElementByXpath(String xpath) {
661         WebElement webElement = getWebElementBy(By.xpath(xpath));
662         return webElement.getAttribute("value");
663     }
664
665     public static List<WebElement> findElementsByXpath(String xPath) {
666         return getDriver().findElements(By.xpath(xPath));
667     }
668
669     public static void clickOnBrowserBackButton() throws Exception {
670         getExtendTest().log(Status.INFO, "Going to press on back browser button.");
671         getDriver().navigate().back();
672         ultimateWait();
673     }
674
675     public static String copyCurrentURL() throws Exception {
676         getExtendTest().log(Status.INFO, "Copying current URL");
677         return getDriver().getCurrentUrl();
678     }
679
680     public static void navigateToURL(String url) throws Exception {
681         getExtendTest().log(Status.INFO, "Navigating to URL " + url);
682         getDriver().navigate().to(url);
683     }
684
685     public static void refreshWebpage() throws Exception {
686         getExtendTest().log(Status.INFO, "Refreshing Webpage");
687         getDriver().navigate().refresh();
688         ultimateWait();
689     }
690
691     public static Object getElementPositionOnCanvas(String elementName) {
692         String scriptJS = "var cy = window.jQuery('.sdc-composition-graph-wrapper').cytoscape('get');\n"
693                 + "var n = cy.nodes('[name=\"" + elementName + "\"]');\n"
694                 + "var nPos = n.renderedPosition();\n"
695                 + "return JSON.stringify({\n"
696                 + "\tx: nPos.x,\n"
697                 + "\ty: nPos.y\n"
698                 + "})";
699         return ((JavascriptExecutor) getDriver()).executeScript(scriptJS);
700     }
701
702     public static Object getElementGreenDotPositionOnCanvas(String elementName) {
703         String scriptJS = "var cy = window.jQuery('.sdc-composition-graph-wrapper').cytoscape('get');\n"
704                 + "var cyZoom = cy.zoom();\n"
705                 + "var n = cy.nodes('[name=\"" + elementName + "\"]');\n"
706                 + "var nPos = n.renderedPosition();\n"
707                 + "var nData = n.data();\n"
708                 + "var nImgSize = nData.imgWidth;\n"
709                 + "var shiftSize = (nImgSize-18)*cyZoom/2;\n"
710                 + "return JSON.stringify({\n"
711                 + "\tx: nPos.x + shiftSize,\n"
712                 + "\ty: nPos.y - shiftSize\n"
713                 + "});";
714         return ((JavascriptExecutor) getDriver()).executeScript(scriptJS);
715     }
716
717     public static Long getAndValidateActionDuration(Runnable action, int regularTestRunTime) {
718         Long actualTestRunTime = null;
719         try {
720             actualTestRunTime = Utils.getActionDuration(() -> {
721                 try {
722                     action.run();
723                 } catch (Throwable throwable) {
724                     throwable.printStackTrace();
725                 }
726             });
727         } catch (Exception e) {
728             e.printStackTrace();
729         }
730         final double factor = 1.5;
731
732         assertTrue("Expected test run time should be less than " + regularTestRunTime * factor + ", "
733                 + "actual time is " + actualTestRunTime, regularTestRunTime * factor > actualTestRunTime);
734          //SetupCDTest.getExtendTest().log(Status.INFO, "Actual catalog loading time is  " + actualTestRunTime + " seconds");
735         return actualTestRunTime;
736     }
737 }