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