d1b07dc32f2fd6ce255df73bf289b82f743732a6
[sdc.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.util.List;
23 import java.util.stream.Collectors;
24
25 import org.onap.sdc.frontend.ci.tests.execute.setup.ExtentTestActions;
26 import org.openqa.selenium.By;
27 import org.openqa.selenium.WebDriver;
28 import org.openqa.selenium.WebElement;
29
30 import com.aventstack.extentreports.Status;
31
32 import lombok.AllArgsConstructor;
33 import lombok.Getter;
34
35 /**
36  * Handles the Resource Properties Assignment Input Tab UI actions
37  */
38 public class ResourcePropertiesAssignmentInputTab extends AbstractPageObject {
39
40     public ResourcePropertiesAssignmentInputTab(final WebDriver webDriver) {
41         super(webDriver);
42     }
43
44     @Override
45     public void isLoaded() {
46         waitForElementVisibility(XpathSelector.INPUT_TAB.getXpath());
47         isInputPropertiesTableLoaded();
48     }
49
50     /**
51      * Creates a List of property names from the inputs tab
52      */
53     public List<String> getInputPropertyNames() {
54         isInputPropertiesTableLoaded();
55         final List<WebElement> propertyNames = findElements(By.xpath(XpathSelector.INPUT_PROPERTY_NAME.getXpath()));
56         return propertyNames.stream().map(propertyName -> propertyName.getAttribute("innerText")).collect(Collectors.toList());
57     }
58
59     /**
60      * Adds metadata to a property within the inputs tab based on a property name
61      * @param name used to determine which property to add metadata
62      * @param key the metadata key to add
63      * @param value the metadata value to add
64      */
65     public void setInputPropertyMetadata(String name, String key, String value) {
66         isInputPropertiesTableLoaded();
67         findElement(By.xpath(XpathSelector.INPUT_PROPERTY_ADD_METADATA_BUTTON.formatXpath(name))).click();
68         waitForElementVisibility(XpathSelector.INPUT_PROPERTY_METADATA_KEY_VALUE_PAIR.formatXpath(name));
69         List<WebElement> keyValueInputs = findElements(By.xpath(XpathSelector.INPUT_PROPERTY_METADATA_KEY_VALUE_PAIR.formatXpath(name)));
70         keyValueInputs.get(0).sendKeys(key);
71         keyValueInputs.get(1).sendKeys(value);
72         saveInputProperties();
73         ExtentTestActions.takeScreenshot(Status.INFO, name, String.format("Added metadata for property %s", name));
74     }
75
76     private void isInputPropertiesTableLoaded() {
77         waitForElementVisibility(XpathSelector.PROPERTIES_TABLE.getXpath());
78         waitForElementInvisibility(By.xpath(XpathSelector.NO_DATA_MESSAGE.getXpath()));
79     }
80
81     private void saveInputProperties() {
82         findElement(By.xpath(XpathSelector.PROPERTY_SAVE_BTN.getXpath())).click();
83         waitForElementVisibility(XpathSelector.PROPERTY_SAVE_MESSAGE.getXpath());
84         waitForElementInvisibility(By.xpath(XpathSelector.PROPERTY_SAVE_MESSAGE.getXpath()));
85     }
86
87     /**
88      * Checks if a input exists.
89      * @param inputName the property name
90      * @return the value of the input
91      */
92     public boolean isInputPresent(final String inputName) {
93         isInputPropertiesTableLoaded();
94         try {
95             waitForElementVisibility(By.xpath(XpathSelector.INPUT_CHECKBOX.formatXpath(inputName)), 5);
96         } catch (final Exception ignored) {
97             return false;
98         }
99         return true;
100     }
101
102     /**
103      * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
104      */
105     @AllArgsConstructor
106     @Getter
107     private enum XpathSelector {
108         INPUT_TAB("//*[contains(@data-tests-id, 'Inputs') and contains(@class, 'active')]"),
109         PROPERTIES_TABLE("//div[contains(@class,'properties-table')]"),
110         INPUT_CHECKBOX("//checkbox[@data-tests-id='%s']"),
111         NO_DATA_MESSAGE("//div[contains(@class,'no-data') and text()='No data to display']"),
112         PROPERTY_SAVE_BTN("//button[@data-tests-id='properties-save-button']"),
113         PROPERTY_SAVE_MESSAGE("//div[contains(text(), 'Successfully saved')]"),
114         INPUT_PROPERTY_NAME("//*[contains(@class, 'property-name')]"),
115         INPUT_PROPERTY_TABLE_ROW("//div[contains(@class, 'table-row') and descendant::*[text() = '%s']]"),
116         INPUT_PROPERTY_ADD_METADATA_BUTTON(INPUT_PROPERTY_TABLE_ROW.getXpath().concat("//a")),
117         INPUT_PROPERTY_METADATA_KEY_VALUE_PAIR(INPUT_PROPERTY_TABLE_ROW.getXpath().concat("//input"));
118
119         @Getter
120         private final String xpath;
121
122         public String formatXpath(Object... params) {
123             return String.format(xpath, params);
124         }
125     }
126
127 }