【问题标题】:test page redirection with Capybara + RSpec使用 Capybara + RSpec 进行测试页面重定向
【发布时间】:2015-11-04 22:44:30
【问题描述】:

我有一个 step_definition

Then(/^I should be redirected to the (.+?) page/) do |target|
  expect(current_url).to eq(Urls[target])
end

而且通常效果很好。有时,当我使用 poltergeist 驱动程序时,它比正常运行速度更快,并且 current_url 仍然是旧页面。那是我收到这样的错误的时候:

Then I should be redirected to the login page                                                # features/step_definitions/navigate-steps.rb:64

expected: "http://example.com/"
got: "http://example.com/reset-password"

(compared using ==)
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/navigation.rb:50:in `/^I should be redirected to the (.+?) page$/'
features/password.feature:100:in `Then I should be redirected to the login page'

有没有办法让匹配器等待 url 更新?

【问题讨论】:

    标签: rspec capybara


    【解决方案1】:

    不要将eq 匹配器与current_pathcurrent_url 一起使用。相反,请使用 Capybara 2.5+ 提供的 have_current_path 匹配器

    expect(page).to have_current_path(Urls[target], url: true)
    

    have_current_path 匹配器使用 Capybara 的等待/重试行为,因此它将等待页面更改。我添加了url: true 选项来比较完整的网址。如果Urls[target] 仅解析为路径,您可以删除该选项。

    【讨论】:

    • 谢谢!我知道肯定有一些东西使用了等待行为。
    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    相关资源
    最近更新 更多