LoginSignup
23
17

More than 5 years have passed since last update.

capybaraでcurrent_pathが一致しているかどうか調べるのにハマった

Posted at

前回の続き
別ウィンドウを開いて、そのURLが合っているかどうかのテストをやろうとした

sample_steps.rb
  step 'テスト画面が別Windowで表示されていること' do
    handle = page.driver.browser.window_handles.last
    page.driver.browser.within_window(handle) do
      current_path.to eq(hoge_path(type: :fuga))
    end  
  end

結果、一致してくれなかった・・・

       expected: "/hoge?type=fuga"
            got: "/hoge"

current_pathではgetパラメーターがくっついてないらしい。
ってことでpathをやめてurlに変更。

sample_steps.rb
  step 'テスト画面が別Windowで表示されていること' do
    handle = page.driver.browser.window_handles.last
    page.driver.browser.within_window(handle) do
      current_url.to eq(hoge_url(type: :fuga))
    end  
  end

結果、惜しいw

       expected: "http://www.example.com/hoge?type=automail"
            got: "http://127.0.0.1:38333/hoge?type=automail"

最終的にうまくいったのは、こんなん↓

sample_steps.rb
  step 'テスト画面が別Windowで表示されていること' do
    handle = page.driver.browser.window_handles.last
    page.driver.browser.within_window(handle) do
      uri = URI.parse(current_url)
      expect("#{uri.path}?#{uri.query}").to eq(hoge_path(type: :fuga))
    end  
  end

参考

How to get current path with query string using Capybara

23
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
17