Support complex types in interface operation inputs
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / ci / tests / pages / AbstractPageObject.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 package org.onap.sdc.frontend.ci.tests.pages;
20
21 import java.util.List;
22 import org.openqa.selenium.By;
23 import org.openqa.selenium.WebDriver;
24 import org.openqa.selenium.WebElement;
25 import org.openqa.selenium.support.ui.ExpectedConditions;
26 import org.openqa.selenium.support.ui.WebDriverWait;
27
28 /**
29  * Base UI test object that represents a page or component in a html page
30  */
31 public abstract class AbstractPageObject implements PageObject {
32
33     protected final WebDriver webDriver;
34     protected int timeoutInSeconds;
35
36     public AbstractPageObject(final WebDriver webDriver) {
37         this.webDriver = webDriver;
38         timeoutInSeconds = 20;
39     }
40
41     /**
42      * Sets the default timeout for Page Object actions.
43      */
44     public void setTimeout(final int timeoutInSeconds) {
45         this.timeoutInSeconds = timeoutInSeconds;
46     }
47
48     /**
49      * Creates a WebDriverWait instance with the default timeout.
50      *
51      * @return a new WebDriverWait instance
52      */
53     protected WebDriverWait getWait() {
54         return new WebDriverWait(webDriver, timeoutInSeconds);
55     }
56
57     /**
58      * Creates a WebDriverWait instance with the provided timeout.
59      *
60      * @param timeoutInSeconds the wait timeout in seconds
61      * @return a new WebDriverWait instance
62      */
63     protected WebDriverWait getWait(final int timeoutInSeconds) {
64         return new WebDriverWait(webDriver, timeoutInSeconds);
65     }
66
67     /**
68      * Find an element based on the provided locator.
69      *
70      * @param locator the By locator
71      * @return the WebElement if found, otherwise throws an exception
72      */
73     protected WebElement findElement(final By locator) {
74         return webDriver.findElement(locator);
75     }
76
77     /**
78      * Find an element based on the provided xpath.
79      *
80      * @param xpath the xpath expression to search for the element
81      * @return the WebElement if found, otherwise throws an exception
82      */
83     protected WebElement findElement(final String xpath) {
84         return webDriver.findElement(By.xpath(xpath));
85     }
86
87     /**
88      * Find elements based on the provided locator.
89      *
90      * @param locator the By locator
91      * @return the list of WebElement if any found, otherwise throws an exception
92      */
93     protected List<WebElement> findElements(final By locator) {
94         return webDriver.findElements(locator);
95     }
96
97     /**
98      * Find an element inside the provided element using the provided xpath.
99      *
100      * @param element the parent element
101      * @param xpath   the xpath expression to search for the internal element
102      * @return the WebElement if found, otherwise throws an exception
103      */
104     protected WebElement findSubElement(final WebElement element, final String xpath) {
105         return findSubElement(element, By.xpath(xpath));
106     }
107
108     /**
109      * Find an element inside the provided element using the provided By locator.
110      *
111      * @param element the parent element
112      * @param locator the By locator to search for the internal element
113      * @return the WebElement if found, otherwise throws an exception
114      */
115     protected WebElement findSubElement(final WebElement element, final By locator) {
116         return element.findElement(locator);
117     }
118
119     /**
120      * Find elements inside the provided element using the provided xpath.
121      *
122      * @param element the parent element
123      * @param xpath   the xpath expression to search for the internal element
124      * @return the list of WebElement if any found, otherwise throws an exception
125      */
126     protected List<WebElement> findSubElements(final WebElement element, final String xpath) {
127         return element.findElements(By.xpath(xpath));
128     }
129
130     /**
131      * Find elements inside the provided element using the provided By locator.
132      *
133      * @param element the parent element
134      * @param locator the By locator to search for the internal element
135      * @return the list of WebElement if any found, otherwise throws an exception
136      */
137     protected List<WebElement> findSubElements(final WebElement element, final By locator) {
138         return element.findElements(locator);
139     }
140
141     /**
142      * Waits for element visibility with the default timeout.
143      *
144      * @param xpath the xpath expression to search for the element
145      * @return the WebElement if visible before timeout, otherwise throws an exception
146      */
147     protected WebElement waitForElementVisibility(final String xpath) {
148         return waitForElementVisibility(By.xpath(xpath));
149     }
150
151     /**
152      * Waits for element visibility with the default timeout.
153      *
154      * @param locator the By locator to search for the element
155      * @return the WebElement if visible before timeout, otherwise throws an exception
156      */
157     protected WebElement waitForElementVisibility(final By locator) {
158         return getWait(timeoutInSeconds)
159             .until(ExpectedConditions.visibilityOfElementLocated(locator));
160     }
161
162     /**
163      * Waits for element visibility with the provided timeout.
164      *
165      * @param locator          the By locator to search for the element
166      * @param timeoutInSeconds the wait timeout in seconds
167      * @return the WebElement if visible before timeout, otherwise throws an exception
168      */
169     protected WebElement waitForElementVisibility(final By locator, final int timeoutInSeconds) {
170         return getWait(timeoutInSeconds)
171             .until(ExpectedConditions.visibilityOfElementLocated(locator));
172     }
173
174     /**
175      * Waits for the visibility of a list of elements matched by the locator.
176      *
177      * @param locator the locator to find the elements
178      * @return the list of elements found if any visible
179      */
180     protected List<WebElement> waitForAllElementsVisibility(final By locator) {
181         return getWait(timeoutInSeconds)
182             .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
183     }
184
185     /**
186      * Waits for element invisibility with the default timeout.
187      *
188      * @param locator the By locator to search for the element
189      * @return the WebElement if invisible before timeout, false otherwise
190      */
191     protected Boolean waitForElementInvisibility(final By locator) {
192         return getWait(timeoutInSeconds)
193             .until(ExpectedConditions.invisibilityOfElementLocated(locator));
194     }
195
196     /**
197      * Waits for element invisibility with the provided timeout.
198      *
199      * @param locator          the By locator to search for the element
200      * @param timeoutInSeconds the wait timeout in seconds
201      * @return the WebElement if invisible before timeout, false otherwise
202      */
203     protected Boolean waitForElementInvisibility(final By locator, final int timeoutInSeconds) {
204         return getWait(timeoutInSeconds)
205             .until(ExpectedConditions.invisibilityOfElementLocated(locator));
206     }
207
208     /**
209      * Waits elements to be clickable with the default timeout.
210      *
211      * @param xpath the xpath to find the element(s)
212      * @return the WebElement if clickable before timeout, otherwise throws an exception
213      */
214     protected WebElement waitToBeClickable(final String xpath) {
215         return waitToBeClickable(By.xpath(xpath));
216     }
217
218     /**
219      * Waits elements to be clickable with the default timeout.
220      *
221      * @param locator the By locator to search for the element(s)
222      * @return the WebElement if clickable before timeout, otherwise throws an exception
223      */
224     protected WebElement waitToBeClickable(final By locator) {
225         return getWait().until(ExpectedConditions.elementToBeClickable(locator));
226     }
227
228 }