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 / OutputsTabComponent.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 package org.onap.sdc.frontend.ci.tests.pages;
20
21 import lombok.AllArgsConstructor;
22 import lombok.Getter;
23 import org.openqa.selenium.By;
24 import org.openqa.selenium.WebDriver;
25
26 /**
27  * Handles the 'Outputs' Tab UI component on 'Attributes & Outputs' Page
28  */
29 public class OutputsTabComponent extends AbstractPageObject {
30
31     public OutputsTabComponent(final WebDriver webDriver) {
32         super(webDriver);
33     }
34
35     @Override
36     public void isLoaded() {
37         waitForElementVisibility(By.xpath(XpathSelector.OUTPUT_ATTRIBUTES_TABLE.getXpath()));
38         waitForElementInvisibility(By.xpath(XpathSelector.NO_DATA_MESSAGE.getXpath()));
39     }
40
41     public void deleteOutput(final String outputName) {
42         if (outputName == null) {
43             return;
44         }
45         waitForElementVisibility(By.xpath(XpathSelector.DELETE_OUTPUT_BTN.getXpath(normalizeOutputName(outputName)))).click();
46         waitToBeClickable(By.xpath(XpathSelector.DELETE_OUTPUT_CONFIRM_BTN.getXpath())).click();
47         waitForElementInvisibility(By.xpath(XpathSelector.OUTPUT_NAME_SPAN.getXpath(normalizeOutputName(outputName))), 5);
48     }
49
50     /**
51      * Checks if a output exists.
52      *
53      * @return true if exists, false if not
54      */
55     public boolean isOutputPresent(final String outputName) {
56         try {
57             waitForElementVisibility(By.xpath(XpathSelector.OUTPUT_NAME_SPAN.getXpath(outputName)));
58         } catch (final Exception ignored) {
59             return false;
60         }
61         return true;
62     }
63
64     /**
65      * Checks if a output deleted.
66      *
67      * @return true if deleted, false if not
68      */
69     public boolean isOutputDeleted(final String outputName) {
70         try {
71             findElement(By.xpath(XpathSelector.OUTPUT_NAME_SPAN.getXpath(outputName)));
72         } catch (final Exception ignored) {
73             return true;
74         }
75         return false;
76     }
77
78     private String normalizeOutputName(final String outputName) {
79         return outputName.replaceAll(" ", "").replaceAll("-", "").toLowerCase();
80     }
81
82     /**
83      * Enum that contains identifiers and xpath expressions to elements related to the enclosing page object.
84      */
85     @AllArgsConstructor
86     private enum XpathSelector {
87         DELETE_OUTPUT_BTN("//span[@data-tests-id='delete-%s']"),
88         OUTPUT_ATTRIBUTES_TABLE("output-attributes-table", "//div[contains(@class,'%s')]"),
89         NO_DATA_MESSAGE("no-data", "//div[contains(@class,'%s') and text()='No data to display']"),
90         DELETE_OUTPUT_CONFIRM_BTN("Delete", "//button[@data-tests-id='%s']"),
91         OUTPUT_NAME_SPAN("//span[contains(@class,'attribute-name') and contains(text(), '%s')]");
92
93         @Getter
94         private String id;
95         private final String xpathFormat;
96
97         XpathSelector(final String xpathFormat) {
98             this.xpathFormat = xpathFormat;
99         }
100
101         public String getXpath() {
102             return String.format(xpathFormat, id);
103         }
104
105         public String getXpath(final String... xpathParams) {
106             return String.format(xpathFormat, xpathParams);
107         }
108     }
109 }