[SDC-29] rebase continue work to align source
[sdc.git] / ui-ci-dev / src / main / java / org / openecomp / sdc / uici / tests / utilities / GeneralUIUtils.java
1 package org.openecomp.sdc.uici.tests.utilities;
2
3 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.retryMethodOnException;
4 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.retryMethodOnResult;
5 import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.swallowException;
6
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.util.List;
10 import java.util.function.Function;
11 import java.util.function.Supplier;
12
13 import org.openecomp.sdc.uici.tests.datatypes.CreateAndUpdateStepsEnum;
14 import org.openecomp.sdc.uici.tests.datatypes.DataTestIdEnum;
15 import org.openecomp.sdc.uici.tests.datatypes.DataTestIdEnum.Dashboard;
16 import org.openecomp.sdc.uici.tests.execute.base.SetupCDTest;
17 import org.openqa.selenium.By;
18 import org.openqa.selenium.Platform;
19 import org.openqa.selenium.WebDriver;
20 import org.openqa.selenium.WebElement;
21 import org.openqa.selenium.firefox.FirefoxDriver;
22 import org.openqa.selenium.interactions.Actions;
23 import org.openqa.selenium.remote.DesiredCapabilities;
24 import org.openqa.selenium.remote.RemoteWebDriver;
25 import org.openqa.selenium.support.ui.ExpectedCondition;
26 import org.openqa.selenium.support.ui.ExpectedConditions;
27 import org.openqa.selenium.support.ui.Select;
28 import org.openqa.selenium.support.ui.WebDriverWait;
29 import org.testng.Assert;
30
31 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
32
33 public final class GeneralUIUtils {
34
35         private static final int DEFAULT_WAIT_TIME_IN_SECONDS = 10;
36         /**************** DRIVERS ****************/
37         private static WebDriver driver;
38
39         private GeneralUIUtils() {
40                 throw new UnsupportedOperationException();
41         }
42         
43         /**
44          * Finding a component in the home screen by name and clicks on it
45          * Uses the search 
46          * 
47          * @param componentName
48          * @throws Exception
49          */
50         public static void findComponentAndClick(String componentName) throws Exception {
51                 getWebElementWaitForVisible("main-menu-input-search").sendKeys(componentName);
52                 try {
53                         getWebElementWaitForClickable(componentName).click();
54                         GeneralUIUtils.waitForLoader();
55                         getWebElementWaitForVisible("formlifecyclestate");
56                 } catch (Exception e) {
57                         String msg = String.format("DID NOT FIND A COMPONENT NAMED %s", componentName);
58                         System.out.println(msg);
59                         Assert.fail(msg);
60                 }
61         }
62
63         public static WebElement getWebElementWaitForVisible(String dataTestId) {
64                 return getWebElementWaitForVisible(dataTestId, DEFAULT_WAIT_TIME_IN_SECONDS);
65         }
66
67         public static WebElement getWebElementWaitForVisible(String dataTestId, int time) {
68                 WebDriverWait wait = new WebDriverWait(getDriver(), time);
69                 ExpectedCondition<WebElement> visibilityOfElementLocated = ExpectedConditions
70                                 .visibilityOfElementLocated(builDataTestIdLocator(dataTestId));
71                 WebElement webElement = wait.until(visibilityOfElementLocated);
72                 return webElement;
73         }
74
75         public static WebElement getWebElementWaitForClickable(String dataTestId) {
76                 WebDriverWait wait = new WebDriverWait(getDriver(), DEFAULT_WAIT_TIME_IN_SECONDS);
77                 ExpectedCondition<WebElement> condition = ExpectedConditions
78                                 .elementToBeClickable(builDataTestIdLocator(dataTestId));
79                 WebElement webElement = wait.until(condition);
80                 return webElement;
81         }
82
83         private static By builDataTestIdLocator(String dataTestId) {
84                 return By.xpath("//*[@data-tests-id='" + dataTestId + "']");
85
86         }
87
88         /**
89          * Returns A list of Web Elements When they are all visible
90          * 
91          * @param dataTestId
92          * @return
93          */
94         public static List<WebElement> getWebElementsListWaitForVisible(String dataTestId) {
95                 WebDriverWait wait = new WebDriverWait(getDriver(), DEFAULT_WAIT_TIME_IN_SECONDS);
96                 ExpectedCondition<List<WebElement>> visibilityOfAllElementsLocatedBy = ExpectedConditions
97                                 .visibilityOfAllElementsLocatedBy(builDataTestIdLocator(dataTestId));
98                 return wait.until(visibilityOfAllElementsLocatedBy);
99         }
100
101         /**
102          * @deprecated Do not use. use {@link #getWebElementWaitForVisible(String)}
103          * @param dataTestId
104          * @return
105          */
106         public static WebElement getWebElementByDataTestId(String dataTestId) {
107                 return driver.findElement(builDataTestIdLocator(dataTestId));
108         }
109
110         /**
111          * Checks if element is present with given dataTestsId
112          * 
113          * @param dataTestId
114          * @return
115          */
116         public static boolean isElementPresent(String dataTestId) {
117                 final boolean isPresent = !driver.findElements(builDataTestIdLocator(dataTestId)).isEmpty();
118                 return isPresent;
119         }
120
121         public static void clickOnCreateEntityFromDashboard(String buttonId) {
122                 Supplier<WebElement> addVfButtonSipplier = () -> {
123                         // TODO ui-ci replace with data-test-id
124                         GeneralUIUtils.moveToHTMLElementByClassName("w-sdc-dashboard-card-new");
125                         return GeneralUIUtils.getWebElementByDataTestId(buttonId);
126                 };
127                 WebElement addVfButton = FunctionalInterfaces.retryMethodOnException(addVfButtonSipplier);
128                 addVfButton.click();
129         }
130
131         // this function located select list by the data-test-id value and the item
132         // to be selected..
133         public static Select getSelectList(String item, String dataTestId) {
134                 Select selectlist = new Select(driver.findElement(builDataTestIdLocator(dataTestId)));
135                 if (item != null) {
136                         selectlist.selectByVisibleText(item);
137                 }
138                 return selectlist;
139         }
140
141         // Define description area .
142         public static String defineDescription(String descriptionText) {
143
144                 WebElement resourceDescriptionTextbox = GeneralUIUtils.getWebElementWaitForVisible("description");
145                 resourceDescriptionTextbox.clear();
146                 resourceDescriptionTextbox.sendKeys(descriptionText);
147
148                 return descriptionText;
149         }
150
151         /**
152          * Clicks on the create button waits for the create to finish and the check
153          * in button to appear
154          */
155         public static void clickCreateButton() {
156                 GeneralUIUtils.waitForLoader();
157                 getWebElementWaitForClickable(DataTestIdEnum.LifeCyleChangeButtons.CREATE.getValue()).click();
158                 GeneralUIUtils.waitForLoader();
159                 closeNotificatin();
160                 getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.CHECK_IN.getValue());
161         }
162         
163         public static void closeNotificatin() {
164                 WebElement notification = driver.findElement(By.className("ui-notification"));
165                 if (notification != null) {
166                         notification.click();
167                 }
168         }
169
170         public static void clickSaveButton() {
171                 WebElement createButton = getWebElementWaitForClickable(DataTestIdEnum.LifeCyleChangeButtons.CREATE.getValue());
172                 createButton.click();
173         }
174
175         public static void checkIn() {
176                 waitForLoader();
177                 getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.CHECK_IN.getValue()).click();
178                 getWebElementWaitForVisible(DataTestIdEnum.ModalItems.ACCEP_TESTING_MESSAGE.getValue()).sendKeys("Check in !");
179                 getWebElementWaitForVisible(DataTestIdEnum.ModalItems.OK.getValue()).click();
180                 waitForLoader();
181         }
182
183         public static void moveToStep(CreateAndUpdateStepsEnum Stepname) {
184                 waitForLoader();
185                 getWebElementWaitForClickable(Stepname.getValue()).click();
186                 waitForLoader();
187         }
188
189         public static void sleep(int duration) {
190                 swallowException(() -> Thread.sleep(duration));
191         }
192
193         public static WebDriver getDriver() {
194                 return driver;
195         }
196
197         public static void initDriver() {
198                 try {
199                         System.out.println("opening browser");
200                         WebDriver webDriver;
201                         boolean remoteTesting = SetupCDTest.config.isRemoteTesting();
202                         if (!remoteTesting) {
203                                 webDriver = new FirefoxDriver();
204                         } else {
205                                 String remoteEnvIP = SetupCDTest.config.getRemoteTestingMachineIP();
206                                 String remoteEnvPort = SetupCDTest.config.getRemoteTestingMachinePort();
207                                 DesiredCapabilities cap = new DesiredCapabilities();
208                                 cap = DesiredCapabilities.firefox();
209                                 cap.setPlatform(Platform.WINDOWS);
210                                 cap.setBrowserName("firefox");
211
212                                 String remoteNodeUrl = String.format(SetupCDTest.SELENIUM_NODE_URL, remoteEnvIP, remoteEnvPort);
213                                 webDriver = new RemoteWebDriver(new URL(remoteNodeUrl), cap);
214
215                         }
216                         driver = webDriver;
217
218                 } catch (MalformedURLException e) {
219                         throw new RuntimeException(e);
220                 }
221
222         }
223
224         /**
225          * waits until either loader finishes or 10 seconds has passed.<br>
226          * If 10 seconds has passed and loader didn't finish throws
227          * LoaderStuckException.<br>
228          */
229         public static void waitForLoader() {
230                 waitForLoader(10);
231         }
232
233         /**
234          * waits until either loader finishes or maxWaitTimeInSeconds has
235          * passed.<br>
236          * If maxWaitTimeInSeconds has passed and loader didn't finish throws
237          * LoaderStuckException.<br>
238          * 
239          * @param maxWaitTimeInSeconds
240          */
241         public static void waitForLoader(int maxWaitTimeInSeconds) {
242                 long maxWaitTimeMS = maxWaitTimeInSeconds * 1000L;
243                 Boolean loaderIsRunning = retryMethodOnResult(
244                                 () -> isElementPresent(DataTestIdEnum.GeneralSection.LOADER.getValue()),
245                                 isLoaderPresent -> !isLoaderPresent, maxWaitTimeMS, 50);
246                 if (loaderIsRunning) {
247                         throw new LoaderStuckException(
248                                         "UI Loader is stuck, max wait time of " + maxWaitTimeInSeconds + " seconds has passed.");
249                 }
250
251         }
252
253         private static class LoaderStuckException extends RuntimeException {
254                 private static final long serialVersionUID = 1L;
255
256                 private LoaderStuckException(String message) {
257                         super(message);
258                 }
259         }
260
261         /**
262          * Move to HTML element by class name. When moving to the HTML element, it
263          * will raise hover event.
264          * 
265          * @param className
266          */
267         public static void moveToHTMLElementByClassName(String className) {
268                 Actions actions = new Actions(getDriver());
269                 final WebElement createButtonsArea = getDriver().findElement(By.className(className));
270                 actions.moveToElement(createButtonsArea).perform();
271         }
272
273         /**
274          * Move to HTML element by element id. When moving to the HTML element, it
275          * will raise hover event.
276          * 
277          * @param className
278          */
279         public static void moveToHTMLElementByDataTestId(String dataTestId) {
280                 Actions actions = new Actions(getDriver());
281                 final WebElement createButtonsArea = getWebElementByDataTestId(dataTestId);
282                 actions.moveToElement(createButtonsArea).perform();
283         }
284
285         public static void defineVendorName(String resourceVendorName) {
286                 // TODO ui-ci replace with Enum
287                 WebElement resourceVendorNameTextbox = getWebElementWaitForVisible("vendorName");
288                 resourceVendorNameTextbox.clear();
289                 resourceVendorNameTextbox.sendKeys(resourceVendorName);
290         }
291
292         public static String defineUserId(String UserId) {
293                 // TODO ui-ci replace with Enum
294                 WebElement resourceTagsTextbox = getWebElementWaitForVisible("contactId");
295                 resourceTagsTextbox.clear();
296                 resourceTagsTextbox.sendKeys(UserId);
297                 return UserId;
298         }
299
300         public static void clickAddComponent(Dashboard componentType) {
301                 Runnable clickAddTask = () -> {
302                         // TODO ui-ci replace with data-test-id
303                         moveToHTMLElementByClassName("w-sdc-dashboard-card-new");
304                         WebElement addVfButton = getWebElementByDataTestId(componentType.getValue());
305                         addVfButton.click();
306                 };
307                 retryMethodOnException(clickAddTask);
308         }
309
310         /**
311          * This method perform submit for testing process for existing service or
312          * resource.<br>
313          * It assumes it is activated when in the resource screen and the Submit For
314          * Testing button is available.
315          * 
316          * @param componentNameForMessage
317          *            TODO
318          */
319         public static void submitForTestingElement(String componentNameForMessage) {
320                 waitForLoader();
321                 getWebElementWaitForVisible(DataTestIdEnum.LifeCyleChangeButtons.SUBMIT_FOR_TESTING.getValue()).click();
322                 waitForLoader();
323                 getWebElementWaitForVisible(DataTestIdEnum.ModalItems.SUMBIT_FOR_TESTING_MESSAGE.getValue())
324                                 .sendKeys("Submit for testing for " + componentNameForMessage);
325                 waitForLoader();
326                 getWebElementWaitForClickable(DataTestIdEnum.ModalItems.OK.getValue()).click();
327                 waitForLoader();
328                 waitForElementToDisappear(DataTestIdEnum.ModalItems.OK.getValue());
329
330         }
331
332         /**
333          * Waits Until elements disappears or until 10 seconds pass
334          * 
335          * @param dataTestId
336          */
337         public static void waitForElementToDisappear(String dataTestId) {
338                 Supplier<Boolean> elementPresenseChecker = () -> GeneralUIUtils.isElementPresent(dataTestId);
339                 Function<Boolean, Boolean> verifier = isElementPresent -> !isElementPresent;
340                 FunctionalInterfaces.retryMethodOnResult(elementPresenseChecker, verifier);
341
342         }
343
344 }