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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
19 package org.onap.sdc.frontend.ci.tests.pages;
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;
29 * Base UI test object that represents a page or component in a html page
31 public abstract class AbstractPageObject implements PageObject {
33 protected final WebDriver webDriver;
34 protected int timeoutInSeconds;
36 public AbstractPageObject(final WebDriver webDriver) {
37 this.webDriver = webDriver;
38 timeoutInSeconds = 20;
42 * Sets the default timeout for Page Object actions.
44 public void setTimeout(final int timeoutInSeconds) {
45 this.timeoutInSeconds = timeoutInSeconds;
49 * Creates a WebDriverWait instance with the default timeout.
51 * @return a new WebDriverWait instance
53 protected WebDriverWait getWait() {
54 return new WebDriverWait(webDriver, timeoutInSeconds);
58 * Creates a WebDriverWait instance with the provided timeout.
60 * @param timeoutInSeconds the wait timeout in seconds
61 * @return a new WebDriverWait instance
63 protected WebDriverWait getWait(final int timeoutInSeconds) {
64 return new WebDriverWait(webDriver, timeoutInSeconds);
68 * Find an element based on the provided locator.
70 * @param locator the By locator
71 * @return the WebElement if found, otherwise throws an exception
73 protected WebElement findElement(final By locator) {
74 return webDriver.findElement(locator);
78 * Find an element based on the provided xpath.
80 * @param xpath the xpath expression to search for the element
81 * @return the WebElement if found, otherwise throws an exception
83 protected WebElement findElement(final String xpath) {
84 return webDriver.findElement(By.xpath(xpath));
88 * Find elements based on the provided locator.
90 * @param locator the By locator
91 * @return the list of WebElement if any found, otherwise throws an exception
93 protected List<WebElement> findElements(final By locator) {
94 return webDriver.findElements(locator);
98 * Find an element inside the provided element using the provided xpath.
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
104 protected WebElement findSubElement(final WebElement element, final String xpath) {
105 return findSubElement(element, By.xpath(xpath));
109 * Find an element inside the provided element using the provided By locator.
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
115 protected WebElement findSubElement(final WebElement element, final By locator) {
116 return element.findElement(locator);
120 * Find elements inside the provided element using the provided By locator.
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
126 protected List<WebElement> findSubElements(final WebElement element, final By locator) {
127 return element.findElements(locator);
131 * Waits for element visibility with the default timeout.
133 * @param xpath the xpath expression to search for the element
134 * @return the WebElement if visible before timeout, otherwise throws an exception
136 protected WebElement waitForElementVisibility(final String xpath) {
137 return waitForElementVisibility(By.xpath(xpath));
141 * Waits for element visibility with the default timeout.
143 * @param locator the By locator to search for the element
144 * @return the WebElement if visible before timeout, otherwise throws an exception
146 protected WebElement waitForElementVisibility(final By locator) {
147 return getWait(timeoutInSeconds)
148 .until(ExpectedConditions.visibilityOfElementLocated(locator));
152 * Waits for element visibility with the provided timeout.
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
158 protected WebElement waitForElementVisibility(final By locator, final int timeoutInSeconds) {
159 return getWait(timeoutInSeconds)
160 .until(ExpectedConditions.visibilityOfElementLocated(locator));
164 * Waits for the visibility of a list of elements matched by the locator.
166 * @param locator the locator to find the elements
167 * @return the list of elements found if any visible
169 protected List<WebElement> waitForAllElementsVisibility(final By locator) {
170 return getWait(timeoutInSeconds)
171 .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
175 * Waits for element invisibility with the default timeout.
177 * @param locator the By locator to search for the element
178 * @return the WebElement if invisible before timeout, false otherwise
180 protected Boolean waitForElementInvisibility(final By locator) {
181 return getWait(timeoutInSeconds)
182 .until(ExpectedConditions.invisibilityOfElementLocated(locator));
186 * Waits for element invisibility with the provided timeout.
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
192 protected Boolean waitForElementInvisibility(final By locator, final int timeoutInSeconds) {
193 return getWait(timeoutInSeconds)
194 .until(ExpectedConditions.invisibilityOfElementLocated(locator));
198 * Waits elements to be clickable with the default timeout.
200 * @param xpath the xpath to find the element(s)
201 * @return the WebElement if clickable before timeout, otherwise throws an exception
203 protected WebElement waitToBeClickable(final String xpath) {
204 return waitToBeClickable(By.xpath(xpath));
208 * Waits elements to be clickable with the default timeout.
210 * @param locator the By locator to search for the element(s)
211 * @return the WebElement if clickable before timeout, otherwise throws an exception
213 protected WebElement waitToBeClickable(final By locator) {
214 return getWait().until(ExpectedConditions.elementToBeClickable(locator));