所以,我結束了自己的解決方案。不知道是否這是最好的或最簡潔的方法,但其實我只是清盤:
- 抽象所有的公共環境中的東西變成
env.rb
- 使用黃瓜型材這就需要一個特定的環境文件(如Firefox。 rb)需要
env.rb
,然後將Capybara的默認驅動程序設置爲適當的驅動程序。
- 寫了一個大的ol'thor類,其中包含捆綁一堆黃瓜命令的任務,並呼籲使用正確的配置文件運行壞男孩。
- 寫了一個捆綁命令的'all_browsers'任務,然後調用每個特定的驅動程序任務,所以現在我可以有一個任務運行我在所有支持的驅動程序上提供的任何一組場景。
工作就像一個魅力,我認爲可能實際上已經結束了,到底比任何我在上面試圖更好,因爲雷神文件我能夠添加一些東西像一個標杆的選擇,以及是否內不要將功能分解成多個線程。 仍然好奇,如果有人想出了這個解決方案雖然。
cucumber.yaml:
這裏,all_features文件少了點在.feature結束一切的水珠,因爲如果我在整個功能目錄拉,將在一切拉之下,包括所有的個人資料文件等,這不是我想要的,因爲每個配置文件將默認水豚驅動程序設置爲不同的值。一旦你指定-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
這隻會在運行波速所有功能。
但我現在還可以提供一些其他的選項,從而獲得傳遞到黃瓜,如雷神命令:
[email protected]_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標記功能,我可以構建一個套件來測試所有水豚驅動程序,在多線程環境中,一個雷神命令:
thor feature_runner:all_drivers_runner --threaded [email protected]_browsers
或建立了一個在快速性運行冒煙測試套件:
thor feature_runner:celerity_runner [email protected]_test
所以這似乎與硒有關。我希望能夠對付水豚支持的任何驅動程序 - 特別是速度。這裏的代碼也沒有太多,我懷疑事情的背後有很多問題。 – bergyman 2010-12-08 17:37:33
那麼你有「硒」標籤,但不是「celerity」標籤。 – 2010-12-08 19:06:43
採取了點。 celerity標籤添加。應該澄清,我想運行它反對硒和迅速。 – bergyman 2010-12-08 21:28:51