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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.sdc.frontend.ci.tests.pages;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import com.aventstack.extentreports.Status;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.concurrent.atomic.AtomicInteger;
30 import lombok.AllArgsConstructor;
32 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
33 import org.onap.sdc.frontend.ci.tests.utilities.LoaderHelper;
34 import org.onap.sdc.frontend.ci.tests.utilities.NotificationComponent;
35 import org.onap.sdc.frontend.ci.tests.utilities.NotificationComponent.NotificationType;
36 import org.openqa.selenium.By;
37 import org.openqa.selenium.WebDriver;
38 import org.openqa.selenium.WebElement;
39 import org.openqa.selenium.support.ui.ExpectedConditions;
40 import org.openqa.selenium.support.ui.Select;
43 * Handles the Resource Properties Assignment Page UI actions
45 public class ResourcePropertiesAssignmentPage extends AbstractPageObject {
47 private WebElement wrappingElement;
48 private LoaderHelper loaderHelper;
49 private NotificationComponent notificationComponent;
51 public ResourcePropertiesAssignmentPage(final WebDriver webDriver) {
53 notificationComponent = new NotificationComponent(webDriver);
54 loaderHelper = new LoaderHelper(webDriver);
58 public void isLoaded() {
59 wrappingElement = getWait(5)
60 .until(ExpectedConditions.visibilityOfElementLocated(By.xpath(XpathSelector.MAIN_DIV.getXpath())));
62 .until(ExpectedConditions.visibilityOfElementLocated(By.xpath(XpathSelector.TITLE_DIV.getXpath())));
66 * Gets the software_version property values.
68 * @return the list of software versions found
70 public List<String> getSoftwareVersionProperty() {
71 waitPropertiesToLoad();
72 final By swVersionCheckboxLocator = By.xpath(XpathSelector.SOFTWARE_VERSION_PROPERTY_CHECKBOX.getXpath());
73 waitForElementVisibility(swVersionCheckboxLocator, 5);
75 final List<String> softwareVersionList = new ArrayList<>();
76 final List<WebElement> elements = wrappingElement.findElements(By.xpath(XpathSelector.SOFTWARE_VERSION_INPUT.getXpath()));
77 for (final WebElement element : elements) {
78 softwareVersionList.add(element.getAttribute("value"));
81 return softwareVersionList;
85 * Gets the property row element for the given propertyName
86 * @param propertyName the property name
87 * @return the property row element
89 private WebElement getPropertyRow(String propertyName) {
90 final By propertyCheckboxLocator = By.xpath(XpathSelector.PROPERTY_CHECKBOX.getXpath(propertyName));
91 final WebElement propertyCheckbox = waitForElementVisibility(propertyCheckboxLocator, 5);
92 return propertyCheckbox.findElement(By.xpath("./../../.."));
96 * Gets the value of a string TOSCA property.
97 * @return the value of the property
99 public String getStringPropertyValue(final String propertyName) {
100 waitPropertiesToLoad();
101 final WebElement propertyInput = getPropertyRow(propertyName).findElement(By.xpath(XpathSelector.INPUT_PROPERTY.getXpath(propertyName)));
102 return propertyInput.getAttribute("value");
106 * Sets a String value to a TOSCA property.
107 * @param propertyName the property name
108 * @param value property value to be set
110 public void setStringPropertyValue(final String propertyName, final String value) {
114 waitPropertiesToLoad();
115 getPropertyRow(propertyName).findElement(By.xpath(XpathSelector.INPUT_PROPERTY.getXpath(propertyName))).sendKeys(value);
119 * Sets a boolean value to a TOSCA property.
120 * @param propertyName
122 private void setBooleanPropertyValue(final String propertyName) {
123 waitPropertiesToLoad();
124 new Select(getPropertyRow(propertyName).findElement(By.xpath(XpathSelector.SELECT_INPUT_PROPERTY.getXpath(propertyName)))).
125 selectByVisibleText("TRUE");
129 * Sets a complex property type value to a TOSCA property. It handles List and Map
130 * @param propertyName the property name
131 * @param objectValue the property complex type value
133 public void setComplexPropertyValue(final String propertyName, final Object objectValue) {
134 if (objectValue == null) {
137 waitPropertiesToLoad();
138 final WebElement addToListLink = getPropertyRow(propertyName)
139 .findElement(By.xpath(XpathSelector.PROPERTY_ADD_VALUE_COMPLEX_TYPE.getXpath(propertyName)));
140 if (objectValue instanceof List) {
141 setValueFromList(propertyName, (List<String>) objectValue, addToListLink);
143 if (objectValue instanceof Map) {
144 setValueFromMap(propertyName, (Map) objectValue, addToListLink);
149 * Sets a value to a complex (List) property type.
150 * @param propertyName the property name
151 * @param values the List of values to be added to the given property name
152 * @param addToListLink the link to add the input value
154 private void setValueFromList(final String propertyName, final List<String> values, final WebElement addToListLink) {
155 AtomicInteger inputIndex = new AtomicInteger(0);
156 values.forEach(value -> {
157 addToListLink.click();
158 final WebElement propertyInput = addToListLink.findElement(By.xpath(XpathSelector.INPUT_PROPERTY_COMPLEX_TYPE_VALUE.getXpath(
159 String.valueOf(new StringBuilder(propertyName).append(".").append(inputIndex)))));
160 propertyInput.sendKeys(value);
161 inputIndex.getAndIncrement();
166 * Sets a value to a complex (Map) property type.
167 * @param propertyName the property name
168 * @param values the Map of values to be added to the given property name
169 * @param addToListLink the link to add the input value
171 private void setValueFromMap(final String propertyName, final Map values, final WebElement addToListLink) {
172 AtomicInteger inputIndex = new AtomicInteger(0);
173 values.forEach((key, value) -> {
174 addToListLink.click();
175 WebElement propertyInput;
177 propertyInput = addToListLink.findElement(By.xpath(XpathSelector.INPUT_PROPERTY_COMPLEX_TYPE_KEY.getXpath(
178 String.valueOf(new StringBuilder(propertyName).append(".").append(inputIndex)))));
179 propertyInput.sendKeys(key.toString());
181 propertyInput = addToListLink.findElement(By.xpath(XpathSelector.INPUT_PROPERTY_COMPLEX_TYPE_VALUE.getXpath(
182 String.valueOf(new StringBuilder(propertyName).append(".").append(inputIndex)))));
183 propertyInput.sendKeys(value.toString());
184 inputIndex.getAndIncrement();
189 * Sets a property value
190 * @param propertyName the property name
191 * @param value the property value
193 public void setPropertyValue(final String propertyName, final Object value) {
198 if (value instanceof String) {
199 setStringPropertyValue(propertyName, (String) value);
203 if (value instanceof Integer) {
204 setStringPropertyValue(propertyName, ((Integer) value).toString());
208 if (value instanceof Boolean) {
209 setBooleanPropertyValue(propertyName);
213 if (value instanceof Map) {
214 setComplexPropertyValue(propertyName, value);
218 if (value instanceof List) {
219 setComplexPropertyValue(propertyName, value);
223 throw new UnsupportedOperationException("Cannot set property value of type: " + value.getClass());
227 * Checks if a property exists.
228 * @param propertyName the property name
229 * @return the value of the property
231 public boolean isPropertyPresent(final String propertyName) {
232 waitPropertiesToLoad();
234 waitForElementVisibility(By.xpath(XpathSelector.PROPERTY_CHECKBOX.getXpath(propertyName)), 5);
235 } catch (final Exception ignored) {
242 * Waits for the properties loading.
244 private void waitPropertiesToLoad() {
245 waitForElementVisibility(By.xpath(XpathSelector.PROPERTIES_TABLE.getXpath()), 5);
246 waitForElementInvisibility(By.xpath(XpathSelector.NO_DATA_MESSAGE.getXpath()), 5);
252 public void saveProperties() {
253 final WebElement saveBtn = waitForElementVisibility(By.xpath(XpathSelector.PROPERTY_SAVE_BTN.getXpath()));
254 assertTrue(saveBtn.isEnabled(), "Property save button should be enabled.");
256 loaderHelper.waitForLoaderInvisibility(20);
257 notificationComponent.waitForNotification(NotificationType.SUCCESS, 20);
262 * @param propertiesMap the properties map to be added
264 public void addProperties(final Map<String, String> propertiesMap) {
265 waitPropertiesToLoad();
266 propertiesMap.forEach((propertyName, propertyType) -> {
267 final By addPropertyButtonLocator = By.xpath(XpathSelector.PROPERTY_ADD_BTN.getXpath());
268 waitForElementVisibility(addPropertyButtonLocator, 30);
269 final WebElement addPropertyRightColumn = findElement(By.xpath(XpathSelector.PROPERTY_ADD_RIGHT_COLUMN_DIV.getXpath()));
270 final WebElement propertyAddButton = addPropertyRightColumn.findElement(addPropertyButtonLocator);
271 assertTrue(propertyAddButton.isDisplayed(), "Property add button should be enabled.");
272 propertyAddButton.click();
273 createProperty(propertyName, propertyType);
274 verifyProperty(propertyName);
275 ExtentTestActions.takeScreenshot(Status.INFO, "added-property",
276 String.format("Property '%s' was created on component", propertyName));
281 * Fills the creation property modal.
282 * @param propertyName the property name to be created
283 * @param propertyType the property type to be selected
285 private void createProperty(final String propertyName, final String propertyType) {
286 final AddPropertyModal addPropertyModal = new AddPropertyModal(webDriver);
287 addPropertyModal.isLoaded();
288 addPropertyModal.fillPropertyForm(propertyName, propertyType);
289 addPropertyModal.clickOnCreate();
293 * Verifies if the added property is displayed on the UI.
294 * @param propertyName the property name to be found
296 private void verifyProperty(final String propertyName) {
297 final By propertyCheckboxLocator = By.xpath(XpathSelector.PROPERTY_CHECKBOX.getXpath(propertyName));
298 final WebElement propertyCheckbox = waitForElementVisibility(propertyCheckboxLocator, 5);
299 assertTrue(propertyCheckbox.isDisplayed(), String.format("%s Property should be displayed", propertyName));
300 assertTrue(this.getPropertyNamesAndTypes().containsKey(propertyName),
301 String.format("%s Property should be listed but found %s", propertyName, this.getPropertyNamesAndTypes().toString()));
305 * Creates a map based on property names and data types
307 public Map<String, String> getPropertyNamesAndTypes() {
308 waitPropertiesToLoad();
309 final Map<String, String> namesAndTypes = new HashMap<>();
310 final List<WebElement> names = findElements(By.xpath(XpathSelector.PROPERTY_NAMES.getXpath()));
311 final List<WebElement> types = findElements(By.xpath(XpathSelector.PROPERTY_TYPES.getXpath()));
313 for (int i = 0;i < names.size();i++) {
314 namesAndTypes.put(names.get(i).getAttribute("innerText"), types.get(i).getAttribute("innerText"));
317 return namesAndTypes;
321 * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
324 private enum XpathSelector {
325 MAIN_DIV("w-sdc-main-right-container", "//div[@class='%s']"),
326 TITLE_DIV("tab-title", "//div[contains(@class,'%s') and contains(text(), 'Properties Assignment')]"),
327 PROPERTIES_TABLE("properties-table", "//div[contains(@class,'%s')]"),
328 NO_DATA_MESSAGE("no-data", "//div[contains(@class,'%s') and text()='No data to display']"),
329 SOFTWARE_VERSION_PROPERTY_CHECKBOX("software_versions", "//checkbox[@data-tests-id='%s']"),
330 SOFTWARE_VERSION_INPUT("value-prop-software_versions", "//input[starts-with(@data-tests-id,'%s')]"),
331 PROPERTY_CHECKBOX("//checkbox[@data-tests-id='%s']"),
332 PROPERTY_SAVE_BTN("properties-save-button", "//button[@data-tests-id='%s']"),
333 PROPERTY_ADD_RIGHT_COLUMN_DIV("right-column", "//div[@class='%s']"),
334 PROPERTY_ADD_BTN("add-btn", "//div[contains(@class,'%s')]"),
335 PROPERTY_ADD_VALUE_COMPLEX_TYPE("//a[contains(@data-tests-id, 'add-to-list-%s')]"),
336 INPUT_PROPERTY_COMPLEX_TYPE_KEY("//input[contains(@data-tests-id, 'value-prop-key-%s')]"),
337 INPUT_PROPERTY_COMPLEX_TYPE_VALUE("//input[contains(@data-tests-id, 'value-prop-%s')]"),
338 INPUT_PROPERTY("//input[@data-tests-id='value-prop-%s']"),
339 SELECT_INPUT_PROPERTY("//select[@data-tests-id='value-prop-%s']"),
340 PROPERTY_TYPES("//*[contains(@data-tests-id, 'propertyType')]"),
341 PROPERTY_NAMES("//*[contains(@data-tests-id, 'propertyName')]");
345 private final String xpathFormat;
347 XpathSelector(final String xpathFormat) {
348 this.xpathFormat = xpathFormat;
351 public String getXpath() {
352 return String.format(xpathFormat, id);
355 public String getXpath(final String... xpathParams) {
356 return String.format(xpathFormat, xpathParams);