LoginSignup
3
4

More than 3 years have passed since last update.

Rspec + Capybara で順序をテストする

Last updated at Posted at 2018-02-03

featureテストにおいて、要素の出現順をテストしたい。

Stringindexメソッドを利用することで、そのテストが可能。

scenario 'type order' do
  visit examples_path

  expect(page.body.index(precede.target.str)).to be < page.body.index(non_preced.word.str)
end

解説

現在表示しているページのbodyを文字列として取得する。
その中から出現順序をテストしたい要素の位置をindexメソッドで算出する。
算出した出現位置を単純に比較することで、要素の出現順序をテストしている。

テスト対象と同じ文字列が複数あると正常に動作しないためその点のみ注意。

参考

index (String) - Rubyリファレンス
http://www.rubydoc.info/github/jnicklas/capybara/Capybara/DSL#page-instance_method

3
4
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
3
4