aboutsummaryrefslogtreecommitdiffstats
path: root/integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/pages/AbstractPageObject.java
blob: 47b655924c66be16344c8ed54cbdb36b63bce3fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */
package org.onap.sdc.frontend.ci.tests.pages;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Base UI test object that represents a page or component in a html page
 */
public abstract class AbstractPageObject implements PageObject {

    protected final WebDriver webDriver;
    protected int timeoutInSeconds;

    public AbstractPageObject(final WebDriver webDriver) {
        this.webDriver = webDriver;
        timeoutInSeconds = 20;
    }

    /**
     * Sets the default timeout for Page Object actions.
     */
    public void setTimeout(final int timeoutInSeconds) {
        this.timeoutInSeconds = timeoutInSeconds;
    }

    /**
     * Creates a WebDriverWait instance with the default timeout.
     *
     * @return a new WebDriverWait instance
     */
    protected WebDriverWait getWait() {
        return new WebDriverWait(webDriver, timeoutInSeconds);
    }

    /**
     * Creates a WebDriverWait instance with the provided timeout.
     *
     * @param timeoutInSeconds the wait timeout in seconds
     * @return a new WebDriverWait instance
     */
    protected WebDriverWait getWait(final int timeoutInSeconds) {
        return new WebDriverWait(webDriver, timeoutInSeconds);
    }

    /**
     * Find an element based on the provided locator.
     *
     * @param locator the By locator
     * @return the WebElement if found, otherwise throws an exception
     */
    protected WebElement findElement(final By locator) {
        return webDriver.findElement(locator);
    }

    /**
     * Find an element based on the provided xpath.
     *
     * @param xpath the xpath expression to search for the element
     * @return the WebElement if found, otherwise throws an exception
     */
    protected WebElement findElement(final String xpath) {
        return webDriver.findElement(By.xpath(xpath));
    }

    /**
     * Find elements based on the provided locator.
     *
     * @param locator the By locator
     * @return the list of WebElement if any found, otherwise throws an exception
     */
    protected List<WebElement> findElements(final By locator) {
        return webDriver.findElements(locator);
    }

    /**
     * Find an element inside the provided element using the provided xpath.
     *
     * @param element the parent element
     * @param xpath   the xpath expression to search for the internal element
     * @return the WebElement if found, otherwise throws an exception
     */
    protected WebElement findSubElement(final WebElement element, final String xpath) {
        return findSubElement(element, By.xpath(xpath));
    }

    /**
     * Find an element inside the provided element using the provided By locator.
     *
     * @param element the parent element
     * @param locator the By locator to search for the internal element
     * @return the WebElement if found, otherwise throws an exception
     */
    protected WebElement findSubElement(final WebElement element, final By locator) {
        return element.findElement(locator);
    }

    /**
     * Find elements inside the provided element using the provided By locator.
     *
     * @param element the parent element
     * @param locator the By locator to search for the internal element
     * @return the list of WebElement if any found, otherwise throws an exception
     */
    protected List<WebElement> findSubElements(final WebElement element, final By locator) {
        return element.findElements(locator);
    }

    /**
     * Waits for element visibility with the default timeout.
     *
     * @param xpath the xpath expression to search for the element
     * @return the WebElement if visible before timeout, otherwise throws an exception
     */
    protected WebElement waitForElementVisibility(final String xpath) {
        return waitForElementVisibility(By.xpath(xpath));
    }

    /**
     * Waits for element visibility with the default timeout.
     *
     * @param locator the By locator to search for the element
     * @return the WebElement if visible before timeout, otherwise throws an exception
     */
    protected WebElement waitForElementVisibility(final By locator) {
        return getWait(timeoutInSeconds)
            .until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

    /**
     * Waits for element visibility with the provided timeout.
     *
     * @param locator          the By locator to search for the element
     * @param timeoutInSeconds the wait timeout in seconds
     * @return the WebElement if visible before timeout, otherwise throws an exception
     */
    protected WebElement waitForElementVisibility(final By locator, final int timeoutInSeconds) {
        return getWait(timeoutInSeconds)
            .until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

    /**
     * Waits for the visibility of a list of elements matched by the locator.
     *
     * @param locator the locator to find the elements
     * @return the list of elements found if any visible
     */
    protected List<WebElement> waitForAllElementsVisibility(final By locator) {
        return getWait(timeoutInSeconds)
            .until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
    }

    /**
     * Waits for element invisibility with the default timeout.
     *
     * @param locator the By locator to search for the element
     * @return the WebElement if invisible before timeout, false otherwise
     */
    protected Boolean waitForElementInvisibility(final By locator) {
        return getWait(timeoutInSeconds)
            .until(ExpectedConditions.invisibilityOfElementLocated(locator));
    }

    /**
     * Waits for element invisibility with the provided timeout.
     *
     * @param locator          the By locator to search for the element
     * @param timeoutInSeconds the wait timeout in seconds
     * @return the WebElement if invisible before timeout, false otherwise
     */
    protected Boolean waitForElementInvisibility(final By locator, final int timeoutInSeconds) {
        return getWait(timeoutInSeconds)
            .until(ExpectedConditions.invisibilityOfElementLocated(locator));
    }

    /**
     * Waits elements to be clickable with the default timeout.
     *
     * @param xpath the xpath to find the element(s)
     * @return the WebElement if clickable before timeout, otherwise throws an exception
     */
    protected WebElement waitToBeClickable(final String xpath) {
        return waitToBeClickable(By.xpath(xpath));
    }

    /**
     * Waits elements to be clickable with the default timeout.
     *
     * @param locator the By locator to search for the element(s)
     * @return the WebElement if clickable before timeout, otherwise throws an exception
     */
    protected WebElement waitToBeClickable(final By locator) {
        return getWait().until(ExpectedConditions.elementToBeClickable(locator));
    }

}