前提は以下のような感じです。
・テストはWindows上でInternetExplorerを利用して行う
・データベースはMS SQL server
・webサイトはASP.Netで書かれている(htmlを出力しているなら言語は問わないはずです)
・テスト用のデータベース、webサイトを専用に用意する
※今回database_cleanerを利用しているので既存データがあったらごっそり消えます。
windows環境でrubyが使えるようにする手順は省略します。
bundlerとrspecをgem installして利用できるところまでは準備してください。
まずInternetExplorerをseleniumで利用できるように準備します。
IE用のドライバを以下から取得します。
http://docs.seleniumhq.org/download/
ダウンロードしたzipファイル内の
IEDriverServer.exe
を
c:\ruby\bin
などのパスが通っているフォルダに配置してください。
そしてIEの設定を確認します。
インターネットオプション
セキュリティで
各ゾーンの
保護モードを有効にする
のチェック状態をすべてのゾーンで同じにするように設定しておく必要があります。
ここからテストプログラム用の環境を作っていきます。
ちなみに、ここから作成するファイルの文字コードはすべてUTF-8です。
まず適当なフォルダを作成して、その作成したフォルダ内で以下を実行します。
rspec --init
フォルダ内にGemfileを準備します。
Gemfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "selenium-webdriver" | |
gem "capybara" | |
gem "rspec" | |
gem "rack" | |
gem "database_cleaner" | |
gem "factory_girl" | |
gem "activerecord" | |
gem "activerecord-sqlserver-adapter" | |
gem "activesupport" | |
gem "tiny_tds" |
必要なgemをインストールするためフォルダ内で以下を実行します。
bundle install
rspec設定ファイル(.rspec)を作成します。
.rspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--color | |
--require spec_helper |
フォルダ内にconfigフォルダを作成して、その中にデータベース接続設定ファイルを作成します。
config/database.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
development: | |
adapter: sqlserver | |
dataserver: sample-srv | |
database: test-db | |
username: test | |
password: test_passwd |
フォルダ内にspecフォルダを作成して、その中にspec_helperを作成します。
spec/spec_helper.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'rubygems' | |
require 'capybara/rspec' | |
require 'selenium-webdriver' | |
require 'active_record' | |
require 'active_support/core_ext' | |
require 'yaml' | |
require 'database_cleaner' | |
require 'factory_girl' | |
# DBへの接続 | |
dbf = File.dirname(__FILE__) + "/../config/database.yml" | |
dbconfig = YAML::load(File.open(dbf)) | |
ActiveRecord::Base.establish_connection(dbconfig['development']) | |
# ajaxの通信などで待ちが発生する場合の最大待ち時間 | |
Capybara.default_max_wait_time = 30 | |
# Capybaraにselenium(ie)を使うように設定 | |
Capybara.register_driver :selenium do |app| | |
Capybara::Selenium::Driver.new(app, :browser => :ie) | |
end | |
Capybara.default_driver = :selenium | |
# データベースに初期値などを設定するためのActiveRecordモデルをrequireする | |
Dir[File.dirname(__FILE__) + '/models/*.rb'].each { |file| require file } | |
RSpec.configure do |config| | |
config.include Capybara::DSL | |
#factroy_girlの設定 | |
config.include FactoryGirl::Syntax::Methods | |
config.before(:all) do | |
FactoryGirl.reload | |
end | |
#database_cleanerの設定 | |
config.before(:suite) do | |
DatabaseCleaner.strategy = :truncation | |
DatabaseCleaner.clean_with(:truncation) | |
end | |
config.before(:each) do | |
DatabaseCleaner.start | |
end | |
config.after(:each) do | |
DatabaseCleaner.clean | |
end | |
end |
データベースのデータにアクセスするためのActiveRecordモデルを作成します。
specフォルダ内にmodelsフォルダを作成して、ここにrailsと同じようにモデルを作成します。
ここではItemクラスを例として作成しています。
spec/models/items.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
class Item < ActiveRecord::Base | |
self.table_name = "shohin_mst" | |
end |
データベースにテストデータを登録するためのFactoryGirlの設定を作成します。
specフォルダ内にfactoriesフォルダを作成して、この中に作成します。
上記のItemクラスに設定する値を例として作成しています。
spec/factories/items.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
FactoryGirl.define do | |
factory :item do | |
code "TEST-ITEM123" | |
name "テスト商品123" | |
amount 1000 | |
end | |
end |
これらの準備ができたらspecフォルダ内にspecファイルを作成します。
spec/sample_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
describe "rubyでできてないサイトのテスト", type: :feature, js: true do | |
before do | |
Capybara.app_host = 'http://test.hogehoge.co.jp/' | |
create(:item) | |
visit '/item_list.aspx' | |
end | |
it "商品情報が表示される" do | |
expect(page).to have_content("テスト商品123") | |
end | |
it "商品の値段が表示される" do | |
expect(page).to have_content("¥1,000(税別)") | |
end | |
end |
テストの実行は作成したフォルダの一番上(Gemfileがあるところ)で以下を実行します。
rspec
こんな感じでとりあえず、データベースにテスト用データを突っ込んで、毎回データベースを初期化するための環境ができあがります。
今回のフォルダ構成をまとめると以下のような感じなります。
+-config | +-database.yml +-spec | +-factories | | +-items.rb | +-models | | +-items.rb | +-spec_helper.rb | +-sample_spec.rb +-.rspec +-Gemfile +-Gemfile.lock
以下のサイトが参考になりました。
http://kakakakakku.hatenablog.com/entry/2016/01/09/142221