Skip to content

Instantly share code, notes, and snippets.

@bluerabbit
Last active April 22, 2022 01:01
Show Gist options
  • Select an option

  • Save bluerabbit/6227860 to your computer and use it in GitHub Desktop.

Select an option

Save bluerabbit/6227860 to your computer and use it in GitHub Desktop.
capybaraのAPIメモ

操作

  • ページに移動
visit '/pages/new'
  • フォームに値を入力
    • fill_in 'id_or_label_text', with: 'yamada'
  • ラジオボタンを選択
    • choose 'ラジオボタン1'
  • チェックボックスをチェック/非チェック
    • check('チェックボックス1')
    • uncheck('チェックボックス1')
  • ファイルの添付
    • attach_file 'file', "#{fixture_path}/xxx.csv"
  • セレクトボックスで選択
    • select('カテゴリ1', from: 'カテゴリ')
  • ボタンまたはリンクをクリック
    • click_on 'ボタンまたはリンク'

検証

  • 現在のページを確認
    • current_path.should == '/pages/1'
  • Http Statusを確認
    • page.status_code.should be(200)
  • ページ内に期待する文字列が(ある|ない)か確認
    • have_content
    • have_no_content
  • チェックボックスがチェックされて(いる|いない)
    • have_checked_field
    • have_unchecked_field
  • inputの値を検証する
    • should have_field('id_or_label', with: 'value')
  • テーブルの行数を検証する
    • all('#table_id tr').should have(0).rows
  • 表示・非表示の検証をする
    • find('#css_selecter').should be_visible
  • テーブルが存在するか
    • page.should have_table('table')
  • セレクターを使った検証
    • page.should have_selector('button', value: '実行')
    • page.should have_selector('h1#title', :content=>'Hello!')
  • ボタンが存在するか
    • find('#navigation').should have_button('Sign out')

debug

  • 現在のページをブラウザで表示
    • save_and_open_page
  • debuggerを起動
    • binding.pry

使ってはイケナイ(?!)

  • xpath引数を使ったfind
    • find(:xpath, '//li[contains(.//a[@href = "#"]/text(), "foo")]').value
  • findした要素の値をshouldではなく参照する
    • find('#id').text

Link

@bluerabbit
Copy link
Author

page.find('#hoge') # 存在しないとエラー
page.first('#hoge') # 存在しないとnil
page.all('table')     # 存在するelement全てを取得

@bluerabbit
Copy link
Author

within(".your_selector") do
  
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment