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