LoginSignup
3

More than 5 years have passed since last update.

Capybara、phantomjsを使ったテスト

Last updated at Posted at 2015-07-23

JavascriptによるDOM操作に依存する総合テスト

Jsのテスト環境構築をメモ

通常の Rspec/Capybara のテストとは分離します。
理由:Js関連と分けることで、テストの実行にかかる時間を短縮
場所: jspec/features ディレクトリを作成

準備 (Mac)


$ cd ~
$ mkdir -p src
$ cd src
$ git clone git://github.com/ariya/phantomjs.git
$ cd phantomjs
$ git checkout 1.9
$ ./build.sh
$ sudo cp bin/phantomjs /usr/local/bin/
$ brew install phantomjs192

※ buildには20分程かかります。

Gemfile
Capybara から PhantomJS を使うには poltergeist を使います。


group :test do
  gem 'rspec-rails'
  gem 'capybara'
  gem 'poltergeist', git: 'git@github.com:teampoltergeist/poltergeist.git'
  gem 'launchy'
end

spec_helper.rb
1, inspector: true Poltergeistが提供しているデバッグ機能
https://github.com/teampoltergeist/poltergeist#remote-debugging-experimental
2, poltergeistを使う指定


ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/poltergeist'

Capybara.register_driver :poltergeist do |app|
  options = { inspector: true }
  Capybara::Poltergeist::Driver.new(app, options)
end

Capybara.javascript_driver = :poltergeist
Capybara.always_include_port = true

Dir[Rails.root.join("jspec/support/**/*.rb")].each { |f| require f }

テスト実行には下記のコマンドを使用します。


$ bin/rspec --default-path=jspec jspec/

これで、Jsを含んだ機能のテストができます。

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