LoginSignup
0
0

CapybaraとSeleniumを使用してRuby on Railsアプリケーションの統合テストを行う方法

Posted at

お疲れ様です。

今日はCapybaraとSeleniumを使用してRuby on Railsアプリケーションの統合テストを行う方法を説明します。

Gemfileに追加

まず、Gemfileにcapybaraとselenium-webdriverを追加します。

group :test do
  gem 'capybara'
  gem 'selenium-webdriver'
end

インストール

次に、Gemをインストールします。

bundle install

テストのセットアップ

spec/rails_helper.rbファイルにCapybaraを設定します。

require 'capybara/rspec'
require 'selenium/webdriver'

Capybara.register_driver :selenium_chrome_headless do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu window-size=1920,1080]))
end

Capybara.javascript_driver = :selenium_chrome_headless

統合テストの作成

spec/featuresディレクトリに統合テストを作成します。例えば、ユーザーがログインするシナリオをテストする場合は次のようにします。

require 'rails_helper'

RSpec.feature 'User authentication', type: :feature do
  scenario 'User logs in successfully' do
    visit '/login'
    fill_in 'Email', with: 'user@example.com'
    fill_in 'Password', with: 'password'
    click_button 'Log in'

    expect(page).to have_text('Logged in successfully')
  end
end

テスト実行

テストを実行します。

bundle exec rspec spec/features/user_authentication_spec.rb

これで、CapybaraとSeleniumを使用してRuby on Railsアプリケーションの統合テストを行う準備が整いました。

今日は以上です。

ありがとうございました。
よろしくお願いいたします。

0
0
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
0
0