例えばコメント投稿のテスト。

# test/integration/post_comment_to_comic_test.rb:
require 'test_helper'

class PostCommentToComicTest < ActionDispatch::IntegrationTest
  test 'Post a comment' do
    visit comic_path comics(:comic)
    within('#new_comment') do
      fill_in 'comment[body]', with: 'コメントのテスト'
    end
    click_button '規約に同意してコメントする'
    assert has_selector? '.content-comment__body-text',
      text: 'コメントのテスト'
  end
end

よくあるこういうのを実行したら下記のエラーが。

% ruby -Itest test/integration/post_comment_to_comic_test.rb -n PostCommentToComicTest#test_Post_a_comment
Run options: -n PostCommentToComicTest#test_Post_a_comment --seed 29141

# Running:

E

Finished in 5.497046s, 0.1819 runs/s, 0.0000 assertions/s.

  1) Error:
PostCommentToComicTest#test_Post_a_comment:
Capybara::Webkit::ClickFailed: Failed to click element /html/body/div[@id='content']/div[@id='comments']/div/div[1]/form[@id='new_comment']/div[@id='comment_action']/input because of overlapping element /html/body/div[@id='content']/nav/div/ul/li[2]/a/span at position 831, 1025; 
A screenshot of the page at the time of the failure has been written to /var/folders/nb/5jp6psyd7h19my68chb0svvh0000gn/T/click_failed_W60479.png
    test/integration/post_comment_to_comic_test.rb:23:in `block in '

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

クリックできなかったElementのxpathを表示してくれる。そして何故クリックできなかったのか、overlapしているElementのpositionとxpathも表示してくれる。更にscreenshotここに撮っておいたからってどこまで至れり尽くせりだよ。

この場合、要はこんな感じになっててコメントボタンがクリックできない。

こんな方法でいいのかどうかわかりませんが、テスト前にexecute_scriptでoverlapしてるメニューをhideして対処。

# test/integration/post_comment_to_comic_test.rb:
require 'test_helper'

class PostCommentToComicTest < ActionDispatch::IntegrationTest
  test 'Post a comment' do
    visit comic_path comics(:comic)
    execute_script("$('nav.other-pages-nav').hide()") # It's workaround.
    within('#new_comment') do
      fill_in 'comment[body]', with: 'コメントのテスト'
    end
    click_button '規約に同意してコメントする'
    assert has_selector? '.content-comment__body-text',
      text: 'コメントのテスト'
  end
end

更に背景と同色の文字問題とかもサポートしてくれたら手動QA不要とはいわないけどもっと自動テスト化できますね。

ヘッド有りブラウザーで動きみたいときもある。

$ brew install chromedriver
# test/test_helper.rb:
Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Capybara.javascript_driver = :chrome

teardownも同じくどっちでも変わらないと思ってた。しかし、前者は上書きだけど後者は親クラスのsetupも呼んでくれるので後者の方が良い。例えば下記みたいにintegrationテストだけは全部js実行できるブラウザでテストしたいけどいちいち書いてられない場合。

# test/test_helper.rb:
class ActionDispatch::IntegrationTest
  setup do
    Capybara.current_driver = Capybara.javascript_driver
  end
end
# test/integration/post_comment_test.rb:
class PostCommentTest < ActionDispatch::IntegrationTest
  setup do
    sign_in('foo@example.com', 'password')
  end

  test 'post a comment' do
    (...)
  end
end

superが要らないのは気持ちいい。

単純にテストの後処理としてのサインアウトだったらcookieを消す方が速い。

page.driver.browser.clear_cookies

サインアウト機能のテストでよくある。visitメソッドだとgetしか飛ばせない。

実際にサインアウトリンクをクリックするのはサインアウトのテストだったらいいけど、他のテストの後処理として使う場合はすごく遅くなりそうで嫌だ

deviseには下記のような設定ができるらしいけど、これテストしてることになんの?って気がするので却下。

config.sign_out_via = Rails.env.test? ? :get : :delete

これで行けた。

page.driver.submit :delete, '/users/sign_out', {}

ajaxで投稿してその結果を調べるみたいなテストでテストが通ったり通らなかったりする。投稿が反映される前に見に行くことがあるから。

feature "Posting a comment", js: true do
  scenario "as signed user" do
    comment_id = Comment.last.id + 1
    within("#new_comment") do
      fill_in 'comment[body]', with: 'コメントのテスト'
    end
    click_button '規約に同意してコメントする'
    sleep 1 # PLZ WAIT!! FIXME!!
    find("#comment_#{comment_id}").should have_content('コメントのテスト')
  end
end

CapybaraのREADMEにも書いてあるけどデフォルトは2秒待つようだけど5に変えたら行けた。(sleep 1は取りましたw)

# spec/spec_helper.rb:
RSpec.configure do |config| 
  Capybara.default_wait_time = 5
end
% rake
`include Capybara` is deprecated please use `include Capybara::DSL` instead.

include Capybaraがdeprecatedになってたので指示通り書き換えました。

# test/test_helper.rb:
class ActiveSupport::TestCase  
  (...)
  class ActionDispatch::IntegrationTest 
    include Capybara::DSL      
    Capybara.app = KowabanaJp::Application
  end
end

Unit::TestとCapybaraでのIntegrationTestは簡単なのにちゃんと動く(バグを見つけたりデグレを防ぐ役割をちゃんと果たすという意味で)のでお気に入りです。