-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseComponent.java
More file actions
366 lines (334 loc) · 13 KB
/
BaseComponent.java
File metadata and controls
366 lines (334 loc) · 13 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
*
* Copyright © 2024 Applause App Quality, Inc.
*
* 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.
*
*/
package com.applause.auto.pageobjectmodel.base;
import com.applause.auto.context.IPageObjectContext;
import com.applause.auto.data.enums.SwipeDirection;
import com.applause.auto.pageobjectmodel.builder.PageObjectBuilder;
import com.applause.auto.pageobjectmodel.elements.ContainerElement;
import com.applause.auto.pageobjectmodel.factory.LazyWebElement;
import com.applause.auto.pageobjectmodel.factory.Locator;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Duration;
import java.util.List;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
/**
* An abstract collection of BaseElements and BaseComponents representing a logical grouping of HTML
* elements in the DOM. Classes extending BaseComponent can be used for collections of elements
* large and small, from a complete page object to a small group of Buttons. Components are
* instantiated with a factory pattern by calling {@link PageObjectBuilder} with the class as a
* parameter, whereupon a new object of the class will be created and all subcomponents and elements
* will be populated. BaseComponents can optionally be created with a parent element using {@link
* PageObjectBuilder}, in which case all subcomponents and elements contained within the
* BaseComponent will be found in the scope of the parent element.
*
* @see BaseElement
* @see com.applause.auto.pageobjectmodel.annotation.Implementation
* @see PageObjectBuilder
*/
public abstract class BaseComponent implements UIElement {
/** A Logger that looks up the classname with any ByteBuddy naming removed */
protected final Logger logger =
getClass().getSimpleName().contains("$ByteBuddy")
? LogManager.getLogger(
getClass()
.getSimpleName()
.substring(0, getClass().getSimpleName().indexOf("$ByteBuddy")))
: LogManager.getLogger();
/** -- GETTER -- Gets the underlying element of this component. */
@Getter @Setter private @Nullable LazyWebElement underlying;
@Getter
@Setter
@SuppressFBWarnings(
value = "NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR",
justification = "Field is set by PageObjectBuilder after construction.")
private @NonNull IPageObjectContext context;
/**
* Post-creation lifecycle method for the BaseComponent. afterInit() is called by
* PageObjectBuilder at component creation, immediately after elements are populated.
*/
@SuppressWarnings("PMD.EmptyMethodInAbstractClassShouldBeAbstract")
public void afterInit() {
// Do nothing. Override this method if you need to.
}
@Override
public final Locator getLocator() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get locator for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying.getLocator();
}
@Override
public final Object[] getFormatArgs() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get format arguments for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying.getFormatArgs();
}
@Override
public void initialize() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot initialize component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
this.underlying.initialize();
}
@Override
public boolean isInitialized() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot initialize component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying.isInitialized();
}
@Override
public BaseComponent format(final Object... formatArgs) {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot format component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
this.underlying.format(formatArgs);
return this;
}
@Override
public <T extends UIElement> T getChild(final Locator locator, final Class<T> type) {
return PageObjectBuilder.withContext(context)
.forUiElement(type)
.withParent(this)
.initialize(locator);
}
@Override
public <T extends UIElement> T getChild(final By locator, final Class<T> type) {
return getChild(new Locator(locator, context.getPlatform()), type);
}
@Override
public ContainerElement getChild(final By locator) {
return this.getChild(locator, ContainerElement.class);
}
@Override
public <T extends UIElement> List<T> getChildren(final Locator locator, final Class<T> type) {
return PageObjectBuilder.withContext(context)
.forUiElement(type)
.withParent(this)
.initializeList(locator);
}
@Override
public <T extends UIElement> List<T> getChildren(final By locator, final Class<T> type) {
return getChildren(new Locator(locator, context.getPlatform()), type);
}
/**
* Gets a List of child elements or components of the underlying element using a By locator.
* Returns a generic ContainerElement.
*
* @param locator the By locator for the child.
* @return the child element or component
*/
@Override
public List<ContainerElement> getChildren(final By locator) {
return this.getChildren(locator, ContainerElement.class);
}
/**
* Links the configured shadowRoot host to a shadowDom with the current page (BaseComponent)
*
* @param <T> the child BaseComponent
* @return the child BaseComponent linked to the underlying element
*/
@SuppressWarnings({"unchecked"})
public <T extends BaseComponent> T linkShadowRoot() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot link shadow root for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
if (!this.underlying.getLocator().isShadowRoot()) {
logger.warn(
"Underlying element Locator [{}] is not configured as ShadowRoot host.",
this.underlying.getLocator().getVariableName());
}
return (T)
PageObjectBuilder.withContext(context)
.forBaseComponent(this.getClass())
.initialize(this.underlying);
}
@Override
public LazyWebElement getParent() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get parent for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying.getParent();
}
@Override
public LazyWebElement getLazyWebElement() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get underlying element for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying;
}
@Override
public WebElement getUnderlyingWebElement() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get underlying element for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying.getUnderlyingWebElement();
}
@Override
public Duration getWaitPollingInterval() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get polling interval for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying.getWaitPollingInterval();
}
@Override
public Duration getWaitTimeout() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get wait timeout for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
return this.underlying.getWaitTimeout();
}
@Override
public void setWait(final Duration timeout, final Duration pollingInterval) {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot set wait for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
this.underlying.setWait(timeout, pollingInterval);
}
/**
* Returns the value of the supplied attribute from the underlying WebElement.
*
* @param attribute the attribute to get
* @return the value of that attribute in the WebElement
*/
@Override
public String getAttribute(final String attribute) {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot get attribute for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
try {
return this.underlying.getAttribute(attribute);
} catch (NoSuchElementException | TimeoutException | StaleElementReferenceException e) {
logger.error("{} could not be initialized!", this.getClass().getSimpleName());
throw e;
}
}
@Override
public boolean exists() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot check existence of component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
boolean exists = this.underlying.exists();
logger.debug("{} {}.", this.getClass().getSimpleName(), exists ? "exists" : "does not exist");
return exists;
}
@Override
public boolean isDisplayed() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot check displayed status for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
boolean isDisplayed = false;
try {
isDisplayed = this.underlying.isDisplayed();
} catch (NoSuchElementException | TimeoutException | StaleElementReferenceException ignored) {
}
logger.debug(
"{} {} displayed.", this.getClass().getSimpleName(), isDisplayed ? "is" : "is not");
return isDisplayed;
}
@Override
public boolean isClickable() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot check clickable state for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
boolean isClickable = false;
try {
isClickable = this.underlying.isDisplayed() && this.underlying.isEnabled();
} catch (NoSuchElementException | TimeoutException | StaleElementReferenceException ignored) {
}
logger.debug(
"{} {} clickable.", this.getClass().getSimpleName(), isClickable ? "is" : "is not");
return isClickable;
}
@Override
public boolean isEnabled() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot check enabled status for component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
boolean isEnabled = false;
try {
isEnabled = this.underlying.isEnabled();
} catch (NoSuchElementException | TimeoutException | StaleElementReferenceException ignored) {
}
logger.debug("{} {} enabled.", this.getClass().getSimpleName(), isEnabled ? "is" : "is not");
return isEnabled;
}
@Override
public void scrollToElement() {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot scroll to component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
this.underlying.scrollToElement();
}
@Override
public void swipeToElement(final SwipeDirection direction, final int attempts) {
if (this.underlying == null) {
throw new UnsupportedOperationException(
"Cannot swipe to component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
this.underlying.swipeToElement(direction, attempts);
}
}