Webdriver StaleElementReferenceException
January 26, 2012
1 - When we didn’t wait (or for the wrong thing)
public void clickMakeSomeAttendeesOptional() {makeSomeAttendeesOptionalLink.click();}public void makeOptional(String name) {driver.findElement(String.format(REQUIRED_ATTENDEE_ICON, name)).click(); // StaleElementReferenceException here}
public void clickMakeSomeAttendeesOptional() {makeSomeAttendeesOptionalLink.click();waitUntilVisible(optionalAttendeesLegend);}
2 - Waiting fails
WebDriverWait wait = new WebDriverWait(driver, timeout);wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(String.format(ATTENDEE, names.get(0)))); // StaleElementReferenceException here
// In concrete Page objectpublic void waitUntilMonthViewIsDisplayed() {waitUntilVisible(By.id("mvEventContainer"));}// In abstract parentprotected RenderedWebElement waitUntilVisible(By locator) {WebDriverWait wait = new WebDriverWait(driver, 20);return (RenderedWebElement) wait.until(refreshed(ExpectedConditions.visibilityOfElementLocated(locator)));}private <T> ExpectedCondition<T> refreshed(final Function<WebDriver, T> originalFunction) {return new ExpectedCondition<T>() {@Overridepublic T apply(WebDriver webdriver) {try {return originalFunction.apply(webdriver);} catch (StaleElementReferenceException sere) {throw new NoSuchElementException("Element stale.", sere);}}};}
3 - @FindBy injected elements
@FindBy(id = "tgCol0")private RenderedWebElement gridFirstColumn;
public EventBubble clickGridFirstColumn() {gridFirstColumn.click(); // StaleElementReferenceException herereturn newEventBubble();}
@Overridepublic void click() {try {underlyingElement.click();} catch (StaleElementReferenceException sere) {againLocate();click();}}
protected void againLocate() {underlyingElement = locator.locate();}
4 - findElement on WebElement
WebElement element = driver.findElement(...);WebElement e2 = element.findElement(...);e2.click(); // StaleElementReferenceException here
class OurWebElement implements WebElement {public static WebElement wrap(WebElement element, Locator locator) { return new OurWebElement(element, locator); }protected OurWebElement(WebElement underlyingElement, Locator locator) {this.underlyingElement = underlyingElement;this.locator = locator;}
@Overridepublic WebElement findElement(By by) {try {return wrap(underlyingElement.findElement(by),new FindElementLocator(this, by));} catch (StaleElementReferenceException sere) {againLocate();return findElement(by);}@Override public void click() { try { delay(); underlyingElement.click(); } catch (StaleElementReferenceException sere) { againLocate(); click(); } }}
protected void againLocate() {underlyingElement = locator.locate();}
5 - Bad luck
driver.findElement(By.xpath("//div[text()='" + text + "']")).click(); // StaleElementReferenceException here
// In Concrete PageObject classpublic void toggleCalendar(String name) {findElement( "//div[@class='t23']//div[text()='" + name + "']").click();}// In Abstract parent classprotected WebElement findElement(By by) {return OurWebElement.wrap(driver.findElement(by), new OurWebElement.FindElementLocator(driver, by));}
Conclusion
Posted by Jerome Mueller, 28 Comments
Book review: The Mythical Man-Month (by Frederick P. Brooks)
January 8, 2011
The book is 35 years old and it shows in many places. What is fascinating in the book is not so much the solutions that they came up with at that time (obviously some technology hasn’t been around at that time), but that many of the problems they faced are still problems today.
For example in the chapter “Why did the tower of Babel fail?” he writes:
“So it is today. Schedule disaster, functional misfits, and system bugs all arise because the left hand doesn’t know what the right hand is doing. As work proceeds, the several teams slowly change the functions, sizes, and speed of their own programs, and they explicitly or implicitly change their assumptions about the inputs available and the uses to be made of outputs.”
His suggestion to using the telephone often, regular project meetings and a (physical) workbook (that needs to updated by each engineer) are outdated of course. However it is interesting to see, that he pointed his finger on a big problem that is still valid today. We’re still looking to solve that. In the meantime, a lot of things have addressed these concerns: Javadoc, Wikis, Design by contract, Unit tests etc.
So if you decide to read this book and expect to get solutions out of it, you will probably be disappointed. But you will be amazed, that many of the big problems from back then are actually still big problems today. Even more, they seem to be problems that still catch projects by surprise as they rediscover them. In that regard, the time spent reading this small book is time very well spent!
Posted by Jerome Mueller, 0 Comments
Swiss Softball National Team Training
January 18, 2010
I used these kinds of pictures for the Trainingsday of the Swiss National Softball Team to explain some common mistakes. All the pictures here [pdf], in case somebody wants to have a closer look at them later on.
Posted by Jerome Mueller, 0 Comments
Some Christmas Cards
December 24, 2009
Merry Christmas to everybody.
Posted by Jerome Mueller, 0 Comments
Sweet child of mine - violin
October 5, 2009
Sometimes, one word is enough: Wow!!!
Posted by Jerome Mueller, 2 Comments
Meteor come - BOOM
September 9, 2009
No more defects. No more test phase. No more testers.