Update integration UI tests selenium image
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / HomePage.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.pages;
22
23 import com.aventstack.extentreports.Status;
24 import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum;
25 import org.onap.sdc.frontend.ci.tests.exception.HomePageRuntimeException;
26 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
27 import org.onap.sdc.frontend.ci.tests.utilities.DownloadManager;
28 import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
29 import org.onap.sdc.frontend.ci.tests.utilities.GeneralUIUtils;
30 import org.openqa.selenium.By;
31 import org.openqa.selenium.WebElement;
32 import org.openqa.selenium.interactions.Actions;
33 import org.openqa.selenium.support.ui.ExpectedConditions;
34 import org.openqa.selenium.support.ui.WebDriverWait;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.text.SimpleDateFormat;
41 import java.util.Calendar;
42 import java.util.List;
43 import java.util.UUID;
44
45 import static org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory.getDriver;
46 import static org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest.getExtendTest;
47 import static org.onap.sdc.frontend.ci.tests.pages.HomePage.PageElement.REPOSITORY_ICON;
48
49 public class HomePage {
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(HomePage.class);
52
53     private static final int WAIT_FOR_ELEMENT_TIME_OUT = 30;
54     private static final int WAIT_FOR_LOADER_TIME_OUT = 600;
55
56     private HomePage() {
57
58     }
59
60     public static void showVspRepository() {
61         GeneralUIUtils.waitForElementInVisibilityBy(By.className("ui-notification"), WAIT_FOR_ELEMENT_TIME_OUT);
62         GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.MainMenuButtons.REPOSITORY_ICON.getValue());
63     }
64
65     public static boolean searchForVSP(String vspName) {
66         GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ImportVfRepository.SEARCH.getValue()).clear();
67         GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ImportVfRepository.SEARCH.getValue()).sendKeys(vspName);
68         GeneralUIUtils.ultimateWait();
69         return true;
70     }
71
72     public static void importVSP(String vspName) {
73         HomePage.showVspRepository();
74         boolean vspFound = HomePage.searchForVSP(vspName);
75         if (vspFound) {
76             List<WebElement> elementsFromTable = GeneralPageElements.getElementsFromTable();
77             WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), WAIT_FOR_ELEMENT_TIME_OUT);
78             WebElement findElement = wait.until(ExpectedConditions.visibilityOf(elementsFromTable.get(1)));
79             findElement.click();
80             GeneralUIUtils.waitForLoader();
81             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ImportVfRepository.IMPORT_VSP.getValue());
82             GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.GeneralElementsEnum.CREATE_BUTTON.getValue());
83             GeneralUIUtils.waitForLoader(WAIT_FOR_LOADER_TIME_OUT);
84             GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralElementsEnum.CHECKIN_BUTTON.getValue());
85         }
86     }
87
88     public static boolean navigateToHomePage() {
89         try {
90             LOGGER.debug("Searching for repository icon");
91             final WebElement repositoryIcon = GeneralUIUtils.getInputElement(REPOSITORY_ICON.getTestId());
92             if (repositoryIcon != null) {
93                 return true;
94             }
95             GeneralUIUtils.ultimateWait();
96             final List<WebElement> homeButtons = GeneralUIUtils
97                 .getElementsByLocator(By.xpath("//a[contains(.,'HOME')]"));
98             if (!homeButtons.isEmpty()) {
99                 homeButtons.stream().filter(WebElement::isDisplayed).findFirst().ifPresent(webElement -> {
100                     webElement.click();
101                     LOGGER.debug("Clicked on home button");
102                 });
103             }
104
105             GeneralUIUtils.closeErrorMessage();
106             WebElement homeButton = GeneralUIUtils
107                 .getInputElement(DataTestIdEnum.MainMenuButtons.HOME_BUTTON.getValue());
108             if (homeButton != null) {
109                 //making sure that the mouse it is not on the add/import area
110                 new Actions(getDriver()).moveToElement(homeButton).perform();
111             }
112             return homeButton != null && homeButton.isDisplayed();
113
114         } catch (final Exception e) {
115             final String msg = "Could not click on home button";
116             getExtendTest()
117                 .log(Status.WARNING, msg);
118             LOGGER.warn(msg, e);
119             return false;
120         }
121     }
122
123     public static File downloadVspCsarToDefaultDirectory(String vspName) throws Exception {
124         GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.MainMenuButtonsFromInsideFrame.HOME_BUTTON.getValue()).click();
125         DownloadManager.downloadCsarByNameFromVSPRepository(vspName, "");
126         return FileHandling.getLastModifiedFileNameFromDir();
127     }
128
129     public static void findComponentAndClick(final String resourceName) {
130         findComponent(resourceName);
131         clickComponent(resourceName);
132     }
133
134     public static void findComponent(final String resourceName) {
135         LOGGER.debug("Searching for component '{}'", resourceName);
136         getExtendTest().log(Status.INFO, "Searching for " + resourceName + " in home tab");
137         clearSearchResults(getSearchInput());
138         searchForComponent(resourceName);
139     }
140
141     private static WebElement getSearchInput() {
142         WebElement searchTextbox;
143         try {
144             searchTextbox = TopSearchComponent.getComponentInput();
145             LOGGER.debug("Search textbox '{}' selected", TopSearchComponent.SEARCH_INPUT_TEST_ID);
146         } catch (final Exception e) {
147             final String errorMsg = "Top Search bar was not visible";
148             getExtendTest().log(Status.ERROR, errorMsg);
149             throw new HomePageRuntimeException(errorMsg, e);
150         }
151         return searchTextbox;
152     }
153
154     private static void clearSearchResults(final WebElement searchTextbox) {
155         try {
156             LOGGER.debug("Clearing search results before searching");
157             TopSearchComponent.replaceSearchValue(searchTextbox, UUID.randomUUID().toString());
158             MainRightContainer.isEmptyResult();
159         } catch (final Exception e) {
160             final String errorMsg = "Could not clean up the search result";
161             getExtendTest().log(Status.ERROR, errorMsg);
162             throw new HomePageRuntimeException(errorMsg, e);
163         }
164     }
165
166     private static void searchForComponent(final String resourceName) {
167         try {
168             LOGGER.debug("Searching for '{}'", resourceName);
169             TopSearchComponent.replaceSearchValue(resourceName);
170             MainRightContainer.isResultVisible(resourceName);
171         } catch (final Exception e) {
172             final String errorMsg = String.format("Could not find the component '%s' after search", resourceName);
173             getExtendTest().log(Status.ERROR, errorMsg);
174             throw new HomePageRuntimeException(errorMsg, e);
175         }
176     }
177
178     public static void clickComponent(final String resourceName) {
179         LOGGER.debug("Clicking on the component " + resourceName);
180         try {
181             getExtendTest()
182                 .log(Status.INFO, String.format("Clicking on the '%s' component from home tab", resourceName));
183             GeneralUIUtils.clickOnElementByTestId(resourceName);
184         } catch (final Exception e) {
185             final String errorMsg = String.format("Could not click on home tab component '%s' ", resourceName);
186             getExtendTest().log(Status.ERROR, e.getMessage());
187             throw new HomePageRuntimeException(errorMsg, e);
188         }
189         final String datetimeString =
190             new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.SSS").format(Calendar.getInstance().getTime());
191         try {
192             ExtentTestActions
193                 .addScreenshot(Status.INFO,
194                     String.format("after-click-resource-%s-%s", resourceName, datetimeString),
195                     String.format("Clicked on resource '%s'", resourceName)
196                 );
197         } catch (final IOException e) {
198             LOGGER.warn("Could take screenshot after resource {} click", resourceName, e);
199         }
200         try {
201             GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralElementsEnum.LIFECYCLE_STATE.getValue());
202         } catch (final Exception e) {
203             final String errorMsg = String.format("Expecting to be inside component '%s' screen", resourceName);
204             getExtendTest().log(Status.ERROR, e.getMessage());
205             throw new HomePageRuntimeException(errorMsg, e);
206         }
207     }
208
209     public static void waitForElement(PageElement homePageElement) {
210         final String cssClass = homePageElement.getCssClass();
211         LOGGER.debug("Waiting for{} visibility", cssClass);
212         GeneralUIUtils.getWebElementByClassName(cssClass);
213         LOGGER.debug("{} is visible", cssClass);
214     }
215
216     public enum PageElement {
217         COMPONENT_PANEL("w-sdc-main-right-container", null),
218         REPOSITORY_ICON(null, "repository-icon");
219
220         private final String cssClass;
221         private final String testId;
222
223         PageElement(String cssClass, String testId) {
224             this.cssClass = cssClass;
225             this.testId = testId;
226         }
227
228         public String getCssClass() {
229             return cssClass;
230         }
231
232         public String getTestId() {
233             return testId;
234         }
235     }
236
237 }