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.openecomp.sdc.ci.tests.pages;
22 import java.util.List;
23 import org.openqa.selenium.By;
24 import org.openqa.selenium.WebDriver;
25 import org.openqa.selenium.WebElement;
26 import org.openqa.selenium.support.ui.ExpectedConditions;
27 import org.openqa.selenium.support.ui.WebDriverWait;
30 * Base UI test object that represents a page or component in a html page
32 public abstract class AbstractPageObject implements PageObject {
34 protected final WebDriver webDriver;
35 protected int timeoutInSeconds;
37 public AbstractPageObject(final WebDriver webDriver) {
38 this.webDriver = webDriver;
39 timeoutInSeconds = 10;
43 * Sets the default timeout for Page Object actions.
45 public void setTimeout(final int timeoutInSeconds) {
46 this.timeoutInSeconds = timeoutInSeconds;
50 * Creates a WebDriverWait instance with the default timeout.
52 * @return a new WebDriverWait instance
54 protected WebDriverWait getWait() {
55 return new WebDriverWait(webDriver, timeoutInSeconds);
59 * Creates a WebDriverWait instance with the provided timeout.
61 * @param timeoutInSeconds the wait timeout in seconds
62 * @return a new WebDriverWait instance
64 protected WebDriverWait getWait(final int timeoutInSeconds) {
65 return new WebDriverWait(webDriver, timeoutInSeconds);
69 * Find an element based on the provided locator.
71 * @param locator the By locator
72 * @return the WebElement if found, otherwise throws an exception
74 protected WebElement findElement(final By locator) {
75 return webDriver.findElement(locator);
79 * Find elements based on the provided locator.
81 * @param locator the By locator
82 * @return the list of WebElement if any found, otherwise throws an exception
84 protected List<WebElement> findElements(final By locator) {
85 return webDriver.findElements(locator);
89 * Find an element inside the provided element using the provided xpath.
91 * @param element the parent element
92 * @param xpath the xpath expression to search for the internal element
93 * @return the WebElement if found, otherwise throws an exception
95 protected WebElement findSubElement(final WebElement element, final String xpath) {
96 return findSubElement(element, By.xpath(xpath));
100 * Find an element inside the provided element using the provided By locator.
102 * @param element the parent element
103 * @param locator the By locator to search for the internal element
104 * @return the WebElement if found, otherwise throws an exception
106 protected WebElement findSubElement(final WebElement element, final By locator) {
107 return element.findElement(locator);
111 * Find elements inside the provided element using the provided By locator.
113 * @param element the parent element
114 * @param locator the By locator to search for the internal element
115 * @return the list of WebElement if any found, otherwise throws an exception
117 protected List<WebElement> findSubElements(final WebElement element, final By locator) {
118 return element.findElements(locator);
122 * Waits for element visibility with the default timeout.
124 * @param xpath the xpath expression to search for the element
125 * @return the WebElement if visible before timeout, otherwise throws an exception
127 protected WebElement waitForElementVisibility(final String xpath) {
128 return waitForElementVisibility(By.xpath(xpath));
132 * Waits for element visibility with the default timeout.
134 * @param locator the By locator to search for the element
135 * @return the WebElement if visible before timeout, otherwise throws an exception
137 protected WebElement waitForElementVisibility(final By locator) {
138 return getWait(timeoutInSeconds)
139 .until(ExpectedConditions.visibilityOfElementLocated(locator));
143 * Waits for element visibility with the provided timeout.
145 * @param locator the By locator to search for the element
146 * @param timeoutInSeconds the wait timeout in seconds
147 * @return the WebElement if visible before timeout, otherwise throws an exception
149 protected WebElement waitForElementVisibility(final By locator, final int timeoutInSeconds) {
150 return getWait(timeoutInSeconds)
151 .until(ExpectedConditions.visibilityOfElementLocated(locator));
155 * Waits for element invisibility with the default timeout.
157 * @param locator the By locator to search for the element
158 * @return the WebElement if invisible before timeout, false otherwise
160 protected Boolean waitForElementInvisibility(final By locator) {
161 return getWait(timeoutInSeconds)
162 .until(ExpectedConditions.invisibilityOfElementLocated(locator));