LoginSignup
3
4

More than 5 years have passed since last update.

認証付きプロキシを超えるようにcapybaraでブラウザ自動テストを実装

Last updated at Posted at 2016-05-13

背景

ゴール

  • 以下のテストが通る
describe "google" do
  before do
    visit '/'
  end

  context "トップページ" do
    it { expect(page).to have_content('Google') }
  end
end

経過

プロキシサーバーを構築

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'

テストファイル構成

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

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

参照

もう一度読めよ

問題解決

example

rails3

3
4
0

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
4