java


A wrapper for WebDriverWait that could take all kinds of ExpectedConditon?


I am using Selenium 3.4 with Java. I am testing a UI that is not very fast and use an implicit wait so that I don't have to care about manual explcit waiting every time something can take a couple of seconds to load.
However, sometimes I need an explicit wait anyway. And for that, I need to disable the implicit wait temporarily. To avoid cluttering up the code, I put the following method in my driver manager class:
public static void waitFor(ExpectedCondition<Boolean> condition,int timeoutSeconds) throws Exception {
setImplicitWait(0);
try {
(new WebDriverWait(driver, timeoutSeconds)).until(condition);
} finally {
setImplicitWait(WAIT_REGULAR);
}
}
The setImplicitWait method is simple:
public static void setImplicitWait(int TimeoutSeconds) {
logger.debug("Setting implicit wait to {} seconds",TimeoutSeconds);
driver.manage().timeouts().implicitlyWait(TimeoutSeconds, TimeUnit.SECONDS);
}
So, this setup works for some waits, like this one:
DriverManager.waitFor(ExpectedConditions.invisibilityOfElementLocated(By.className("gridxLoad")), DriverManager.WAIT_HUGE);
However, if I try this one:
DriverManager.waitFor(ExpectedConditions.visibilityOfElementLocated(By.className("gridxMain")), DriverManager.WAIT_HUGE);
I get a compile error:
The method waitFor(ExpectedCondition, int) in the type DriverManager is not applicable for the arguments (ExpectedCondition, int)
If I change the parameter type in the definition of the method to ExpectedCondition<WebElement> , then, of course, the second wait suceeds and the first one throws a compile error. If I try creating the same method for both ExpectedCondition<WebElement> and ExpectedCondition<Boolean>, I get a compile error:
Erasure of method waitFor(ExpectedCondition, int) is the same as another method in type DriverManager
Finally, if I try to define the parameter as just ExpectedCondition, I get loads of compile warnings in the same small method:
ExpectedCondition is a raw type. References to generic type ExpectedCondition should be parameterized
Type safety: The expression of type ExpectedCondition needs unchecked conversion to conform to Function
Type safety: Unchecked invocation until(ExpectedCondition) of the generic method until(Function) of type FluentWait
I did also try defining the method like this:
public static void waitFor(Function<? super WebDriver,Object> condition,int timeoutSeconds) throws Exception {
But then both invocations of the method throw compiler errors.
What should I do? Go with a raw ExpectedCondition parameter and just ignore the warnings?
The methods visibilityOfElementLocated and invisibilityOfElementLocated don't return the same type:
public static ExpectedCondition<Boolean> invisibilityOfElementLocated(final By locator) { ... }
public static ExpectedCondition<WebElement> visibilityOfElementLocated(final By locator) { ... }
When the condition is applied, the first one returns a Boolean while the second one returns a WebElement.
So to make it work, you need to implement both:
public static void waitFor(ExpectedCondition<Boolean> condition,int timeoutSeconds) throws Exception {
setImplicitWait(0);
try {
(new WebDriverWait(driver, timeoutSeconds)).until(condition);
} finally {
setImplicitWait(WAIT_REGULAR);
}
}
public static WebElement waitFor(ExpectedCondition<WebElement> condition,int timeoutSeconds) throws Exception {
setImplicitWait(0);
try {
return (new WebDriverWait(driver, timeoutSeconds)).until(condition);
} finally {
setImplicitWait(WAIT_REGULAR);
}
}
I did eventually find what I think is the right solution:
public static void waitFor(ExpectedCondition<?> condition,int timeoutSeconds) throws Exception {
setImplicitWait(0);
try {
(new WebDriverWait(driver, timeoutSeconds)).until(condition);
} finally {
setImplicitWait(WAIT_REGULAR);
}
}

Related Links

In Java Windows Application using PdfBox Proguard zaps me
Consuming Spring Hateoas Restservice with RestTemplate
MASON Get Moore Neighbors
Java “no such element exception”
Trying to refer to a final ImageView does not work
Connect to tor with java - TorLib
Why won't this sorting work
First random number after setSeed in Java always similar
When override a method, say(), and then run the program I do not get what I anticipated to be printed
doClick(), and Simon: All buttons will unpress at the same time instead of individually
Java Algorithms: comparing each *thing* to every other [closed]
Whats causing this NullPointerException in AWT-EventQueue-0?
Gradle + Dropwizard + Shadow -> Could not find or load main class
Android - Try to send fake sms to myself without mobile network usage
Selenium, open new window
Using Android TCP Client app to interface with sensor

Categories

HOME
proxy
path-finding
homebrew
relayjs
cplex
v8
networkx
upload
contact
slurm
primary-key
adobe-analytics
spring-kafka
row
windows-azure-storage
visual-studio-2005
evopdf
oracle-coherence
alpine
autoconf
ehcache
facebook-instant-articles
kvc
mmap
angular2-aot
file-rename
gsmcomm
semantic-versioning
google-sites-2016
google-qpx-express-api
http-get
vsts-build-task
textmate
devextreme
android-browser
reportbuilder
document.write
eigenvalue
scorm
fault
appcompat
azure-application-gateway
sage-one
csound
dtexec
sqldf
minimization
xcb
color-picker
httplistener
probability-density
login-control
skobbler-maps
python-stackless
packagist
yaws
dataview
coveralls
browser-bugs
clipperlib
truevault
fscommand
rake-task
gadt
network-interface
neolane
typeof
htmlcleaner
transcoding
monomac
openlaszlo
ril
qt-faststart
dice
automount
newtonscript
parametric-equations
chrono
punbb
subscript
datareader
fluent-interface
swing-app-framework
thread-local-storage

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html