Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StaleElementReference Error Element not found in the cache

I'm using Capybara 2.1 with Ruby 1.9.3 using the selenium driver (with Minitest and Test Unit) in order to test a web app.

I am struggling with the StaleElementReferenceException problem. I have seen quite a number of discussions on the topic but I haven't been able to find a solution to the issue that I am facing.

So basically, I'm trying to find all pagination elements on my page using this code:

pagination_elements = page.all('.pagination a')

Then I'm doing some assertions on those elements like:

pagination_elements.first.must_have_content('1')

After those assertions, I'm continuing the test by clicking on the Next Page link to make sure that my future first pagination element will be the Previous Page. To do that I'm retrieving paginations elements again :

new_pagination_elements = page.all('.pagination a')

And the Stale Error is occurring here, because I'm reaching elements that I've already reached. ( Here is the error )

You can see the link states here.

I really have no idea how to make this common test work properly. Do you have any tips for a better way to reach my pagination elements?

like image 249
Evers Avatar asked Aug 27 '13 08:08

Evers


4 Answers

I sometimes have some problem with AJAX intensive pages, in my case this workaround solves it:

begin
  ...
rescue Selenium::WebDriver::Error::StaleElementReferenceError
  sleep 1
  retry
end
like image 103
ejosafat Avatar answered Sep 19 '22 15:09

ejosafat


I saw the main message in the gist is:

Element not found in the cache - 
perhaps the page has changed since it was looked up

I have similar case before. There are two solutions:

  1. Add page.reload before checking same stuff in new page, if you have set Capybara.automatic_reload = false in spec_helper

  2. find a special element in new page which previous page doesn't have. This effect is equivalent to wait.

Another method is to use specific selector. For example, instead of

pagination_elements = page.all('.pagination a')

Use

pagination_elements = page.all('#post_123 .pagination a')

Append a unique id area to the selector and you should not meet such problem.

like image 26
Billy Chan Avatar answered Sep 18 '22 15:09

Billy Chan


Interesting link about this error and how to fix it : http://stefan.haflidason.com/testing-with-rails-and-capybara-methods-that-wait-method-that-wont/

like image 34
Ben Colon Avatar answered Sep 19 '22 15:09

Ben Colon


Apparently, in addition to race conditions, this error also appears due to misused within blocks. For example:

within '.edit_form' do
  click '.edit_button'
  # The error will appear here if the 'edit_button' is not a
  # descendant of the 'edit_form'
end
like image 39
art-solopov Avatar answered Sep 19 '22 15:09

art-solopov