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