【问题标题】:Capybara/Cucumber In Ruby: stop searching when no more elements existCapybara/Cucumber In Ruby:当不再存在元素时停止搜索
【发布时间】:2016-03-28 19:41:45
【问题描述】:

我正在测试的网站有一堆选定的过滤器 (picture for context)。我正在遍历所有这些并像这样关闭它们:

When /^each (.+) is clicked$/ do |element|
  page.all("#{element}").each do |element|
    element.click
  end
end

此代码单击关闭按钮并关闭所有打开的过滤器。我遇到的问题是,当我到达过滤器的末尾时,它会继续查找并引发此错误:

stale element reference: element is not attached to the page document
        (Session info: chrome=49.0.2623.87)
        (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Mac OS X 10.10.5 x86_64) (Selenium::WebDriver::Error::StaleElementReferenceError)
      ./features/step_definitions/ux_steps.rb:22:in `block (2 levels) in <top (required)>'
      ./features/step_definitions/ux_steps.rb:21:in `/^each (.+) is clicked$/'
      features/ux.feature:16:in `When each span is clicked'

HTML:

<div class="sort-block order" id="selectedFilters">
    <a id="Anti-Inflammatory_selectedfilter" href="javascript:void(0)" class="current">Anti-Inflammatory<span class="selectedFilterCross" onclick="selectedFilterFun('selectedFilterText1','Anti-Inflammatory',-1); $(this).parent('a').remove();">X</span></a>
    <a id="Cardiac_selectedfilter" href="javascript:void(0)" class="current">Cardiac<span class="selectedFilterCross" onclick="selectedFilterFun('selectedFilterText1','Cardiac',-1); $(this).parent('a').remove();">X</span></a>
    <a id="Diabetic_selectedfilter" href="javascript:void(0)" class="current">Diabetic<span class="selectedFilterCross" onclick="selectedFilterFun('selectedFilterText1','Diabetic',-1); $(this).parent('a').remove();">X</span></a>
</div>

关于如何在项目列表为空后停止搜索的任何想法?感谢所有帮助!

【问题讨论】:

    标签: ruby cucumber capybara acceptance-testing


    【解决方案1】:

    我认为这是因为当您开始循环时页面上的元素会通过单击从页面中删除。如果是这样,您应该能够通过重复搜索可点击元素、单击找到的第一个元素并再次搜索直到找不到更多内容来修复它:

    When /^each (.+) is clicked$/ do |selector|
      while true
        element = page.first(selector)
        if element
          element.click
        else
          break
        end
      end
    end
    

    注意"#{string}" 与string 相同。

    【讨论】:

    • 这消除了错误,但现在它陷入了无限循环。
    • 糟糕,错字。固定。
    • 它仍然陷入无限循环。它会关闭所有过滤器,但会继续寻找更多过滤器。为了清楚起见,我将 html 添加到我的问题中。
    • 发现问题:页面上有一堆span标签,所以我添加了within "##{id}" do使其只针对div中的标签。
    • 我很抱歉,但我无法投票,因为我还没有 15 声望。
    猜你喜欢
    • 1970-01-01
    • 2022-10-21
    • 2018-06-16
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多