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=========================================================
20 package org.onap.sdc.frontend.ci.tests.pages;
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;
28 import java.util.List;
31 * Base UI test object that represents a page or component in a html page
33 public abstract class AbstractPageObject implements PageObject {
35 protected final WebDriver webDriver;
36 protected int timeoutInSeconds;
38 public AbstractPageObject(final WebDriver webDriver) {
39 this.webDriver = webDriver;
40 timeoutInSeconds = 20;
44 * Sets the default timeout for Page Object actions.
46 public void setTimeout(final int timeoutInSeconds) {
47 this.timeoutInSeconds = timeoutInSeconds;
51 * Creates a WebDriverWait instance with the default timeout.
53 * @return a new WebDriverWait instance
55 protected WebDriverWait getWait() {
56 return new WebDriverWait(webDriver, timeoutInSeconds);
60 * Creates a WebDriverWait instance with the provided timeout.
62 * @param timeoutInSeconds the wait timeout in seconds
63 * @return a new WebDriverWait instance
65 protected WebDriverWait getWait(final int timeoutInSeconds) {
66 return new WebDriverWait(webDriver, timeoutInSeconds);
70 * Find an element based on the provided locator.
72 * @param locator the By locator
73 * @return the WebElement if found, otherwise throws an exception
75 protected WebElement findElement(final By locator) {
76 return webDriver.findElement(locator);
80 * Find elements based on the provided locator.
82 * @param locator the By locator
83 * @return the list of WebElement if any found, otherwise throws an exception
85 protected List<WebElement> findElements(final By locator) {
86 return webDriver.findElements(locator);
90 * Find an element inside the provided element using the provided xpath.
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
96 protected WebElement findSubElement(final WebElement element, final String xpath) {
97 return findSubElement(element, By.xpath(xpath));
101 * Find an element inside the provided element using the provided By locator.
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
107 protected WebElement findSubElement(final WebElement element, final By locator) {
108 return element.findElement(locator);
112 * Find elements inside the provided element using the provided By locator.
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
118 protected List<WebElement> findSubElements(final WebElement element, final By locator) {
119 return element.findElements(locator);
123 * Waits for element visibility with the default timeout.
125 * @param xpath the xpath expression to search for the element
126 * @return the WebElement if visible before timeout, otherwise throws an exception
128 protected WebElement waitForElementVisibility(final String xpath) {
129 return waitForElementVisibility(By.xpath(xpath));
133 * Waits for element visibility with the default timeout.
135 * @param locator the By locator to search for the element
136 * @return the WebElement if visible before timeout, otherwise throws an exception
138 protected WebElement waitForElementVisibility(final By locator) {
139 return getWait(timeoutInSeconds)
140 .until(ExpectedConditions.visibilityOfElementLocated(locator));
144 * Waits for element visibility with the provided timeout.
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
150 protected WebElement waitForElementVisibility(final By locator, final int timeoutInSeconds) {
151 return getWait(timeoutInSeconds)
152 .until(ExpectedConditions.visibilityOfElementLocated(locator));
156 * Waits for element invisibility with the default timeout.
158 * @param locator the By locator to search for the element
159 * @return the WebElement if invisible before timeout, false otherwise
161 protected Boolean waitForElementInvisibility(final By locator) {
162 return getWait(timeoutInSeconds)
163 .until(ExpectedConditions.invisibilityOfElementLocated(locator));