47b655924c66be16344c8ed54cbdb36b63bce3fa
[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 By locator.
121      *
122      * @param element the parent element
123      * @param locator the By locator 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 By locator) {
127         return element.findElements(locator);
128     }
129
130     /**
131      * Waits for element visibility with the default timeout.
132      *
133      * @param xpath the xpath expression to search for the element
134      * @return the WebElement if visible before timeout, otherwise throws an exception
135      */
136     protected WebElement waitForElementVisibility(final String xpath) {
137         return waitForElementVisibility(By.xpath(xpath));
138     }
139
140     /**
141      * Waits for element visibility with the default timeout.
142      *
143      * @param locator the By locator to search for the element
144      * @return the WebElement if visible before timeout, otherwise throws an exception
145      */
146     protected WebElement waitForElementVisibility(final By locator) {
147         return getWait(timeoutInSeconds)
148             .until(ExpectedConditions.visibilityOfElementLocated(locator));
149     }
150
151     /**
152      * Waits for element visibility with the provided timeout.
153      *
154      * @param locator          the By locator to search for the element
155      * @param timeoutInSeconds the wait timeout in seconds
156      * @return the WebElement if visible before timeout, otherwise throws an exception
157      */
158     protected WebElement waitForElementVisibility(final By locator, final int timeoutInSeconds) {
159         return getWait(timeoutInSeconds)
160             .until(ExpectedConditions.visibilityOfElementLocated(locator));
161     }
162
163     /**
164      * Waits for the visibility of a list of elements matched by the locator.
165      *
166      * @param locator the locator to find the elements
167      * @return the list of elements found if any visible
168      */
169     protected List<WebElement> waitForAllElementsVisibility(final By locator) {
170         return getWait(timeoutInSeconds)
171             .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
172     }
173
174     /**
175      * Waits for element invisibility with the default timeout.
176      *
177      * @param locator the By locator to search for the element
178      * @return the WebElement if invisible before timeout, false otherwise
179      */
180     protected Boolean waitForElementInvisibility(final By locator) {
181         return getWait(timeoutInSeconds)
182             .until(ExpectedConditions.invisibilityOfElementLocated(locator));
183     }
184
185     /**
186      * Waits for element invisibility with the provided timeout.
187      *
188      * @param locator          the By locator to search for the element
189      * @param timeoutInSeconds the wait timeout in seconds
190      * @return the WebElement if invisible before timeout, false otherwise
191      */
192     protected Boolean waitForElementInvisibility(final By locator, final int timeoutInSeconds) {
193         return getWait(timeoutInSeconds)
194             .until(ExpectedConditions.invisibilityOfElementLocated(locator));
195     }
196
197     /**
198      * Waits elements to be clickable with the default timeout.
199      *
200      * @param xpath the xpath to find the element(s)
201      * @return the WebElement if clickable before timeout, otherwise throws an exception
202      */
203     protected WebElement waitToBeClickable(final String xpath) {
204         return waitToBeClickable(By.xpath(xpath));
205     }
206
207     /**
208      * Waits elements to be clickable with the default timeout.
209      *
210      * @param locator the By locator to search for the element(s)
211      * @return the WebElement if clickable before timeout, otherwise throws an exception
212      */
213     protected WebElement waitToBeClickable(final By locator) {
214         return getWait().until(ExpectedConditions.elementToBeClickable(locator));
215     }
216
217 }