2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SeleniumでCapybaraってみるテスト(という名の備忘録)

Posted at

Selenium Grid環境が出来たので、Webテストをしてみる。

用意するもの

ruby
Capybara
RSpec
テストサイト

テストコード

ちなみにテストサイトもdockerで作ればいいw
ログイン画面を作って、ログイン出来たら「Hello World」と画面に出るサイトがあるとして、以下のコードを実行してみる。

※先週ぐらいからruby始めたので、書き方おかしいかもしれないw

hoge.rb
require 'capybara/rspec'
require 'selenium-webdriver'

hub = 'http://192.168.99.100:32768/wd/hub'
id = 'hoge'
password = 'hogehoge'
site = 'http://192.168.99.100:32780'

Capybara.register_driver :remote_browser do |app|
  Capybara::Selenium::Driver.new(
    app,
    :browser => :remote,
    :url => hub,
    :desired_capabilities => :firefox
  )
end
Capybara.default_driver = :remote_browser
Capybara.javascript_driver = :remote_browser
Capybara.app_host = site

feature '[まずやってみよう]' do
    scenario '#1 ログインできるかな?' do
      visit '/'
      fill_in 'ユーザーID', :with => id
      fill_in 'パスワード', :with => password
      click_button 'ログイン'
      expect(page).to have_content('Hello World')
    end
end

実行

こんな感じで動くはず。

$ rspec hoge.rb
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?