Reputation: 15
I have this class named ReusableFunctions that I use to implement methods that are commonly used like checking for the presence of elements or invisibility of elements. I have declared all these methods as static and invoke them using the class name in my test class. I have methods that wait for a default period of time (20 seconds in my case) and also methods that take in a time value as its parameter and wait for the specified time period. The problem is that even though I pass parameters into my wait methods WebDriver discards the parameter value and just waits for the default time of 20 seconds.I have logs printed in my console and I see that even though I have passed a time parameter value of 10 seconds web driver discards that and waits for a time period of 20 seconds. Please refer to the code below to understand this better.
//The reusable class where i implement my wait methods
public class ReusableFunctions extends InitializeBrowser{
static Logger log = Logger.getLogger(ReusableFunctions.class);
//A general wait method that waits for 20 seconds before throwing an
exception
public static void waitTillElementDisappears(WebElement we){
WebDriverWait wait=new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.invisibilityOf(we));
}
//A wait method that takes in a parameter value and waits for the
specified time limit
public static boolean
waitTillElementDisappearsWithCustomizedWaitParameter(WebElement we, long
timeValue){
WebDriverWait waitTillInvisibility=new WebDriverWait(driver,
timeValue);
waitTillInvisibility.until(ExpectedConditions.invisibilityOf(we));
if(!we.isDisplayed()){
return true;
}else{
return false;
}
}
}
//This is how I write validation methods using the above wait
implementations
public void clickEditInCommunityList(){
log.info("Clicking edit button in the community list section");
ExtentTestManager.getTest().log(Status.INFO,"Clicking edit button in
the community list section");
try{
if(errorMessageBox.isDisplayed()){
ReusableFunctions.waitTillElementDisappearsWithCustomizedWaitParameter
(errorMessageBox,10);
}
}catch(Exception e){
log.info("The exception is++++"+e);
log.info("Error message box not displayed");
}
try{
if(addBackgroundErrorMessage.isDisplayed()){
ReusableFunctions.waitTillElementDisappears(addBackgroundErrorMessage);
}
}catch(Exception e){
log.info("The exception is++++"+e);
log.info("Error messages background not displayed");
}
communityEditButton.click();
log.info("Edit button clicked successfully");
ExtentTestManager.getTest().log(Status.INFO,"Edit button clicked
successfully");
}
As you can see from the above code in one try, catch block I have used the default wait function which waits for a period of 20 seconds and in the other try, catch block I have used the other wait function which takes in a time value of 10 seconds as its parameter. But when I run these tests what I see from my logs is that in both the try, catch blocks web driver waits for 20 seconds.
Logs below
05-07-2019 **15:39:38** INFO [SNMPPage]: Clicking edit button in the
community list section
05-07-2019 **15:39:59** INFO [SNMPPage]: The exception
is++++org.openqa.selenium.TimeoutException: Expected condition failed:
waiting for invisibility of Proxy element for: DefaultElementLocator
'By.xpath: //ul[@id='noty_center_layout_container']' (tried for 10
second(s) with 500 milliseconds interval)
The above log is what I get when the wait statement with the time parameter value is executed. You can see that it says 10 seconds but the time stamp value shows 20 seconds.
Any help regarding this would be greatly appreciated.
Upvotes: 1
Views: 163
Reputation: 3635
The problem is mixing Implicit
and Explicit
wait.
When you set ImplicitWait like this:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Then you slow down driver actions like findElement
which causes an additional 10 seconds delay. The outcome of mixing two different kind of waits is unpredictable.
Removing implicitWait
will fix your case
EDIT:
From Selenium docs:
WARNING: Do not mix implicit and explicit waits! Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.
Upvotes: 3
Reputation: 7466
It looks like you run:
ReusableFunctions.waitTillElementDisappearsWithCustomizedWaitParameter(errorMessageBox,10);
and then
ReusableFunctions.waitTillElementDisappears(addBackgroundErrorMessage);
in sequence.
Both of these are Logging log.info("The exception is++++"+e);
in the catch block. What's the following log line, is it Error messages background has disappeared
? If so it looks like your code is working as expected.
You may want to tweak the lines that log the exception so that they better identify which line of code is actually logging.
Upvotes: 0