Selenium assertFalse fails with staleelementreferenceexception
Asked Answered
V

2

2

I have a selenium test in Java and I am doing some assertions like that:

assertFalse(isElementPresent(By.xpath("//td[2]/div")));

private boolean isElementPresent(By by) {
try { driver.findElement(by); return true; }
catch (NoSuchElementException e) { 
return false; }

It´s the standard method Selenium is generating when export from IDE to Java Webdriver.

(Yes I want to assert that this element is not present)

I always get errors when I am testing at this above code line Error: stale element reference: element is not attached to the DOM

But when I put a thread.sleep in front of that step it works. The fact I don´t get is that it is enough to wait 1 milli sec. Is it typical to wait before an assertion? Is there another way to solve this? (Implicit wait is not helping here) Greetings from Germany!

Vector answered 12/3, 2018 at 9:18 Comment(2)
Share the code for isElementPresent()Urinary
Instead of adding as a comment update the actual question with this additional info for a proper analysis.Itch
I
1

As you are facing staleelementreferenceexception in assertFalse() function, to negate the FalsePossitive usecase you can induce WebDriverWait with ExpectedConditions clause set to stalenessOf within assertTrue() function as follows :

Assert.assertTrue(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));

Explaination

The ExpectedConditions clause stalenessOf will check for the staleness of the element identified as (By.xpath("//td[2]/div")). When the intended element becomes stale, you can check for assertTrue(boolean condition). assertTrue() would assert that a condition is true. If it isn't, an AssertionError would be raised.

assertFalse(condition)

If you still want to implement the FalsePossitive case of assertFalse(condition) raising Error you still can :

Assert.assertFalse(new WebDriverWait(driver, 20).until(ExpectedConditions.stalenessOf(driver.findElement(By.xpath("//td[2]/div")))));
Itch answered 12/3, 2018 at 10:41 Comment(3)
Hi DebanjanB thanks for your answer. One last question: I now get an compilation error in your codeline for the "By" right after the stalenessOf expression. "The method stalenessOf(WebElement) in the type ExpectedConditions is not applicable for the arguments (By)" So i need to change the isElementpresent methode?Vector
@ArnoDonCalzone There was a bug in my code which I have corrected and should work flawless. Let me know the status.Itch
Hi, thanks for your update. Works quite well. Thanks a lot!Vector
T
0

I think, timeouts are not set to WebDriver. try this

assertFalse(isElementPresent(By.xpath("//td[2]/div")));

private boolean isElementPresent(By by) {
driver.timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
try { driver.findElement(by); return true; }
catch (NoSuchElementException e) { 
return false; }
Thoth answered 12/3, 2018 at 11:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.