有没有人想出办法在多个浏览器/网络驱动程序上运行相同的黄瓜方案?

时间:2010-12-08 05:04:54

标签: ruby selenium cucumber capybara celerity

我正在使用黄瓜+水豚进行一些网络自动化测试。我希望能够连接我自己的标签(类似于场景之前的@all_browsers)并让它针对我设置的网络驱动程序列表(快速,firefox上的selenium,即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

但这并不像我希望的那样。它使用相同的驱动程序并使用它运行场景两次。

沿着勾线,黄瓜文件中有这个:
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...
这可能是一条潜在的道路,但我还没有设法在这里发挥作用。

我研究过自定义格式化程序,但它们看起来确实只是这样 - 格式化输出,而不是指定功能实际运行的方式。

我已经研究过覆盖黄瓜的功能转轮,但这看起来并不容易或友好 请帮忙?任何人吗?

3 个答案:

答案 0 :(得分:9)

所以,我结束了自己的解决方案。不确定它是最好还是最优雅的方法,但实际上我刚刚结束:

  1. 将所有常见环境内容抽象为env.rb
  2. 使用需要env.rb的特定环境文件(例如firefox.rb)的Cucumber配置文件,然后将Capybara的默认驱动程序设置为相应的驱动程序。
  3. 写了一个很棒的'thor课程,其任务包括一堆黄瓜命令,然后召唤出一个带有正确轮廓的坏男孩。
  4. 写了一个'all_browsers'任务,它捆绑了命令,然后调用每个特定的驱动程序任务,所以我现在可以有一个任务运行我在所有支持的驱动程序上提供的任何场景。
  5. 像魅力一样工作,我认为最终可能比我上面尝试的任何事情都要好,因为在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('个人资料'文件):

    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
    这将在每个驱动程序的线程中运行所有capybara驱动程序的所有功能,从而对结果进行基准测试。

    或者
    thor feature_runner:celerity_runner
    这将仅在快速运行时运行所有功能。

    但我现在还可以为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命令:
    thor feature_runner:all_drivers_runner --threaded --tags=@all_browsers

    或者建立一个快速运行的烟雾测试套件:
    thor feature_runner:celerity_runner --tags=@smoke_test

答案 1 :(得分:0)

这可以通过SauceLabs的托管服务实现。 Cucumber Sauce gem为您提供并行的多浏览器测试。

或者,如果您想自己实施,可以借用该gem的来源。

答案 2 :(得分:0)

这是我的黑客: (我的情况是证明一个功能适用于javascript和javascript关闭)

  1. 将每个方案放入其自己的要素文件中。
  2. 将每一行但最后一行移至“背景:”部分。
  3. 将最后一行放入每个浏览器的方案
  4. 正确标记每个方案

    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"