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