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

How to store latitude and longitude values [closed]
Esper rules for different users
how to intent with on click button from activity to fragment in android studio
GWT Compiler NoSuchMethodError
Cannot access JAR Manifest Specification page in Eclipse
AES Decryption Error [closed]
Jsoup code that works for Eclipse but not Android Studio (httpurlconnectionimpl)
How to find an element in new Webpage using selenium webdriver?
Every iteration of the game I want it to reduce the life time by 1
Returning string from Android Activity started with `startActivityForResult` inside `onClickListener` returns null
Java SQLException with JDBC - SQLITE_MISUSE
Java Polymorphism error [duplicate]
How to reset or refresh Jframe with new values
Getting Started with Lagom gives Runtime Exception
How do you sort out an array in java without using a sorting algorithm, while ignoring other values?
How to modify MIB_IFTABLE

Categories

HOME
image
clips
c#-4.0
xmpp
maven-3
paw-app
win32gui
onelogin
node-notifier
basic
user-input
etl
php-7.1
static-libraries
fortumo
decomposition
carthage
try-catch
lombok
uitypeeditor
opentracing
openrefine
normalizr
sms-gateway
contextmenu
google-pagespeed
nas
cx-freeze
microsoft-r
restful-authentication
hammerspoon
autosys
angular2-aot
xpages-ssjs
buildbot
fish
applozic
elasticsearch-ruby
assistant
jspm
gsoap
microsoft-chart-controls
tinymce-4
media-queries
google-cloud-endpoints-v2
pdb
winrt-xaml-toolkit
code-contracts
vsts-build-task
geopositioning
android-ble
host
io-redirection
git-merge
neuroscience
event-driven
c11
squib
ensembles
hp-ux
sfdc
fedex
gzipstream
nand2tetris
mime
unixodbc
babel-core
smartcontracts
revapi
zip4j
errordocument
jquery-validate
hls.js
mu
statsd
recursive-datastructures
google-perftools
cudafy.net
intrusion-detection
django-debug-toolbar
bstr
websitepanel
connect-by
packagist
whois
wapiti
associative-array
qtableview
msys
collapse
comobject
django-unittest
muse
method-parameters
generic-programming
graph-api-explorer
python-green
tween
clicktag
jscript.net
tt-news
googlemock
aapt
didselectrowatindexpath
flexmojos
pyhdf
dexterity
ora-00911
mqx
hamiltonian-cycle
funscript
bubble-chart
vdsp
coercion
django-tagging
ncqrs
virtual-functions
xetex
procedural-music
w3wp
windows-live-messenger
paul-graham





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