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