Increase timeout for Selenium tests
[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
20 package org.onap.sdc.frontend.ci.tests.pages;
21
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 import java.util.List;
29
30 /**
31  * Base UI test object that represents a page or component in a html page
32  */
33 public abstract class AbstractPageObject implements PageObject {
34
35     protected final WebDriver webDriver;
36     protected int timeoutInSeconds;
37
38     public AbstractPageObject(final WebDriver webDriver) {
39         this.webDriver = webDriver;
40         timeoutInSeconds = 20;
41     }
42
43     /**
44      * Sets the default timeout for Page Object actions.
45      */
46     public void setTimeout(final int timeoutInSeconds) {
47         this.timeoutInSeconds = timeoutInSeconds;
48     }
49
50     /**
51      * Creates a WebDriverWait instance with the default timeout.
52      *
53      * @return a new WebDriverWait instance
54      */
55     protected WebDriverWait getWait() {
56         return new WebDriverWait(webDriver, timeoutInSeconds);
57     }
58
59     /**
60      * Creates a WebDriverWait instance with the provided timeout.
61      *
62      * @param timeoutInSeconds the wait timeout in seconds
63      * @return a new WebDriverWait instance
64      */
65     protected WebDriverWait getWait(final int timeoutInSeconds) {
66         return new WebDriverWait(webDriver, timeoutInSeconds);
67     }
68
69     /**
70      * Find an element based on the provided locator.
71      *
72      * @param locator the By locator
73      * @return the WebElement if found, otherwise throws an exception
74      */
75     protected WebElement findElement(final By locator) {
76         return webDriver.findElement(locator);
77     }
78
79     /**
80      * Find elements based on the provided locator.
81      *
82      * @param locator the By locator
83      * @return the list of WebElement if any found, otherwise throws an exception
84      */
85     protected List<WebElement> findElements(final By locator) {
86         return webDriver.findElements(locator);
87     }
88
89     /**
90      * Find an element inside the provided element using the provided xpath.
91      *
92      * @param element the parent element
93      * @param xpath the xpath expression to search for the internal element
94      * @return the WebElement if found, otherwise throws an exception
95      */
96     protected WebElement findSubElement(final WebElement element, final String xpath) {
97         return findSubElement(element, By.xpath(xpath));
98     }
99
100     /**
101      * Find an element inside the provided element using the provided By locator.
102      *
103      * @param element the parent element
104      * @param locator the By locator to search for the internal element
105      * @return the WebElement if found, otherwise throws an exception
106      */
107     protected WebElement findSubElement(final WebElement element, final By locator) {
108         return element.findElement(locator);
109     }
110
111     /**
112      * Find elements inside the provided element using the provided By locator.
113      *
114      * @param element the parent element
115      * @param locator the By locator to search for the internal element
116      * @return the list of WebElement if any found, otherwise throws an exception
117      */
118     protected List<WebElement> findSubElements(final WebElement element, final By locator) {
119         return element.findElements(locator);
120     }
121
122     /**
123      * Waits for element visibility with the default timeout.
124      *
125      * @param xpath the xpath expression to search for the element
126      * @return the WebElement if visible before timeout, otherwise throws an exception
127      */
128     protected WebElement waitForElementVisibility(final String xpath) {
129         return waitForElementVisibility(By.xpath(xpath));
130     }
131
132     /**
133      * Waits for element visibility with the default timeout.
134      *
135      * @param locator the By locator to search for the element
136      * @return the WebElement if visible before timeout, otherwise throws an exception
137      */
138     protected WebElement waitForElementVisibility(final By locator) {
139         return getWait(timeoutInSeconds)
140             .until(ExpectedConditions.visibilityOfElementLocated(locator));
141     }
142
143     /**
144      * Waits for element visibility with the provided timeout.
145      *
146      * @param locator the By locator to search for the element
147      * @param timeoutInSeconds the wait timeout in seconds
148      * @return the WebElement if visible before timeout, otherwise throws an exception
149      */
150     protected WebElement waitForElementVisibility(final By locator, final int timeoutInSeconds) {
151         return getWait(timeoutInSeconds)
152             .until(ExpectedConditions.visibilityOfElementLocated(locator));
153     }
154
155     /**
156      * Waits for element invisibility with the default timeout.
157      *
158      * @param locator the By locator to search for the element
159      * @return the WebElement if invisible before timeout, false otherwise
160      */
161     protected Boolean waitForElementInvisibility(final By locator) {
162         return getWait(timeoutInSeconds)
163             .until(ExpectedConditions.invisibilityOfElementLocated(locator));
164     }
165
166 }