I'm really struggling trying to figure out how to stop PHPUnit+Selenium from breaking once my tests try to move from one page to another. For example, I do something like this:
public function myTest()
{
$this->clickOnElement('somelink');
$this->assertEquals('content', $this->byId('newElement'));
}
When I try to run this, I get an error like this:
1) myTestClass::myTest
PHPUnit_Extensions_Selenium2TestCase_WebDriverException: Element not found in the cache
- perhaps the page has changed since it was looked up
Command duration or timeout: 107 milliseconds
The problem only appears when moving between two pages, and the element you are looking for exists on both pages (like a heading or title.) There are actually two problems here, both of which are race conditions:
clickOnElement()
starts the next page loading, but before that happensbyId()
runs on the current page and finds the element. Then the browser finishes loading the new page, andassertContains()
tries to get the value out of the element. But the new page has now loaded, and the element reference we have is from the previous page. Because that element no longer exists in the current DOM, you get the error about the stale element.clickOnElement()
starts the next page loading, but before it has finished loadingbyId()
runs on the new, but not fully loaded page. Because the page hasn't finished loading, you get an 'element not found' error.
Any ideas how to fix these two problems?