複数のブラウザ/Webドライバーで同じキュウリのシナリオを実行する方法を見つけた人はいますか?
質問
いくつかのWeb自動化テストには、キュウリ +カピバラを使用しています。自分のタグ(シナリオの前に@all_browsersのようなもの)を配線し、設定したWebドライバーのリスト(Celerity、Selenium on Firefox、IE、Chrome)に対して実行できるようにしたいと思います。前面に4つの異なるタグを使用して、シナリオを4つの異なる時間を書く必要はありません。私は以下を登録する新しいドライバーでこれをやろうとしています。
Capybara.register_driver :all_browsers do |app|
# What would even work in here? I don't think anything will.
end
そして、それをフォローしてください:
Before('@all_browsers') do
# Same problem here.
end
しかし、実際に機能する可能性のある方法の前に何を入れるべきかはよくわかりません。
特にキュウリフックを使用してみました。
Around('@all_browsers') do |scenario, block|
Capybara.current_driver = :selenium_firefox
block.call
Capybara.current_driver = :selenium_chrome
block.call
# etc
end
しかし、これは私が望んでいたように振る舞いません。同じドライバーを使用して、シナリオを2回実行します。
フックラインに沿って続いて、キュウリのドキュメントからこれがあります。
You may also provide an AfterConfiguration hook that will be run after Cucumber has been configured. This hook will run only once; after support has been loaded but before features are loaded. You can use this hook to extend Cucumber, for example you could affect how features are loaded...
これは、これのために降りる潜在的な道かもしれませんが、私はここでも機能するものを思いつくことができませんでした。
私はカスタムフォーマッタを調べましたが、実際にはまさにそれを行うように見えます - 出力をフォーマットし、機能の実際にどのように実行されるかをそれほど指定していません。
私はオーバーライドキュウリの機能ランナーを検討しましたが、それは簡単でフレンドリーではありません。
助けてください?誰?
解決
それで、私はこれに私自身の解決策を転がすことになりました。それが最良か最もエレガントなアプローチであるかはわかりませんが、私は実際に巻き上げられました:
- すべての一般的な環境のものを抽象化します
env.rb
- 必要な特定の環境ファイル(firefox.rbなど)を必要とするキュウリプロファイルを使用する
env.rb
次に、カピバラのデフォルトのドライバーを適切なドライバーに設定します。 - 大きなolを書いた ソー たくさんのキュウリのコマンドを束ねて、適切なプロファイルで悪い男の子を実行するよう呼びかけるタスクを備えたクラス。
- コマンドをバンドルする「All_Browsers」タスクを作成してから、特定のドライバータスクを各タスクに呼びかけるため、サポートされているすべてのドライバーに提供するシナリオのセットを実行するタスクを1つ持つことができます。
魅力のように働いていて、トールファイル内でベンチマークオプションのようなものを追加することができ、機能の実行を分割するかどうかを追加することができたので、私が上で試していたものよりも最終的に実際に巻き上げられたかもしれないと思います複数のスレッドに上がります。しかし、他の誰かがこのための解決策を思いついたのではないか興味があります。
cucumber.yaml:
ここで、all_featuresファイルは、featureで終わるすべてのグローブを実行するだけです。なぜなら、私が機能全体を引いた場合、それは引き込むからです すべての その下には、すべてのプロファイルファイルなどを含む。各プロファイルファイルがデフォルトのCapybaraドライバーを別の値に設定するため、私が望んでいたものではありません。指定したら -r
キュウリのオプションとして、 すべて の自動装置 どれか ファイルが停止します。
default: --format pretty
chrome: --format pretty -r features/support/profiles/chrome.rb -r features/all_features -r features/step_definitions
firefox: --format pretty -r features/support/profiles/firefox.rb -r features/all_features -r features/step_definitions
celerity: --format pretty -r features/support/profiles/celerity.rb -r features/all_features -r features/step_definitions
firefox.rb(「プロファイル」ファイル):
require File.dirname(__FILE__) + "/../env.rb"
Capybara.configure do |config|
config.default_driver = :selenium_firefox
end
selenium_firefox.rb(ドライバーを登録し、今では必要としないタグ機能を設定します。 @selenium_firefox
タグは、質問に投稿されたこれでの私の最初の試みの一部でした):
# Register a specific selenium driver for firefox
Capybara.register_driver :selenium_firefox do |app|
Capybara::Driver::Selenium.new(app, :browser => :firefox)
end
# Allows the use of a tag @selenium_firefox before a scenario to run it in selenium with firefox
Before('@selenium_firefox') do
Capybara.current_driver = :selenium_firefox
end
feature_runner.thor:
require 'benchmark'
class FeatureRunner < Thor
APP_ROOT = File.expand_path(File.dirname(__FILE__) + "/../")
# One place to keep all the common feature runner options, since every runner in here uses them.
# Modify here, and all runners below will reflect the changes, as they all call this proc.
feature_runner_options = lambda {
method_option :verbose, :type => :boolean, :default => true, :aliases => "-v"
method_option :tags, :type => :string
method_option :formatter, :type => :string
method_option :other_cucumber_args, :type => :string
}
desc "all_drivers_runner", "Run features in all available browsers"
method_option :benchmark, :type => :boolean, :default => false
method_option :threaded, :type => :boolean, :default => true
feature_runner_options.call # Set up common feature runner options defined above
def all_drivers_runner
if options[:threaded]
feature_run = lambda {
thread_pool = []
t = Thread.new do |n|
invoke :firefox_runner
end
thread_pool << t
t = Thread.new do |n|
invoke :chrome_runner
end
thread_pool << t
t = Thread.new do |n|
invoke :celerity_runner
end
thread_pool << t
thread_pool.each {|th| th.join}
}
else
feature_run = lambda {
invoke "feature_runner:firefox_runner", options
invoke "feature_runner:chrome_runner", options
invoke "feature_runner:celerity_runner", options
}
end
if options[:benchmark]
puts "Benchmarking feature run"
measure = Benchmark.measure { feature_run.call }
puts "Benchmark Results (in seconds):"
puts "CPU Time: #{measure.utime}"
puts "System CPU TIME: #{measure.stime}"
puts "Elasped Real Time: #{measure.real}"
else
feature_run.call
end
end
desc "firefox_runner", "Run features on firefox"
feature_runner_options.call # Set up common feature runner options defined above
def firefox_runner
command = build_cucumber_command("firefox", options)
run_command(command, options[:verbose])
end
desc "chrome_runner", "Run features on chrome"
feature_runner_options.call # Set up common feature runner options defined above
def chrome_runner
command = build_cucumber_command("chrome", options)
run_command(command, options[:verbose])
end
desc "celerity_runner", "Run features on celerity"
feature_runner_options.call # Set up common feature runner options defined above
def celerity_runner
command = build_cucumber_command("celerity", options)
run_command(command, options[:verbose])
end
private
def build_cucumber_command(profile, options)
command = "cd #{APP_ROOT} && ./bin/cucumber -p #{profile}"
command += " --tags=#{options[:tags]}" if options[:tags]
command += " --formatter=#{options[:formatter]}" if options[:formatter]
command += " #{options[:other_cucumber_args]}" if options[:other_cucumber_args]
command
end
def run_command(command, verbose)
puts "Running: #{command}" if verbose
output = `#{command}`
puts output if verbose
end
end
ルートディレクトリに関連して、すべてが巻き上げられた場所:
.
|____cucumber.yml
|____features
| |____all_features.rb
| |____google_search.feature
| |____step_definitions
| | |____google_steps.rb
| | |____web_steps.rb
| |____support
| | |____custom_formatters
| | | |____blah.rb
| | |____env.rb
| | |____paths.rb
| | |____profiles
| | | |____celerity.rb
| | | |____chrome.rb
| | | |____firefox.rb
| | |____selenium_drivers
| | | |____selenium_chrome.rb
| | | |____selenium_firefox.rb
| | | |____selenium_ie.rb
| | | |____selenium_remote.rb
| | |____selenium_drivers.rb
|____tasks
| |____feature_runner.thor
| |____server_task.rb
の出力 thor -T
feature_runner
--------------
thor feature_runner:all_drivers_runner # Run features in all available browsers
thor feature_runner:celerity_runner # Run features on celerity
thor feature_runner:chrome_runner # Run features on chrome
thor feature_runner:firefox_runner # Run features on firefox
今、私は次のようなものを実行できます:
thor feature_runner:all_drivers_runner --benchmark
これにより、各ドライバーのスレッド内のすべてのカピバラドライバーのすべての機能が実行され、結果がベンチマークされます。
または
thor feature_runner:celerity_runner
これにより、すべての機能がCelerityでのみ実行されます。
しかし、次のようなキュウリに渡されるThorコマンドに他のいくつかのオプションを提供することもできます。
--tags=@all_browsers
--formatter=hotpants
--other_cucumber_args="--dry-run --guess --etc"
機能ファイルがどのように見えるか:
Feature: Start up browser
@all_browsers
Scenario: Search Google
Given I am on the home page
When I fill in the search bar with "Capybara"
And I press "Search"
Then I should see "Capybara"
多くのセットアップのように思えますが、@all_browsersで機能にタグを付けると、マルチスレッド環境ですべてのCapybaraドライバーに対してテストするスイートを構築することができます。
thor feature_runner:all_drivers_runner --threaded --tags=@all_browsers
または、セレリティで実行される煙テストスイートを構築します。
thor feature_runner:celerity_runner --tags=@smoke_test
他のヒント
これは、Saucelabsによるホストされたサービスを通じて可能です。 キュウリソース GEMは、並行したマルチブラウザーテストを提供します。
または、自分で実装したい場合は、その宝石の源から借りることができる場合があります。
これが私のハックです:(私の状況はJavaScript OnとJavaScriptをオフにして機能する機能を証明しています)
- 各シナリオを独自の機能ファイルに入れます。
- 最後の行を「背景:」セクションに移動します。
- 最後の行をブラウザごとにシナリオに入れます
各シナリオに適切にタグを付けます
Feature: a feature Background: Given a user "Jim" exists Given a user "Mike" exists When I login as "mike" And I follow "Lesson 1" And I follow "Upload a video" Then "#upload_a_video" should be active And I fill in "video_title" with "my film" And I attach the file "video.mov" to "video_upload" And I press "Post" Scenario: normal And I should see "my film" @javascript Scenario: javascript And I should see "my film"
私はそれを「ワタル」プロジェクトが推奨するようにします。私が使う require parallel_cucumber
私のrakefileでは、各キュウリのシナリオが独自の並列スレッドを取得します(この場合は最大20まで)。
task :run_cucumber do
FileUtils.mkpath(ENV['OUT_DIR'])
begin
# cannot format report as HTML because of parallel forking
threads = 20
@result = system "parallel_cucumber features -o \"--format junit --out #{ENV['OUT_DIR']} --format pretty --tag @sauce\" -n #{threads}"
ensure
@success &= @result
end
end
次に、キュウリプロジェクトの残りの部分はいつものように書くことができます!!とても簡単!ここでの私の例: https://github.com/djangofan/cuke-parallel-starter