因此,我总结了自己的解决方案。不知道这是最好还是最优雅的方法,但实际上我只是总结:
- 将所有常见环境的东西抽象为
env.rb
- 使用需要特定环境文件(例如firefox.rb)的Cucumber配置文件
env.rb
,然后将Capybara的默认驱动程序设置为适当的驱动程序。 - 写了一大醇” 雷神类捆绑了一堆黄瓜命令,招呼一下,用正确的配置文件中运行的坏小子任务。
- 编写了一个“ all_browsers”任务,该任务将命令捆绑在一起,然后调出每个特定的驱动程序任务,因此我现在可以拥有一个任务,该任务可以运行在所有受支持的驱动程序上提供的任何场景。
像魅力一样工作,我认为最终实际上可能比我上面尝试的要好,例如,在Thor文件中,我能够添加诸如基准测试选项之类的东西,以及是否拆分功能运行分成多个线程。仍然好奇是否有人为此提出了解决方案。
cumul.yaml
:在这里,all_features文件只是对所有以.feature结尾的内容进行了处理,因为如果我拉入整个features目录,它将拉入其下的 所有
内容,包括所有配置文件,等等,这不是什么。我想要,因为每个配置文件都将默认的水豚驱动程序设置为其他值。一旦您指定
-r作为一个选项,以黄瓜, 所有
的自动加载 任何 文件被暂停。
default: --format prettychrome: --format pretty -r features/support/profiles/chrome.rb -r features/all_features -r features/step_definitionsfirefox: --format pretty -r features/support/profiles/firefox.rb -r features/all_features -r features/step_definitionscelerity: --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_firefoxend
selenium_firefox.rb(我在其中注册驱动程序,并设置了一些我现在不需要的标签功能,因为
@selenium_firefox标签是我最初在问题中提出的尝试的一部分):
# Register a specific selenium driver for firefoxCapybara.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 firefoxBefore('@selenium_firefox') do Capybara.current_driver = :selenium_firefoxendfeature_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 endend相对于根目录而言,一切都结束了:
.|____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 browsersthor feature_runner:celerity_runner # Run features on celeritythor feature_runner:chrome_runner # Run features on chromethor 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标记功能,则可以在一个多线程环境中使用一个thor命令构建一套套件,以针对所有capybara驱动程序进行测试:
thor feature_runner:all_drivers_runner --threaded --tags=@all_browsers
或构建一个可以快速运行的烟雾测试套件:
thor feature_runner:celerity_runner --tags=@smoke_test



