【问题标题】:Wait till the form gets submitted in capybara等到表单在 capybara 中提交
【发布时间】:2016-01-06 12:33:10
【问题描述】:

我是 capybara 和 Ruby on Rails 世界的新手。我正在使用 capybara 为 Rails 应用程序编写黄瓜功能。

我在步骤定义文件中使用 sleep 语句来等待页面(或者更确切地说是表单)被提交。 但我观察到页面提交时间各不相同。那么我怎样才能告诉 capybara 等到下一页被加载或某些动作发生?

expect(page).to have_css(".alert.in.alert-success" , text: arg1,exact: true)

我尝试了上述方法等待,但没有成功。我发现在 capybara2.0 中不推荐使用 wait_until 那么谁能告诉我如何在 capybara 中等待页面被提交或直到某些操作发生?

【问题讨论】:

    标签: ruby-on-rails-4 rspec cucumber capybara


    【解决方案1】:

    水豚通常非常擅长等待元素出现。但是您遇到问题可以尝试增加默认等待时间,也可以手动等待。这是一篇可能有帮助的文章

    https://robots.thoughtbot.com/automatically-wait-for-ajax-with-capybara

    【讨论】:

      【解决方案2】:

      在您提供的示例中,exact:true 选项与传递的 :text 选项无关,而是与传递给 Capybara 提供的“选择器”的定位器(:button、:field 等)相关。由于您没有使用任何这些选择器,因此该选项实际上并没有做任何事情。要让 Capybara 等待下一页加载,您需要让它查找在下一页上可见但在当前页面上不可见的内容

      expect(page).to have_css('.alert.in.alert-success', text: 'This is only on the next page')
      

      将导致 Capybara 等待 Capybara.default_max_wait_time 以在页面上找到具有给定类的可见元素,其中包含给定文本。如果要匹配元素中的确切文本,可以传递正则表达式

      expect(page).to have_css('.alert.in.alert-success', text: /\AThis is only on the next page\Z/)
      

      如果页面加载速度不够快,您可以增加 Capybara.default_max_wait_time 的值,也可以使用 :wait 选项覆盖用于特定调用的值

      expect(page).to have_css('.alert.in.alert-success', text: 'This is only on the next page', wait: 10)  # wait up to 10 seconds for the element with given classes containing given text to become visible
      

      【讨论】:

        猜你喜欢
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多