LoginSignup
11
6

More than 5 years have passed since last update.

Capybara で n 番目の要素をテスト

Posted at

はじめに

Capybara でフィーチャスペックを書いているのですが、
table タグを使っているページで「どこどこの td にはなにがしが表示されている」なんてのをテストしたくなり、ページの中の特定の td を指定してテストする機会があったので備忘録にするほどでもないのですが書いておきます。

実装

◯◯.html.haml
%table
  %tr
    %th
    %td 最初の td
  %tr
    %th
    %td 2番めの td

めちゃくちゃ雑ですが、上記 html で最初の td に表示されているのが「最初の td」であることをテストする時には、

◯◯_spec.rb
tds = page.all('td')

expect(tds[0]).to have_content '最初の td'

これでテスト完了

まとめ

td は1ページで使う数も多い上に class とか id 振ってないことが多いので助かりました。

11
6
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
11
6