aboutsummaryrefslogtreecommitdiffstats
path: root/integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/pages/HomePage.java
blob: 0c72a9752c502bdfaed55a015c9655fb63ff5d78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.frontend.ci.tests.pages;

import com.aventstack.extentreports.Status;
import org.onap.sdc.frontend.ci.tests.datatypes.DataTestIdEnum;
import org.onap.sdc.frontend.ci.tests.exception.HomePageRuntimeException;
import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
import org.onap.sdc.frontend.ci.tests.utilities.DownloadManager;
import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
import org.onap.sdc.frontend.ci.tests.utilities.GeneralUIUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import static org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory.getDriver;
import static org.onap.sdc.frontend.ci.tests.execute.setup.SetupCDTest.getExtendTest;
import static org.onap.sdc.frontend.ci.tests.pages.HomePage.PageElement.REPOSITORY_ICON;

public class HomePage {

    private static final Logger LOGGER = LoggerFactory.getLogger(HomePage.class);

    private static final int WAIT_FOR_ELEMENT_TIME_OUT = 30;
    private static final int WAIT_FOR_LOADER_TIME_OUT = 600;

    private HomePage() {

    }

    public static void showVspRepository() {
        GeneralUIUtils.waitForElementInVisibilityBy(By.className("ui-notification"), WAIT_FOR_ELEMENT_TIME_OUT);
        GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.MainMenuButtons.REPOSITORY_ICON.getValue());
    }

    public static boolean searchForVSP(String vspName) {
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ImportVfRepository.SEARCH.getValue()).clear();
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.ImportVfRepository.SEARCH.getValue()).sendKeys(vspName);
        GeneralUIUtils.ultimateWait();
        return true;
    }

    public static void importVSP(String vspName) {
        HomePage.showVspRepository();
        boolean vspFound = HomePage.searchForVSP(vspName);
        if (vspFound) {
            List<WebElement> elementsFromTable = GeneralPageElements.getElementsFromTable();
            WebDriverWait wait = new WebDriverWait(GeneralUIUtils.getDriver(), WAIT_FOR_ELEMENT_TIME_OUT);
            WebElement findElement = wait.until(ExpectedConditions.visibilityOf(elementsFromTable.get(1)));
            findElement.click();
            GeneralUIUtils.waitForLoader();
            GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.ImportVfRepository.IMPORT_VSP.getValue());
            GeneralUIUtils.clickOnElementByTestId(DataTestIdEnum.GeneralElementsEnum.CREATE_BUTTON.getValue());
            GeneralUIUtils.waitForLoader(WAIT_FOR_LOADER_TIME_OUT);
            GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralElementsEnum.CHECKIN_BUTTON.getValue());
        }
    }

    public static boolean navigateToHomePage() {
        try {
            LOGGER.debug("Searching for repository icon");
            final WebElement repositoryIcon = GeneralUIUtils.getInputElement(REPOSITORY_ICON.getTestId());
            if (repositoryIcon != null) {
                return true;
            }
            GeneralUIUtils.ultimateWait();
            final List<WebElement> homeButtons = GeneralUIUtils
                .getElementsByLocator(By.xpath("//a[contains(.,'HOME')]"));
            if (!homeButtons.isEmpty()) {
                homeButtons.stream().filter(WebElement::isDisplayed).findFirst().ifPresent(webElement -> {
                    webElement.click();
                    LOGGER.debug("Clicked on home button");
                });
            }

            GeneralUIUtils.closeErrorMessage();
            WebElement homeButton = GeneralUIUtils
                .getInputElement(DataTestIdEnum.MainMenuButtons.HOME_BUTTON.getValue());
            if (homeButton != null) {
                //making sure that the mouse it is not on the add/import area
                new Actions(getDriver()).moveToElement(homeButton).perform();
            }
            return homeButton != null && homeButton.isDisplayed();

        } catch (final Exception e) {
            final String msg = "Could not click on home button";
            getExtendTest()
                .log(Status.WARNING, msg);
            LOGGER.warn(msg, e);
            return false;
        }
    }

    public static File downloadVspCsarToDefaultDirectory(String vspName) throws Exception {
        GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.MainMenuButtonsFromInsideFrame.HOME_BUTTON.getValue()).click();
        DownloadManager.downloadCsarByNameFromVSPRepository(vspName, "");
        return FileHandling.getLastModifiedFileNameFromDir();
    }

    public static void findComponentAndClick(final String resourceName) {
        findComponent(resourceName);
        clickComponent(resourceName);
    }

    public static void findComponent(final String resourceName) {
        LOGGER.debug("Searching for component '{}'", resourceName);
        getExtendTest().log(Status.INFO, "Searching for " + resourceName + " in home tab");
        clearSearchResults(getSearchInput());
        searchForComponent(resourceName);
    }

    private static WebElement getSearchInput() {
        WebElement searchTextbox;
        try {
            searchTextbox = TopSearchComponent.getComponentInput();
            LOGGER.debug("Search textbox '{}' selected", TopSearchComponent.SEARCH_INPUT_TEST_ID);
        } catch (final Exception e) {
            final String errorMsg = "Top Search bar was not visible";
            getExtendTest().log(Status.ERROR, errorMsg);
            throw new HomePageRuntimeException(errorMsg, e);
        }
        return searchTextbox;
    }

    private static void clearSearchResults(final WebElement searchTextbox) {
        try {
            LOGGER.debug("Clearing search results before searching");
            TopSearchComponent.replaceSearchValue(searchTextbox, UUID.randomUUID().toString());
            MainRightContainer.isEmptyResult();
        } catch (final Exception e) {
            final String errorMsg = "Could not clean up the search result";
            getExtendTest().log(Status.ERROR, errorMsg);
            throw new HomePageRuntimeException(errorMsg, e);
        }
    }

    private static void searchForComponent(final String resourceName) {
        try {
            LOGGER.debug("Searching for '{}'", resourceName);
            TopSearchComponent.replaceSearchValue(resourceName);
            MainRightContainer.isResultVisible(resourceName);
        } catch (final Exception e) {
            final String errorMsg = String.format("Could not find the component '%s' after search", resourceName);
            getExtendTest().log(Status.ERROR, errorMsg);
            throw new HomePageRuntimeException(errorMsg, e);
        }
    }

    public static void clickComponent(final String resourceName) {
        LOGGER.debug("Clicking on the component " + resourceName);
        try {
            getExtendTest()
                .log(Status.INFO, String.format("Clicking on the '%s' component from home tab", resourceName));
            GeneralUIUtils.clickOnElementByTestId(resourceName);
        } catch (final Exception e) {
            final String errorMsg = String.format("Could not click on home tab component '%s' ", resourceName);
            getExtendTest().log(Status.ERROR, e.getMessage());
            throw new HomePageRuntimeException(errorMsg, e);
        }
        final String datetimeString =
            new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.SSS").format(Calendar.getInstance().getTime());
        try {
            ExtentTestActions
                .addScreenshot(Status.INFO,
                    String.format("after-click-resource-%s-%s", resourceName, datetimeString),
                    String.format("Clicked on resource '%s'", resourceName)
                );
        } catch (final IOException e) {
            LOGGER.warn("Could take screenshot after resource {} click", resourceName, e);
        }
        try {
            GeneralUIUtils.getWebElementByTestID(DataTestIdEnum.GeneralElementsEnum.LIFECYCLE_STATE.getValue());
        } catch (final Exception e) {
            final String errorMsg = String.format("Expecting to be inside component '%s' screen", resourceName);
            getExtendTest().log(Status.ERROR, e.getMessage());
            throw new HomePageRuntimeException(errorMsg, e);
        }
    }

    public static void waitForElement(PageElement homePageElement) {
        final String cssClass = homePageElement.getCssClass();
        LOGGER.debug("Waiting for{} visibility", cssClass);
        GeneralUIUtils.getWebElementByClassName(cssClass);
        LOGGER.debug("{} is visible", cssClass);
    }

    public enum PageElement {
        COMPONENT_PANEL("w-sdc-main-right-container", null),
        REPOSITORY_ICON(null, "repository-icon");

        private final String cssClass;
        private final String testId;

        PageElement(String cssClass, String testId) {
            this.cssClass = cssClass;
            this.testId = testId;
        }

        public String getCssClass() {
            return cssClass;
        }

        public String getTestId() {
            return testId;
        }
    }

}