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