Provide user to specify the ouput name while declaring the atrributes
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / AddPropertyModal.java
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 com.google.common.collect.ImmutableSet;
23 import lombok.AllArgsConstructor;
24 import lombok.Getter;
25 import org.openqa.selenium.By;
26 import org.openqa.selenium.WebDriver;
27 import org.openqa.selenium.WebElement;
28 import org.openqa.selenium.support.ui.Select;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Handles the Property Creation Modal UI actions
34  */
35 public class AddPropertyModal extends AbstractPageObject {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(AddPropertyModal.class);
38
39     private WebElement wrappingElement;
40
41     public AddPropertyModal(final WebDriver webDriver) {
42         super(webDriver);
43         timeoutInSeconds = 5;
44     }
45
46     @Override
47     public void isLoaded() {
48         LOGGER.debug("Finding element with xpath '{}'", XpathSelector.MODAL_XPATH.getXpath());
49         wrappingElement = waitForElementVisibility(XpathSelector.MODAL_XPATH.getXpath());
50     }
51
52     /**
53      * Fills the property modal creation.
54      * @param propertyName the property name to be created
55      * @param propertyType the property type to be selected
56      */
57     public void fillPropertyForm(final String propertyName, final String propertyType) {
58         fillName(propertyName);
59         selectType(propertyType);
60         if (isComplexType(propertyType)) {
61             setSchemaType();
62         }
63         fillDescription("Integration Test for adding property to a component");
64     }
65
66     /**
67      * Clicks on the create button.
68      */
69     public void clickOnCreate() {
70         clickElement(XpathSelector.SAVE_BTN);
71     }
72
73     /**
74      * Fills the Property name.
75      *
76      * @param propertyName the property name
77      */
78     private void fillName(final String propertyName) {
79         setInputValue(XpathSelector.NAME_TXT, propertyName);
80     }
81
82     /**
83      * Selects a property type based on the option value
84      *
85      * @param propertyType the option value
86      */
87     private void selectType(final String propertyType) {
88         setSelectValue(propertyType);
89     }
90
91     /**
92      * Fills the property creation description.
93      *
94      * @param description the property description
95      */
96     private void fillDescription(final String description) {
97         setInputValue(XpathSelector.DESCRIPTION_TXT, description);
98     }
99
100     /**
101      * Sets Input value
102      * @param inputTestId Data test id Xpath
103      * @param value Input value
104      */
105     private void setInputValue(final XpathSelector inputTestId, final String value) {
106         findSubElement(wrappingElement, By.xpath(inputTestId.getXpath())).sendKeys(value);
107     }
108
109     /**
110      * Selects the option from the given propertyType value
111      * @param propertyType option value to be selected
112      */
113     private void setSelectValue(final String propertyType) {
114         new Select(findElement(By.xpath(XpathSelector.PROPERTY_TYPE_SELECT.getXpath()))).selectByVisibleText(propertyType);
115     }
116
117     /**
118      * Sets Schema Type for complex types
119      */
120     private void setSchemaType() {
121         new Select(findElement(By.xpath(XpathSelector.PROPERTY_SCHEMA_TYPE_SELECT.getXpath()))).selectByVisibleText("string");
122     }
123
124     private void clickElement(final XpathSelector elementTestId) {
125         wrappingElement.findElement(By.xpath(elementTestId.getXpath())).click();
126     }
127
128     /**
129      * Verifies if the given property type is a complex type
130      * @param propertyType Property type
131      * @return true if property type is found
132      */
133     private boolean isComplexType(final String propertyType) {
134         return ImmutableSet.of("map", "list").contains(propertyType);
135     }
136
137     /**
138      * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
139      */
140     @AllArgsConstructor
141     private enum XpathSelector {
142         MODAL_XPATH("custom-modal", "//div[contains(@class,'%s')]"),
143         NAME_TXT("property-name", "//input[@data-tests-id='%s']"),
144         PROPERTY_TYPE_SELECT("value-property-type", "//select[@data-tests-id='%s']"),
145         PROPERTY_SCHEMA_TYPE_SELECT("value-property-schema-type", "//select[@data-tests-id='%s']"),
146         PROPERTY_CHECKBOX("//checkbox[@data-tests-id='%s']"),
147         DESCRIPTION_TXT("property-description", "//textarea[@data-tests-id='%s']"),
148         SAVE_BTN("Save", "//*[@data-tests-id='%s']");
149
150         @Getter
151         private String id;
152         private final String xpathFormat;
153
154         XpathSelector(final String xpathFormat) {
155             this.xpathFormat = xpathFormat;
156         }
157
158         public String getXpath() {
159             return String.format(xpathFormat, id);
160         }
161
162         public String getXpath(final String... xpathParams) {
163             return String.format(xpathFormat, xpathParams);
164         }
165     }
166
167 }