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
但是我不太清楚那个可能实际工作的Before方法.
我试过使用黄瓜钩,具体来说:
Around('@all_browsers') do |scenario,block| Capybara.current_driver = :selenium_firefox block.call Capybara.current_driver = :selenium_chrome block.call # etc end
但这并不像我曾经希望的那样.它使用相同的驱动程序并运行该场景两次.
沿着钩线,黄瓜文档中有这样的:
您还可以提供一个AfterConfiguration钩子,该配置将在配置完黄瓜后运行.这个钩子只运行一次;在加载支持之后,加载功能之前.您可以使用此钩子来扩展黄瓜,例如,您可能会影响功能的加载方式…
这可能是一个潜在的路径,但是我还没有想出任何在这里工作的东西.
我已经研究过自定义格式化程序,但是它们真的只是看起来像这样做 – 格式化输出,而不是指定功能实际运行的方式.
我看过黄瓜的特色赛跑者,但看起来不容易或友善.
请帮助?任何人?
解决方法
>将所有常见的环境文件抽象成env.rb
>使用需要env.rb的特定环境文件(如firefox.rb)的黄瓜档案,然后将Capybara的默认驱动程序设置为适当的驱动程序.
>写了一个大型的ol’thor课程,捆绑一堆黄瓜命令的任务,并呼吁运行坏男孩的正确的配置文件.
>编写一个“all_browsers”任务来捆绑命令,然后调用每个特定的驱动程序任务,所以我现在可以有一个任务运行我提供的所有支持的驱动程序的任何一组场景.
像一个魅力一样工作,我认为最终可能比上面尝试的更好地结束了,因为在Thor文件中,我可以添加一些基准选项,以及是否分割功能运行多个线程.
如果有人提出了解决方案,仍然好奇.
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(‘profile’文件):
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,:default => false method_option :threaded,: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[: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[: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
胸部输出
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
这将在每个驱动程序的线程中运行所有capybara驱动程序的所有功能,从而达到结果.
要么
thor feature_runner:celerity_runner
这将运行所有功能只有在速度.
但是现在我还可以提供一些其他的选项,这些命令可以传递给黄瓜,如:
–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命令:
thor feature_runner:all_drivers_runner –threaded –tags = @ all_browsers
或者建立一个以高速运行的烟雾测试套件:thor feature_runner:celerity_runner –tags = @ smoke_test