I am having trouble getting this “invisibilityOfElementLocated()” method to work. Here is the
code I am using and I am witnessing the element disappear after about 35 seconds with my own eyes but Webdriver does not detect this and crashes on this code and skips the test. Would love if someone had a diffferent method that worked better.
if ( driver.findElement( By.xpath( "//div[@class='Caption']" )).isDisplayed() ) {
System.out.println("Caption is visible.");
}
WebDriverWait wait1 = new WebDriverWait( driver , 60 );
wait1.until(
ExpectedConditions.invisibilityOfElementLocated(
By.xpath( "//div[@class='Caption']" )
)
);
Here is the error I causes:
org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for
element to no longer be visible: By.xpath: //div[@class='Caption']
Build info: version: '2.24.1', revision: '17205', time: '2012-06-19 16:53:24'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1',
java.version: '1.6.0_31'
Driver info: driver.version: unknown
at org.openqa.selenium.support.ui.FluentWait.timeoutException(
FluentWait.java:251)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:220)
at test.TaskListPage.isLoaded(TaskListPage.java:43)
at org.openqa.selenium.support.ui.SlowLoadableComponent.get(
SlowLoadableComponent.java:48)
at test.TaskListPage.<init>(TaskListPage.java:31)
at test.Form.testLogin(Form.java:153)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(
ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(
FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(
InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(
BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunAfters.evaluate(
RunAfters.java:30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
Also, this method gives a similar error:
ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
WebElement tE = driver.findElement( By.className("Caption") );
// return the invisibility instead of the visibility
return !tE.isDisplayed();
}
};
WebDriverWait w = new WebDriverWait(driver, 60);
w.until(e);
I had some success with this but it still throws an exception when the element finally becomes invisible and then bounces me out of my test case:
while (
// dialogMiddleCenterInner is a td that contains the Caption div
driver.findElement( By.className("dialogMiddleCenterInner")
).isDisplayed() ) {
System.out.println("Caption is visible. Waiting for login.");
AFormUtils.waitSeconds(2);
}
Thanks to Slanec for trying to help me. Ok, this was the answer. It seems that, whether or not I used ExpectedCondition or not, I just had to handle the exception instead of allowing it to throw, but it finally works: