aboutsummaryrefslogtreecommitdiffstats
path: root/integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/pages/AddPropertyModal.java
blob: 69680d27e6f39e1b4f4c440668ca6b79f6127ae5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.onap.sdc.frontend.ci.tests.pages;

import com.google.common.collect.ImmutableSet;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handles the Property Creation Modal UI actions
 */
public class AddPropertyModal extends AbstractPageObject {

    private static final Logger LOGGER = LoggerFactory.getLogger(AddPropertyModal.class);

    private WebElement wrappingElement;

    public AddPropertyModal(final WebDriver webDriver) {
        super(webDriver);
        timeoutInSeconds = 5;
    }

    @Override
    public void isLoaded() {
        LOGGER.debug("Finding element with xpath '{}'", XpathSelector.MODAL_XPATH.getXpath());
        wrappingElement = waitForElementVisibility(XpathSelector.MODAL_XPATH.getXpath());
    }

    /**
     * Fills the property modal creation.
     * @param propertyName the property name to be created
     * @param propertyType the property type to be selected
     */
    public void fillPropertyForm(final String propertyName, final String propertyType) {
        fillName(propertyName);
        selectType(propertyType);
        if (isComplexType(propertyType)) {
            setSchemaType();
        }
        fillDescription("Integration Test for adding property to a component");
    }

    /**
     * Clicks on the create button.
     */
    public void clickOnCreate() {
        clickElement(XpathSelector.SAVE_BTN);
    }

    /**
     * Fills the Property name.
     *
     * @param propertyName the property name
     */
    private void fillName(final String propertyName) {
        setInputValue(XpathSelector.NAME_TXT, propertyName);
    }

    /**
     * Selects a property type based on the option value
     *
     * @param propertyType the option value
     */
    private void selectType(final String propertyType) {
        setSelectValue(propertyType);
    }

    /**
     * Fills the property creation description.
     *
     * @param description the property description
     */
    private void fillDescription(final String description) {
        setInputValue(XpathSelector.DESCRIPTION_TXT, description);
    }

    /**
     * Sets Input value
     * @param inputTestId Data test id Xpath
     * @param value Input value
     */
    private void setInputValue(final XpathSelector inputTestId, final String value) {
        findSubElement(wrappingElement, By.xpath(inputTestId.getXpath())).sendKeys(value);
    }

    /**
     * Selects the option from the given propertyType value
     * @param propertyType option value to be selected
     */
    private void setSelectValue(final String propertyType) {
        new Select(findElement(By.xpath(XpathSelector.PROPERTY_TYPE_SELECT.getXpath()))).selectByVisibleText(propertyType);
    }

    /**
     * Sets Schema Type for complex types
     */
    private void setSchemaType() {
        new Select(findElement(By.xpath(XpathSelector.PROPERTY_SCHEMA_TYPE_SELECT.getXpath()))).selectByVisibleText("string");
    }

    private void clickElement(final XpathSelector elementTestId) {
        wrappingElement.findElement(By.xpath(elementTestId.getXpath())).click();
    }

    /**
     * Verifies if the given property type is a complex type
     * @param propertyType Property type
     * @return true if property type is found
     */
    private boolean isComplexType(final String propertyType) {
        return ImmutableSet.of("map", "list").contains(propertyType);
    }

    /**
     * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
     */
    @AllArgsConstructor
    private enum XpathSelector {
        MODAL_XPATH("custom-modal", "//div[contains(@class,'%s')]"),
        NAME_TXT("property-name", "//input[@data-tests-id='%s']"),
        PROPERTY_TYPE_SELECT("value-property-type", "//select[@data-tests-id='%s']"),
        PROPERTY_SCHEMA_TYPE_SELECT("value-property-schema-type", "//select[@data-tests-id='%s']"),
        PROPERTY_CHECKBOX("//checkbox[@data-tests-id='%s']"),
        DESCRIPTION_TXT("property-description", "//textarea[@data-tests-id='%s']"),
        SAVE_BTN("Save", "//*[@data-tests-id='%s']");

        @Getter
        private String id;
        private final String xpathFormat;

        XpathSelector(final String xpathFormat) {
            this.xpathFormat = xpathFormat;
        }

        public String getXpath() {
            return String.format(xpathFormat, id);
        }

        public String getXpath(final String... xpathParams) {
            return String.format(xpathFormat, xpathParams);
        }
    }

}