Provide user to specify the ouput name while declaring the atrributes
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / TopNavComponent.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.sdc.frontend.ci.tests.pages;
21
22 import java.time.Duration;
23 import java.util.List;
24 import lombok.AllArgsConstructor;
25 import lombok.Getter;
26 import org.onap.sdc.frontend.ci.tests.execute.setup.DriverFactory;
27 import org.onap.sdc.frontend.ci.tests.pages.home.HomePage;
28 import org.openqa.selenium.By;
29 import org.openqa.selenium.WebDriver;
30 import org.openqa.selenium.WebElement;
31 import org.openqa.selenium.interactions.Actions;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Handles the Top Navigation Component UI actions
37  */
38 public class TopNavComponent extends AbstractPageObject {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(TopNavComponent.class);
41
42     private WebElement wrappingElement;
43     private By navLocator = By.xpath(XpathSelector.NAV.getXpath());
44
45     public TopNavComponent(final WebDriver webDriver) {
46         super(webDriver);
47         timeoutInSeconds = 5;
48     }
49
50     @Override
51     public void isLoaded() {
52         wrappingElement = getWrappingElement();
53     }
54
55     /**
56      * Gets the enclosing element of the component.
57      *
58      * @return the enclosing element
59      */
60     public WebElement getWrappingElement() {
61         LOGGER.debug("Finding element with xpath '{}'", XpathSelector.NAV.getXpath());
62         return waitForElementVisibility(navLocator);
63     }
64
65     /**
66      * Clicks on home link inside the first breadcrumb arrow.
67      */
68     public HomePage clickOnHome() {
69         hoverToBreadcrumbArrow(0);
70         final By homeButtonLocator = By.xpath(XpathSelector.SUB_MENU_BUTTON_HOME.getXpath());
71         waitForElementVisibility(homeButtonLocator);
72         waitToBeClickable(homeButtonLocator).click();
73         return new HomePage(webDriver, this);
74     }
75
76     public boolean isHomeSelected() {
77         final By homeLinkLocator = By.xpath(XpathSelector.MAIN_MENU_LINK_HOME.getXpath());
78         final WebElement homeLinkElement = waitToBeClickable(homeLinkLocator);
79         final WebElement homeLinkParentElement = homeLinkElement.findElement(By.xpath("./.."));
80         final String homeLinkClass = homeLinkParentElement.getAttribute("class");
81         LOGGER.debug(String.format("Home link class '%s'", homeLinkClass));
82         return homeLinkClass != null && homeLinkClass.contains("selected");
83     }
84
85     /**
86      * Click on one of the items of the top nav breadcrumb based on the given position.
87      * The first item in the breadcrumb is position 0.
88      *
89      * @param position the position of the breadcrumb item
90      */
91     public void clickOnBreadCrumb(final int position) {
92         if (position < 0) {
93             throw new IllegalStateException("The position cannot be less that zero");
94         }
95         waitForElementVisibility(By.xpath(String.format("//*[@data-tests-id='breadcrumbs-button-%s']", position))).click();
96     }
97
98     public void waitRepositoryToBeClickable() {
99         waitToBeClickable(XpathSelector.REPOSITORY_ICON.getXpath());
100     }
101
102     /**
103      * Clicks on the VSP repository icon.
104      *
105      * @return the next page object
106      */
107     public VspRepositoryModalComponent clickOnRepositoryIcon() {
108         wrappingElement.findElement(By.xpath(XpathSelector.REPOSITORY_ICON.getXpath())).click();
109
110         return new VspRepositoryModalComponent(webDriver);
111     }
112
113     /**
114      * Clicks on the Onboard button.
115      *
116      * @return the next page object
117      */
118     public OnboardHomePage clickOnOnboard() {
119         wrappingElement.findElement(By.xpath(XpathSelector.MAIN_MENU_ONBOARD_BTN.getXpath())).click();
120         return new OnboardHomePage(DriverFactory.getDriver(), new OnboardHeaderComponent(DriverFactory.getDriver()));
121     }
122
123     /**
124      * Hover to a breadcrumb arrow of the given position.
125      *
126      * @param arrowPosition the position of the arrow from left to right
127      * @return the hovered breadcrumb arrow element
128      */
129     public WebElement hoverToBreadcrumbArrow(final int arrowPosition) {
130         final Actions actions = new Actions(webDriver);
131         final List<WebElement> arrowElementList = waitForAllElementsVisibility(By.xpath(XpathSelector.ARROW_DROPDOWN.getXpath()));
132         final WebElement selectedArrowElement = arrowElementList.get(arrowPosition);
133         actions.moveByOffset(20, 20).moveToElement(selectedArrowElement).pause(Duration.ofMillis(500)).perform();
134         return selectedArrowElement;
135     }
136
137     /**
138      * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
139      */
140     @AllArgsConstructor
141     private enum XpathSelector {
142         NAV("top-nav", "//nav[@class='%s']"),
143         SUB_MENU_BUTTON_HOME("sub-menu-button-home", "//*[@data-tests-id='%s']"),
144         MAIN_MENU_LINK_HOME("main-menu-button-home", "//*[@data-tests-id='%s']"),
145         ARROW_DROPDOWN("triangle-dropdown", "//li[contains(@class, '%s')]"),
146         MAIN_MENU_ONBOARD_BTN("main-menu-button-onboard", "//a[@data-tests-id='%s']"),
147         REPOSITORY_ICON("repository-icon", "//*[@data-tests-id='%s']");
148
149         @Getter
150         private final String id;
151         private final String xpathFormat;
152
153         public String getXpath() {
154             return String.format(xpathFormat, id);
155         }
156     }
157
158 }