bf99e03f9fc516df6c101dd9fb41f0478aeba5f3
[sdc.git] /
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 static org.junit.jupiter.api.Assertions.assertTrue;
23 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.INPUT_PROPERTY;
24 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.MAIN_DIV;
25 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.NO_DATA_MESSAGE;
26 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.PROPERTIES_TABLE;
27 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.PROPERTY_CHECKBOX;
28 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.PROPERTY_SAVE_BTN;
29 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.SOFTWARE_VERSION_INPUT;
30 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.SOFTWARE_VERSION_PROPERTY_CHECKBOX;
31 import static org.onap.sdc.frontend.ci.tests.pages.ResourcePropertiesAssignmentPage.XpathSelector.TITLE_DIV;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import lombok.AllArgsConstructor;
36 import lombok.Getter;
37 import org.onap.sdc.frontend.ci.tests.utilities.LoaderHelper;
38 import org.onap.sdc.frontend.ci.tests.utilities.NotificationComponent;
39 import org.onap.sdc.frontend.ci.tests.utilities.NotificationComponent.NotificationType;
40 import org.openqa.selenium.By;
41 import org.openqa.selenium.WebDriver;
42 import org.openqa.selenium.WebElement;
43 import org.openqa.selenium.support.ui.ExpectedConditions;
44
45 /**
46  * Handles the Resource Properties Assignment Page UI actions
47  */
48 public class ResourcePropertiesAssignmentPage extends AbstractPageObject {
49
50     private WebElement wrappingElement;
51     private LoaderHelper loaderHelper;
52     private NotificationComponent notificationComponent;
53
54     public ResourcePropertiesAssignmentPage(final WebDriver webDriver) {
55         super(webDriver);
56         notificationComponent = new NotificationComponent(webDriver);
57         loaderHelper = new LoaderHelper(webDriver);
58     }
59
60     @Override
61     public void isLoaded() {
62         wrappingElement = getWait(5)
63             .until(ExpectedConditions.visibilityOfElementLocated(By.xpath(MAIN_DIV.getXpath())));
64         getWait(5)
65             .until(ExpectedConditions.visibilityOfElementLocated(By.xpath(TITLE_DIV.getXpath())));
66     }
67
68     /**
69      * Gets the software_version property values.
70      *
71      * @return the list of software versions found
72      */
73     public List<String> getSoftwareVersionProperty() {
74         waitPropertiesToLoad();
75         final By swVersionCheckboxLocator = By.xpath(SOFTWARE_VERSION_PROPERTY_CHECKBOX.getXpath());
76         waitForElementVisibility(swVersionCheckboxLocator, 5);
77
78         final List<String> softwareVersionList = new ArrayList<>();
79         final List<WebElement> elements = wrappingElement.findElements(By.xpath(SOFTWARE_VERSION_INPUT.getXpath()));
80         for (final WebElement element : elements) {
81             softwareVersionList.add(element.getAttribute("value"));
82         }
83
84         return softwareVersionList;
85     }
86
87     /**
88      * Gets the value of a string TOSCA property.
89      *
90      * @return the value of the property
91      */
92     public String getStringPropertyValue(final String propertyName) {
93         waitPropertiesToLoad();
94         final By propertyCheckboxLocator = By.xpath(PROPERTY_CHECKBOX.getXpath(propertyName));
95         final WebElement propertyCheckbox = waitForElementVisibility(propertyCheckboxLocator, 5);
96         final WebElement propertyRow = propertyCheckbox.findElement(By.xpath("./../../.."));
97         final WebElement propertyInput = propertyRow.findElement(By.xpath(INPUT_PROPERTY.getXpath(propertyName)));
98         return propertyInput.getAttribute("value");
99     }
100
101     /**
102      * Set a value to a TOSCA string property.
103      */
104     public void setStringPropertyValue(final String propertyName, final String value) {
105         if (value == null) {
106             return;
107         }
108         waitPropertiesToLoad();
109         final By propertyCheckboxLocator = By.xpath(PROPERTY_CHECKBOX.getXpath(propertyName));
110         final WebElement propertyCheckbox = waitForElementVisibility(propertyCheckboxLocator, 5);
111         final WebElement propertyRow = propertyCheckbox.findElement(By.xpath("./../../.."));
112         final WebElement propertyInput = propertyRow.findElement(By.xpath(INPUT_PROPERTY.getXpath(propertyName)));
113         propertyInput.sendKeys(value);
114     }
115
116     public void setPropertyValue(final String propertyName, final Object value) {
117         if (value == null) {
118             return;
119         }
120
121         if (value instanceof String) {
122             setStringPropertyValue(propertyName, (String) value);
123             return;
124         }
125
126         if (value instanceof Integer) {
127             setStringPropertyValue(propertyName, ((Integer) value).toString());
128             return;
129         }
130
131         if (value instanceof Boolean) {
132             setStringPropertyValue(propertyName, ((Boolean) value).toString());
133             return;
134         }
135
136         throw new UnsupportedOperationException("Cannot set property value of type: " + value.getClass());
137     }
138
139     /**
140      * Checks if a property exists.
141      *
142      * @return the value of the property
143      */
144     public boolean isPropertyPresent(final String propertyName) {
145         waitPropertiesToLoad();
146         try {
147             waitForElementVisibility(By.xpath(PROPERTY_CHECKBOX.getXpath(propertyName)), 5);
148         } catch (final Exception ignored) {
149             return false;
150         }
151         return true;
152     }
153
154     /**
155      * Waits for the properties loading.
156      */
157     private void waitPropertiesToLoad() {
158         waitForElementVisibility(By.xpath(PROPERTIES_TABLE.getXpath()), 5);
159         waitForElementInvisibility(By.xpath(NO_DATA_MESSAGE.getXpath()), 5);
160     }
161
162     public void saveProperties() {
163         final WebElement saveBtn = waitForElementVisibility(By.xpath(PROPERTY_SAVE_BTN.getXpath()));
164         assertTrue(saveBtn.isEnabled(), "Property save button should be enabled.");
165         saveBtn.click();
166         loaderHelper.waitForLoader(20);
167         notificationComponent.waitForNotification(NotificationType.SUCCESS, 20);
168     }
169
170     /**
171      * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
172      */
173     @AllArgsConstructor
174     public enum XpathSelector {
175         MAIN_DIV("w-sdc-main-right-container", "//div[@class='%s']"),
176         TITLE_DIV("tab-title", "//div[contains(@class,'%s') and contains(text(), 'Properties Assignment')]"),
177         PROPERTIES_TABLE("properties-table", "//div[contains(@class,'%s')]"),
178         NO_DATA_MESSAGE("no-data", "//div[contains(@class,'%s') and text()='No data to display']"),
179         SOFTWARE_VERSION_PROPERTY_CHECKBOX("software_versions", "//checkbox[@data-tests-id='%s']"),
180         SOFTWARE_VERSION_INPUT("value-prop-software_versions", "//input[starts-with(@data-tests-id,'%s')]"),
181         PROPERTY_CHECKBOX("//checkbox[@data-tests-id='%s']"),
182         PROPERTY_SAVE_BTN("properties-save-button", "//button[@data-tests-id='%s']"),
183         INPUT_PROPERTY("//input[@data-tests-id='value-prop-%s']");
184
185         @Getter
186         private String id;
187         private final String xpathFormat;
188
189         XpathSelector(final String xpathFormat) {
190             this.xpathFormat = xpathFormat;
191         }
192
193         public String getXpath() {
194             return String.format(xpathFormat, id);
195         }
196
197         public String getXpath(final String... xpathParams) {
198             return String.format(xpathFormat, xpathParams);
199         }
200     }
201
202 }