StaleElementReference错误元素在缓存中找不到

Eve*_*ers 6 ruby ruby-on-rails capybara

我正在使用带有Ruby 1.9.3的Capybara 2.1使用selenium驱动程序(使用Minitest和测试单元)来测试Web应用程序.

我正在努力解决这个StaleElementReferenceException问题.我已经看过很多关于这个主题的讨论,但我无法找到解决我所面临的问题的方法.

基本上,我试图使用以下代码在我的页面上找到所有分页元素:

pagination_elements = page.all('.pagination a')
Run Code Online (Sandbox Code Playgroud)

然后我在这些元素上做一些断言,比如:

pagination_elements.first.must_have_content('1')
Run Code Online (Sandbox Code Playgroud)

在这些断言之后,我通过单击Next Page链接继续测试,以确保我未来的第一个分页元素将是Previous Page.要做到这一点,我再次检索分页元素:

new_pagination_elements = page.all('.pagination a')
Run Code Online (Sandbox Code Playgroud)

而且Stale Error正在这里发生,因为我已经达到了我已经达到过的元素.(这是错误)

您可以在此处查看链接状态.

我真的不知道如何使这个常见的测试工作正常.你有更好的方法来达到我的分页元素吗?

Bil*_*han 1

我看到要点中的主要信息是:

Element not found in the cache - 
perhaps the page has changed since it was looked up
Run Code Online (Sandbox Code Playgroud)

我以前也有类似的案例。有两种解决方案:

  1. page.reload如果您已设置,Capybara.automatic_reload = false请在检查新页面中的相同内容之前添加spec_helper

  2. find新页面中前一页没有的特殊元素。这个效果相当于等待。

另一种方法是使用特定的选择器。例如,代替

pagination_elements = page.all('.pagination a')
Run Code Online (Sandbox Code Playgroud)

使用

pagination_elements = page.all('#post_123 .pagination a')
Run Code Online (Sandbox Code Playgroud)

向选择器附加一个唯一的 id 区域,您应该不会遇到此类问题。