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