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

Difference Interfaces between Abstract Classes [duplicate]
Mockito's ArgumentCaptor: One method name, two method definitions (Part 2)
Difference between String.getBytes() and IOUtils.toByteArray()?
Producer / Consumer using wait and notify
Generics in return types of static methods and inheritance
Java queue linkedlist [closed]
java library to run shell commands on multiple remote machines simultaneously
how to view .class files in a rt.jar file in jdk1.6 software
jqgrid check primary key
Java Methods to rotate, increase size, and clear Box image in Java
Speech to text with time stamps using google API
AppEngine java.util.Date with Timezone in Struts2
Java Swing Change Context of JTextField in several JPanels In 1 JFrame
ADF in flatbed scanner
PrimeFaces fileUpload testing with Selenium WebDriver
Spring/JPA/Hibernate persist entity : Nothing happen

Categories

HOME
amazon-ec2
json.net
session
react-router
kalman-filter
qore
node-notifier
contact
facebook-php-sdk
here-api
offline
etl
google-apps-marketplace
commonmark
iron-router
serverless-framework
dosgi
clojurescript
opentracing
after-effects
aurigma
nhibernate-envers
opencover
dcevm
php-openssl
one-to-many
cas
social-media
maquette
typo3-6.2.x
log4js-node
windows-server-2000
nouislider
ecto
twilio-api
ioio
perlin-noise
textmate
wallpaper
normal-distribution
android-browser
mapzen
sfdc
avro4s
sencha-touch-2.3
websphere-mq-fte
fault
network-flow
chain-builder
pango
jquery-nestable
sage-one
tropo
cudafy.net
imanage
iso8601
minimization
color-picker
database-optimization
storekit
dataview
ford-fulkerson
createprocessasuser
processmodel
generic-programming
mmc
census
python-green
ng-animate
xc16
cctv
aapt
reactfx
rtmfp
picturefill
typo3-neos
ivyde
limejs
eventlistener
ocunit
referrer
errai
viewswitcher
ticoredatasync
dotnethighcharts
asyncfileupload
boost-filesystem
genshi
infobox
datareader
gallio
infrastructure
icanhaz.js
xsdobjectgen
preference
forums
vc90
virtual-functions
boost-smart-ptr
mdac
multiple-languages

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
Mobile App
Mobile App
Mobile App