【问题标题】:Why do I need to add sleep for rspec to pass with selenium 2.48.0?为什么我需要为 rspec 添加睡眠才能通过 selenium 2.48.0?
【发布时间】:2026-01-25 01:15:02
【问题描述】:

最近我们将 Selenium Web 驱动程序从 2.47.1 升级到了 2.48.0。

通过这次升级,我需要在 rspec 中添加 sleep 几秒钟才能通过。在没有sleep 的情况下,旧版本的规范可以正常工作。

sleep(inspection_time=5) // why do I need this?
my_form_page.save_button.click
// some assertion here

编辑

我尝试使用隐式等待而不是睡眠。但它不起作用。这背后有什么具体原因吗?

Capybara.current_session.driver.browser.manage.timeouts.implicit_wait = 50

【问题讨论】:

    标签: ruby selenium rspec


    【解决方案1】:

    一般来说,rspec selenium 测试被称为“flakey”。有时 rspec 由于多种原因(即:元素出现在 ajax 响应时)尝试在元素出现在页面上之前对其进行搜索。

    【讨论】:

      【解决方案2】:

      这里有一个提示可以帮助您解决这个问题,如果您将 capybara finders 包裹在 within block 中,您的测试将等到它在选择器 FIRST 中找到它,然后再尝试在其中运行代码它。

      这通常有助于解决在需要一段时间才能加载的页面上运行太快的测试以及您的按钮或选择器或页面上实际尚未出现的任何内容(这就是它失败的原因) .

      所以看看这 2 个例子并尝试使用 within 方法...

      # spec/features/home_page_spec.rb
      require "spec_helper"
      
      describe "the home page", type: :feature do
        context "form" do
      
          # THIS MIGHT FAIL!!!!
          it "submits the form", js: true, driver: :selenium do
            visit "/"
            find("#submit_button").click
          end
      
          # THIS PROBABLY WILL PASS!!! 
          it "submits the form", js: true, driver: :selenium do
            visit "/"
            within "form" do
              find("#submit_button").click
            end
          end
        end
      end
      

      【讨论】:

        最近更新 更多