Catalog alignment
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / utilities / NotificationHelper.java
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.openecomp.sdc.ci.tests.utilities;
21
22 import org.openecomp.sdc.ci.tests.execute.setup.DriverFactory;
23 import org.openqa.selenium.By;
24 import org.openqa.selenium.support.ui.ExpectedConditions;
25 import org.openqa.selenium.support.ui.WebDriverWait;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import static org.openecomp.sdc.ci.tests.utilities.NotificationHelper.XpathSelector.MAIN_CONTAINER_DIV;
30 import static org.openecomp.sdc.ci.tests.utilities.NotificationHelper.XpathSelector.MESSAGE_CONTENT_DIV;
31 import static org.openecomp.sdc.ci.tests.utilities.NotificationHelper.XpathSelector.MESSAGE_SUCCESS_DIV;
32
33 public class NotificationHelper {
34
35     private static final Logger LOGGER = LoggerFactory.getLogger(NotificationHelper.class);
36
37     public void waitForNotification(final NotificationType notificationType, final int timeout) {
38         final By messageLocator = getMessageLocator(notificationType);
39         waitForVisibility(messageLocator, timeout);
40         waitForInvisibility(messageLocator, timeout);
41     }
42
43     private By getMessageLocator(final NotificationType notificationType) {
44         return By.xpath(getMessageXpath(notificationType));
45     }
46
47     private String getMessageXpath(final NotificationType notificationType) {
48         if (notificationType == NotificationType.SUCCESS) {
49             return String.format("%s%s%s", MAIN_CONTAINER_DIV.getXpath(), MESSAGE_CONTENT_DIV.getXpath(), MESSAGE_SUCCESS_DIV.getXpath());
50         }
51
52         LOGGER.warn("Xpath for NotificationType {} not yet implemented. Returning empty Xpath.", notificationType);
53         return "";
54     }
55
56     private void waitForVisibility(By messageLocator, final int timeout) {
57         getWait(timeout)
58             .until(ExpectedConditions.visibilityOfElementLocated(messageLocator));
59     }
60
61     private void waitForInvisibility(By messageLocator, int timeout) {
62         getWait(timeout)
63             .until(ExpectedConditions.invisibilityOfElementLocated(messageLocator));
64     }
65
66     private WebDriverWait getWait(final int timeout) {
67         return new WebDriverWait(DriverFactory.getDriver(), timeout);
68     }
69
70     public enum XpathSelector {
71         MAIN_CONTAINER_DIV("notification-container", "//div[@class='%s']"),
72         MESSAGE_CONTENT_DIV("msg-content", "//div[@class='%s']"),
73         MESSAGE_SUCCESS_DIV("message", "//div[contains(@class, '%s') and contains(text(),'successfully')]");
74
75         private final String id;
76         private final String xpath;
77
78         XpathSelector(String id, String xpath) {
79             this.id = id;
80             this.xpath = xpath;
81         }
82
83         public String getId() {
84             return id;
85         }
86
87         public String getXpath() {
88             return String.format(xpath, id);
89         }
90     }
91
92     public enum NotificationType {
93         SUCCESS;
94     }
95
96 }