背景
- 認証付きプロキシを超えないといけない
- capybara + selenium webdriverで、google.co.jpへアクセステストを実行したい
- ubuntu上に、プロキシサーバーを構築できた
ゴール
- 以下のテストが通る
describe "google" do
before do
visit '/'
end
context "トップページ" do
it { expect(page).to have_content('Google') }
end
end
経過
プロキシサーバーを構築
- 参照: http://qiita.com/alingogo/items/9c216bb7edc2d94d192e
- local_proxy環境変数を追加
- vi ~/.bashrc
export local_proxy=127.0.0.1:3128
- source ~/.bashrc
gemバージョン
group :development, :test do
gem 'capybara', '2.7.1'
gem 'selenium-webdriver', '2.53.0'
- selenium-webdriverのバージョンを最新にしよう
テストファイル構成
rails root
|---> spec
|--->その他の単体テスト
|--->features
|--->supprot/feature_helper.rb
|--->test_spec.rb
spec/features/support/feature_helper.rb
require 'capybara/rspec'
require 'selenium-webdriver'
ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
Capybara.register_driver :remote_firefox do |app|
local_proxy = ENV['local_proxy'] || ENV['LOCAL_PROXY']
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new( :http => local_proxy, :ssl => local_proxy)
Capybara::Selenium::Driver.new(app, :profile => profile)
end
Capybara.default_driver = :remote_firefox
Capybara.app_host = 'http://www.google.co.jp'
RSpec.configure do |config|
config.include Capybara::DSL
end
- 補足1
- ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
- http://stackoverflow.com/questions/11151222/unable-to-connect-to-browser-using-ruby-selenium-webdriver
spec/features/test_spec.rb
require File.expand_path(File.join(%w(support feature_helper)), File.dirname(__FILE__))
describe "google" do
before do
visit '/'
end
context "トップページ" do
it { expect(page).to have_content('Google') }
end
end
確認
$ bundle exec rspec spec/features/test_spec.rb
.
Finished in 6.31 seconds
1 example, 0 failures
参照
もう一度読めよ
- http://qiita.com/oh_rusty_nail/items/b8ba525d31ea7c522856
- http://qiita.com/awakia/items/2d9a70af86bc3488b241
- http://qiita.com/jnchito/items/607f956263c38a5fec24
問題解決
- http://stackoverflow.com/questions/15148585/undefined-method-visit-when-using-rspec-and-capybara-in-rails
- http://stackoverflow.com/questions/16278597/using-poltergeist-with-a-proxy
- http://stackoverflow.com/questions/2887978/webdriver-and-proxy-server-for-firefox
- http://qiita.com/sanryuu/items/5b1b4794f5fd3b26679c