Increase timeout for Selenium tests
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / utilities / ResourceUIUtils.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.onap.sdc.frontend.ci.tests.utilities;
22
23 import com.aventstack.extentreports.Status;
24 import org.apache.commons.lang3.tuple.ImmutablePair;
25 import org.json.simple.JSONArray;
26 import org.json.simple.JSONObject;
27 import org.json.simple.JSONValue;
28 import org.onap.sdc.backend.ci.tests.datatypes.ResourceReqDetails;
29 import org.onap.sdc.frontend.ci.tests.pages.GeneralPageElements;
30 import org.openecomp.sdc.be.model.User;
31 import org.onap.sdc.frontend.ci.tests.datatypes.CreateAndImportButtonsEnum;
32 import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum;
33 import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum.Dashboard;
34 import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum.StepsEnum;
35 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
36 import org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest;
37 import org.onap.sdc.frontend.ci.tests.pages.ResourceGeneralPage;
38 import org.openqa.selenium.By;
39 import org.openqa.selenium.ElementNotVisibleException;
40 import org.openqa.selenium.WebDriver;
41 import org.openqa.selenium.WebElement;
42 import org.openqa.selenium.support.ui.ExpectedConditions;
43 import org.openqa.selenium.support.ui.Select;
44 import org.openqa.selenium.support.ui.WebDriverWait;
45
46 import java.io.File;
47 import java.util.List;
48 import java.util.Random;
49
50 public final class ResourceUIUtils {
51     public static final String RESOURCE_NAME_PREFIX = "ResourceCDTest-";
52     protected static final boolean IS_BEFORE_TEST = true;
53     public static final String INITIAL_VERSION = "0.1";
54     public static final String ICON_RESOURCE_NAME = "call_controll";
55     protected static final String UPDATED_RESOURCE_ICON_NAME = "objectStorage";
56     private static final int BASIC_TIMEOUT = 10 * 60;
57
58     private ResourceUIUtils() {
59     }
60
61     private static WebDriver driver = GeneralUIUtils.getDriver();
62
63     // click and upload tosca file //**to be changed.
64     public static void importFileWithSendKey(String filePath, String fileName, CreateAndImportButtonsEnum type)
65             throws Exception {
66         WebElement importButton = HomeUtils.createAndImportButtons(type, driver).findElement(By.tagName("input"));
67         importButton.sendKeys(filePath + fileName);
68     }
69
70     public static String defineUserId(String userId) {
71         //
72         WebElement resourceUserIdTextbox = ResourceGeneralPage.getContactIdField();
73         resourceUserIdTextbox.clear();
74         resourceUserIdTextbox.sendKeys(userId);
75         return userId;
76     }
77
78     static String definePropertyName(String name) {
79
80         WebElement nameProperty = GeneralUIUtils.getDriver().findElement(By.name("propertyName"));
81         nameProperty.sendKeys(name);
82         return name;
83     }
84
85     public static void selectRandomResourceIcon() throws Exception {
86         final int webDriverWaitingTimeout = 10;
87         GeneralUIUtils.moveToStep(StepsEnum.ICON);
88         WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), webDriverWaitingTimeout);
89         wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@data-tests-id, 'iconBox')]")));
90         List<WebElement> iconElement = GeneralUIUtils.getDriver()
91                 .findElements(By.xpath("//*[contains(@data-tests-id, 'iconBox')]"));
92         iconElement.get(0).click();
93     }
94
95     static void defineDefaultValueByType(String value) {
96
97         WebElement valueString = GeneralUIUtils.getDriver().findElement(By.name("value"));
98         valueString.clear();
99         valueString.sendKeys(value);
100     }
101
102     static void defineBoolenDefaultValue(String value) {
103
104         WebElement elementBoolean = GeneralUIUtils.getDriver().findElement(By.name("value"));
105         Select se = new Select(elementBoolean);
106         se.selectByValue(value);
107     }
108
109     public static void fillResourceGeneralInformationPage(ResourceReqDetails resource, User user, boolean isNewResource) {
110         try {
111             ResourceGeneralPage.defineName(resource.getName());
112             ResourceGeneralPage.defineDescription(resource.getDescription());
113             ResourceGeneralPage.defineCategory(resource.getCategories().get(0).getSubcategories().get(0).getName());
114             ResourceGeneralPage.defineVendorName(resource.getVendorName());
115             ResourceGeneralPage.defineVendorRelease(resource.getVendorRelease());
116             if (isNewResource) {
117                 ResourceGeneralPage.defineTagsList(resource, new String[]{"This-is-tag", "another-tag", "Test-automation-tag"});
118             } else {
119                 ResourceGeneralPage.defineTagsList(resource, new String[]{"one-more-tag"});
120             }
121         } catch (Exception e) {
122             throw new RuntimeException(e);
123         }
124     }
125
126     public static void fillMaxValueResourceGeneralInformationPage(ResourceReqDetails resource) {
127         final int stringPatternLength = 5000;
128         String stringPattern = "ABCDabcd123456";
129         GeneralUIUtils.addStringtoClipboard(buildStringFromPattern(stringPattern, stringPatternLength));
130         ResourceGeneralPage.defineNameWithPaste();
131         ResourceGeneralPage.defineDescriptionWithPaste();
132         ResourceGeneralPage.defineVendorNameWithPaste();
133         ResourceGeneralPage.defineVendorReleaseWithPaste();
134         ResourceGeneralPage.defineTagsListWithPaste();
135         GeneralUIUtils.waitForAngular();
136     }
137
138     public static String buildStringFromPattern(String stringPattern, int stringLength) {
139         char[] chars = stringPattern.toCharArray();
140         StringBuilder sb = new StringBuilder();
141         Random random = new Random();
142         for (int i = 0; i < stringLength; i++) {
143             char c = chars[random.nextInt(chars.length)];
144             sb.append(c);
145         }
146         return sb.toString();
147     }
148
149     /**
150      * @deprecated Use {@link #createVF(ResourceReqDetails, User)} instead
151      */
152     public static void createResource(ResourceReqDetails resource, User user) throws Exception {
153         createVF(resource, user);
154     }
155
156     public static void createVF(ResourceReqDetails resource, User user) {
157         ExtentTestActions.log(Status.INFO, "Going to create a new VF.");
158         createResource(resource, user, DataTestIdEnum.Dashboard.BUTTON_ADD_VF);
159     }
160
161     private static void createResource(ResourceReqDetails resource, User user, DataTestIdEnum.Dashboard button) {
162         WebElement addVFButton;
163         try {
164             GeneralUIUtils.ultimateWait();
165             try {
166                 GeneralUIUtils.hoverOnAreaByClassName("w-sdc-dashboard-card-new");
167                 addVFButton = GeneralUIUtils.getWebElementByTestID(button.getValue());
168             } catch (Exception e) {
169                 File imageFilePath = GeneralUIUtils.takeScreenshot(null, SetupCDTest.getScreenshotFolder(), "Warning_" + resource.getName());
170                 final String absolutePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFilePath.toURI()).getPath();
171                 SetupCDTest.getExtendTest().log(Status.WARNING, "Add button is not visible after hover on import area of Home page, moving on ..." + SetupCDTest.getExtendTest().addScreenCaptureFromPath(absolutePath));
172                 showButtonsADD();
173                 addVFButton = GeneralUIUtils.getWebElementByTestID(button.getValue());
174             }
175             addVFButton.click();
176             GeneralUIUtils.ultimateWait();
177         } catch (Exception e) {
178             SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Exeption catched on ADD button, retrying ... "));
179             GeneralUIUtils.hoverOnAreaByClassName("w-sdc-dashboard-card-new");
180             GeneralUIUtils.ultimateWait();
181             GeneralUIUtils.getWebElementByTestID(button.getValue()).click();
182             GeneralUIUtils.ultimateWait();
183         }
184         fillResourceGeneralInformationPage(resource, user, true);
185         resource.setVersion("0.1");
186         GeneralPageElements.clickCreateButton();
187     }
188
189     public static void updateResource(ResourceReqDetails resource, User user) {
190         ResourceGeneralPage.defineContactId(resource.getContactId());
191         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Updating General screen fields ..."));
192         fillResourceGeneralInformationPage(resource, user, false);
193         ResourceGeneralPage.clickUpdateButton();
194     }
195
196     /**
197      * Click on HTML element.
198      *
199      * @param dataTestId
200      * @throws Exception
201      */
202     public static void getWebElementByTestID(String dataTestId) throws Exception {
203         final int webDriverWaitingTimeout = 20;
204         WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), webDriverWaitingTimeout);
205         WebElement element = wait
206                 .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@data-tests-id='" + dataTestId + "']")));
207         element.click();
208         // wait.until(ExpectedConditions.elemetto)
209         // WebElement serviceButton =
210         // GeneralUIUtils.getDriver().findElement(By.xpath("//*[@data-tests-id='"
211         // + dataTestId + "']"));
212         // serviceButton.
213         // serviceButton.click();
214     }
215
216     /**
217      * Import VFC
218      *
219      * @param user
220      * @param filePath
221      * @param fileName
222      * @return
223      * @throws Exception
224      */
225
226     public static void importVfc(ResourceReqDetails resourceMetaData, String filePath, String fileName, User user)
227             throws Exception {
228         GeneralUIUtils.ultimateWait();
229         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating new VFC resource ", resourceMetaData.getName()));
230         GeneralUIUtils.hoverOnAreaByTestId(Dashboard.IMPORT_AREA.getValue());
231         GeneralUIUtils.ultimateWait();
232         // Insert file to the browse dialog
233         WebElement buttonVFC = GeneralUIUtils.findByText("Import VFC");
234         WebElement fileInputElement = GeneralUIUtils.getInputElement(DataTestIdEnum.Dashboard.IMPORT_VFC_FILE.getValue());
235         if (!buttonVFC.isDisplayed()) {
236             File imageFilePath = GeneralUIUtils.takeScreenshot(null, SetupCDTest.getScreenshotFolder(), "Warning_" + resourceMetaData.getName());
237             final String absolutePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFilePath.toURI()).getPath();
238             SetupCDTest.getExtendTest().log(Status.WARNING, "VFC button not visible after hover on import area of Home page, moving on ..." + SetupCDTest.getExtendTest().addScreenCaptureFromPath(absolutePath));
239         }
240         try {
241             fileInputElement.sendKeys(filePath + fileName);
242         } catch (ElementNotVisibleException e) {
243             SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Exeption catched on file input, converting VFC file input to visible"));
244             showButtons();
245             fileInputElement.sendKeys(filePath + fileName);
246         }
247         // Fill the general page fields.
248         GeneralUIUtils.ultimateWait();
249         fillResourceGeneralInformationPage(resourceMetaData, user, true);
250         GeneralPageElements.clickCreateButton();
251     }
252
253     public static void importVfcNoCreate(ResourceReqDetails resourceMetaData, String filePath, String fileName, User user)
254             throws Exception {
255         GeneralUIUtils.hoverOnAreaByTestId(Dashboard.IMPORT_AREA.getValue());
256         // Insert file to the browse dialog
257         WebElement buttonVFC = GeneralUIUtils.findByText("Import VFC");
258         WebElement fileInputElement = GeneralUIUtils.getInputElement(DataTestIdEnum.Dashboard.IMPORT_VFC_FILE.getValue());
259         if (!buttonVFC.isDisplayed()) {
260             File imageFilePath = GeneralUIUtils.takeScreenshot(null, SetupCDTest.getScreenshotFolder(), "Warning_" + resourceMetaData.getName());
261             final String absolutePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFilePath.toURI()).getPath();
262             SetupCDTest.getExtendTest().log(Status.WARNING, "VFC button not visible after hover on import area of Home page, moving on ..." + SetupCDTest.getExtendTest().addScreenCaptureFromPath(absolutePath));
263         }
264         try {
265             fileInputElement.sendKeys(filePath + fileName);
266         } catch (ElementNotVisibleException e) {
267             SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Exeption catched on file input, converting VFC file input to visible"));
268             showButtons();
269             fileInputElement.sendKeys(filePath + fileName);
270         }
271         // Fill the general page fields.
272         GeneralUIUtils.waitForLoader();
273         fillResourceGeneralInformationPage(resourceMetaData, user, true);
274     }
275
276
277     public static void importVfFromCsar(ResourceReqDetails resourceMetaData, String filePath, String fileName, User user)
278             throws Exception {
279         GeneralUIUtils.ultimateWait();
280         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating new VF asset resource %s", resourceMetaData.getName()));
281         GeneralUIUtils.hoverOnAreaByTestId(Dashboard.IMPORT_AREA.getValue());
282         GeneralUIUtils.ultimateWait();
283         // Insert file to the browse dialog
284         WebElement buttonDCAE = GeneralUIUtils.findByText("Import DCAE asset");
285         WebElement fileInputElement = GeneralUIUtils.getInputElement(DataTestIdEnum.Dashboard.IMPORT_VF_FILE.getValue());
286         if (!buttonDCAE.isDisplayed()) {
287             File imageFilePath = GeneralUIUtils.takeScreenshot(null, SetupCDTest.getScreenshotFolder(), "Warning_" + resourceMetaData.getName());
288             final String absolutePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFilePath.toURI()).getPath();
289             SetupCDTest.getExtendTest().log(Status.WARNING, "DCAE button not visible after hover on import area of Home page, moving on ..." + SetupCDTest.getExtendTest().addScreenCaptureFromPath(absolutePath));
290         }
291         try {
292             fileInputElement.sendKeys(filePath + fileName);
293         } catch (ElementNotVisibleException e) {
294             SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Exeption catched on file input, converting DCAE file input to visible"));
295             showButtons();
296             fileInputElement.sendKeys(filePath + fileName);
297         }
298         // Fill the general page fields.
299         GeneralUIUtils.ultimateWait();
300         fillResourceGeneralInformationPage(resourceMetaData, user, true);
301         GeneralPageElements.clickCreateButton(BASIC_TIMEOUT);
302         //GeneralUIUtils.ultimateWait(); "don't change import of csar can take longer then 3 minutes"
303         GeneralUIUtils.waitForLoader(BASIC_TIMEOUT);
304     }
305
306     public static void importVfFromCsarNoCreate(ResourceReqDetails resourceMetaData, String filePath, String fileName, User user)
307             throws Exception {
308         GeneralUIUtils.ultimateWait();
309         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Creating new VF asset resource %s, Create button will not be clicked", resourceMetaData.getName()));
310         GeneralUIUtils.hoverOnAreaByTestId(Dashboard.IMPORT_AREA.getValue());
311         GeneralUIUtils.ultimateWait();
312         // Insert file to the browse dialog
313         WebElement buttonDCAE = GeneralUIUtils.findByText("Import DCAE asset");
314         WebElement fileInputElement = GeneralUIUtils.getInputElement(DataTestIdEnum.Dashboard.IMPORT_VF_FILE.getValue());
315         if (!buttonDCAE.isDisplayed()) {
316             File imageFilePath = GeneralUIUtils.takeScreenshot(null, SetupCDTest.getScreenshotFolder(), "Warning_" + resourceMetaData.getName());
317             final String absolutePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFilePath.toURI()).getPath();
318             SetupCDTest.getExtendTest().log(Status.WARNING, "DCAE button not visible after hover on import area of Home page, moving on ..." + SetupCDTest.getExtendTest().addScreenCaptureFromPath(absolutePath));
319         }
320         try {
321             fileInputElement.sendKeys(filePath + fileName);
322         } catch (ElementNotVisibleException e) {
323             SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Exeption catched on file input, converting DCAE file input to visible"));
324             showButtons();
325             fileInputElement.sendKeys(filePath + fileName);
326         }
327         // Fill the general page fields.
328         GeneralUIUtils.ultimateWait();
329         fillResourceGeneralInformationPage(resourceMetaData, user, true);
330         GeneralUIUtils.waitForLoader(BASIC_TIMEOUT);
331     }
332
333     public static void updateVfWithCsar(String filePath, String fileName) {
334         ExtentTestActions.log(Status.INFO, "Updating VF with updated CSAR file named " + fileName);
335         WebElement browseWebElement = GeneralUIUtils.getInputElement(DataTestIdEnum.GeneralElementsEnum.UPLOAD_FILE_INPUT.getValue());
336         browseWebElement.sendKeys(filePath + fileName);
337         GeneralUIUtils.ultimateWait();
338         GeneralPageElements.clickUpdateButton();
339         GeneralUIUtils.waitForLoader();
340         ExtentTestActions.log(Status.INFO, "VF is updated.");
341     }
342
343     private static void showButtons() {
344         String parentElementClassAttribute = "sdc-dashboard-import-element-container";
345         WebElement fileInputElementWithVisible = GeneralUIUtils.getDriver().findElement(By.className(parentElementClassAttribute));
346         GeneralUIUtils.unhideElement(fileInputElementWithVisible, parentElementClassAttribute);
347         GeneralUIUtils.ultimateWait();
348         SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Input buttons now visible..."));
349     }
350
351     private static void showButtonsADD() {
352         try {
353             GeneralUIUtils.ultimateWait();
354             String parentElementClassAttribute = "sdc-dashboard-create-element-container";
355             WebElement fileInputElementWithVisible = GeneralUIUtils.getDriver().findElement(By.className(parentElementClassAttribute));
356             GeneralUIUtils.unhideElement(fileInputElementWithVisible, parentElementClassAttribute);
357             GeneralUIUtils.ultimateWait();
358         } catch (Exception e) {
359             GeneralUIUtils.ultimateWait();
360             String parentElementClassAttribute = "sdc-dashboard-create-element-container";
361             WebElement fileInputElementWithVisible = GeneralUIUtils.getDriver().findElement(By.className(parentElementClassAttribute));
362             GeneralUIUtils.unhideElement(fileInputElementWithVisible, parentElementClassAttribute);
363             GeneralUIUtils.ultimateWait();
364         }
365         SetupCDTest.getExtendTest().log(Status.WARNING, String.format("Input buttons now visible..."));
366     }
367
368     public static void clickOnElementByText(String textToClick, String customizationFoLog) {
369         String customizationFoLogLocal = customizationFoLog != null ? customizationFoLog : "";
370         SetupCDTest.getExtendTest().log(Status.INFO, String.format("Clicking on %s %s", textToClick, customizationFoLogLocal));
371         GeneralUIUtils.clickOnElementByText(textToClick);
372     }
373
374     public static void createPNF(ResourceReqDetails resource, User user) throws Exception {
375         ExtentTestActions.log(Status.INFO, "Going to create a new PNF");
376         createResource(resource, user, DataTestIdEnum.Dashboard.BUTTON_ADD_PNF);
377     }
378
379     public static void createCR(ResourceReqDetails resource, User user) throws Exception {
380         ExtentTestActions.log(Status.INFO, "Going to create a new CR");
381         createResource(resource, user, DataTestIdEnum.Dashboard.BUTTON_ADD_CR);
382     }
383
384     public static ImmutablePair<String, String> getFirstRIPos(ResourceReqDetails createResourceInUI, User user) {
385         String responseAfterDrag = RestCDUtils.getResource(createResourceInUI, user).getResponse();
386         JSONObject jsonResource = (JSONObject) JSONValue.parse(responseAfterDrag);
387         String xPosPostDrag = (String) ((JSONObject) ((JSONArray) jsonResource.get("componentInstances")).get(0))
388                 .get("posX");
389         String yPosPostDrag = (String) ((JSONObject) ((JSONArray) jsonResource.get("componentInstances")).get(0))
390                 .get("posY");
391         return new ImmutablePair<String, String>(xPosPostDrag, yPosPostDrag);
392
393     }
394
395
396 }