Skip to content

Instantly share code, notes, and snippets.

@unakatsuo
Last active December 25, 2015 13:59
Show Gist options
  • Save unakatsuo/6987966 to your computer and use it in GitHub Desktop.
Save unakatsuo/6987966 to your computer and use it in GitHub Desktop.
Capybara + rspec + PhantomJS (Poltergeist) 連携テスト

Capybara + rspec + PhantomJS (Poltergeist) 連携テスト

headlessで、http://www.yahoo.co.jp/ の検索Boxをテストするだけの内容。 これだけの内容でも、検索テキストボックスDOMの描画待ちをしないといけない。

Install & Run:

% bundle install --path=vendor
% bundle exec rspec a_rspec.rb
# -*- coding: utf-8 -*-
require 'rubygems'
require 'rspec'
require 'capybara/rspec'
require 'capybara/poltergeist'
#require 'capybara/mechanize'
#Capybara.current_driver = :mechanize
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
opts = {
:phantomjs=>File.join((ENV['PHANTOM_JS_DIR'] || PHANTOM_JS_DIR), 'bin/phantomjs')
}
Capybara::Poltergeist::Driver.new(app, opts)
end
Capybara.configure do |c|
c.app = proc{}
c.app_host = "http://www.yahoo.co.jp"
c.run_server = false
end
feature 'Top page' do
scenario 'search works' do
visit '/'
# DOMが存在するか確認。
# https://github.com/jnicklas/capybara#asynchronous-javascript-ajax-and-friends
has_no_xpath?("//form[@name='sf1']")
within(:xpath, "//form[@name='sf1']") do
fill_in 'p', with:'ふなっしー'
click_button('検索') # 検索 ボタン
end
# save_screenshot("/tmp/a.png")
end
end
source 'https://rubygems.org/'
gem 'capybara'
#gem 'capybara-mechanize'
gem 'poltergeist'
gem 'rspec'
PHANTOM_JS='phantomjs-1.9.2-linux-x86_64'
PHANTOM_JS_DIR=unless ENV['PHANTOM_JS_DIR']
ENV['PHANTOM_JS_DIR'] = File.join(Bundler.root, Bundler.settings[:path], PHANTOM_JS)
else
ENV['PHANTOM_JS_DIR']
end
# setup phantomjs pre-compiled binary when "bundle install".
if File.basename($0) == 'bundle' && ARGV.member?('install') && !File.exists?(PHANTOM_JS_DIR)
Bundler.ui.info "Trying to install pre-compiled PhantomJS binary '#{PHANTOM_JS_DIR}'..."
if Bundler.settings[:path].nil?
abort "Please set --path option for 'bundle install'. The PhantomJS binary is installed under the foot."
end
Dir.chdir(File.dirname(PHANTOM_JS_DIR)) do
system("curl -O http://phantomjs.googlecode.com/files/#{PHANTOM_JS}.tar.bz2")
system("tar jxf #{PHANTOM_JS}.tar.bz2")
end
unless File.exists?(PHANTOM_JS_DIR)
abort "Failed to copy PhantomJS binary to #{PHANTOM_JS_DIR}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment