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

spark sql join performance issue with mongo-spark and spark-redshift connectors
Why doesn't my thread wait for CompletableFutures to complete with `allOf()`?
spring MVC request param auto mapping complex object
Hibernate Tools required jars
Java - NumberFormatException when using .parseInt(String)
HttpServletResponse Writer sends message length on flush
Socket Connections to APNS (binary provider API) are timing out after 2 min if they are idle
Kotlin reflection + generics
value <!DOCTYPE of type java.lang.string can not be converted to jsonobject
Push notification from java/spring server to iOS, Android and web browser clients
Why we use superclass type object that reference to subclass object [duplicate]
Spring boot websocket with custom token based authentication: send message to specific user
How to iterate simultaneously over two lists using thymeleaf
How could I pass enum object with nested enum in it to a method as parameter?
Can ThreadLocal be used as Key in HashMap? [closed]
Questions regarding programming a single-line calculator in Java

Categories

HOME
maven
log4j
image
minimum-spanning-tree
deezer
json.net
iterator
spagobi
survey
ip
sql-server-2016
alpha
indesign
gitpitch
gnupg
metatrader4
kentor-authservices
collectd
windows-server-2012
carthage
reactive-cocoa
kryo
paging
crystal-reports-2010
emgucv
h2db
sparse-matrix
kannel
entitlements
jaxb2
google-cloud-nl
html5-fullscreen
simplexml
dartium
automake
starteam
tasker
sas-visual-analytics
email-templates
fedex
isbn
unixodbc
wso2carbon
osx-mavericks
youcompleteme
atomicity
python-webbrowser
alphabet
dropbox-php
carrot
chord-diagram
synchronous
ableton-live
firmata
static-ip-address
android-fonts
probability-density
fody
nsviewcontroller
testng-dataprovider
ipconfig
asp.net5
xpath-1.0
execute
icu4j
wireshark-dissector
tarjans-algorithm
browser-bugs
interrupted-exception
generic-programming
website-monitoring
codeigniter-routing
device-manager
googlemock
castle
tld
uitouch
htmlcleaner
comexception
html5-notifications
odata4j
easy-install
mylyn
chronoforms
pvrtc
sublist
mt
qt-jambi
xmemcached
recent-documents
suppress
getresponsestream
firefox4
microsoft-virtualization
ctp4
swing-app-framework
defensive-programming

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