10

いくつかの Web 自動化テストにキュウリ + カピバラを使用しています。自分のタグ (シナリオの前に @all_browsers のようなもの) を接続して、設定した Web ドライバーのリスト (celerity、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

しかし、実際に機能する可能性のあるその Before メソッドに何を入れればよいかはよくわかりません。

具体的には、キュウリフックを使用してみました:

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...
これがあります。

私はカスタムフォーマッタを調べましたが、実際には、出力をフォーマットするだけで、機能が実際にどのように実行されるかを指定しているようには見えません。

cucumber の機能ランナーをオーバーライドすることを検討しましたが、それは簡単でも友好的でもありません。
助けてください?誰?

4

4 に答える 4

9

それで、私はこれに対する独自のソリューションを展開することになりました。それが最善のアプローチなのか、最もエレガントなアプローチなのかはわかりませんが、実際には次のようになりました。

  1. すべての一般的な環境のものを抽象化するenv.rb
  2. 特定の環境ファイル (firefox.rb など) を必要env.rbとする Cucumber プロファイルを使用し、Capybara のデフォルト ドライバーを適切なドライバーに設定します。
  3. きゅうりのコマンドをまとめて、適切なプロファイルでバッドボーイを実行するように呼びかけるタスクを含む、古いトールクラスを作成しました。
  4. コマンドをまとめて特定の各ドライバー タスクを呼び出す「all_browsers」タスクを作成したので、サポートされているすべてのドライバーで提供する任意のシナリオ セットを実行する 1 つのタスクを作成できます。

魔法のように機能し、Thor ファイル内でベンチマーク オプションのようなものを追加することができ、機能の実行を分割するかどうかを指定することができたので、実際には上で試した何よりも最終的にうまくいったかもしれません。複数のスレッドに。ただし、他の誰かがこれに対する解決策を思いついたかどうかはまだ興味があります。

cucumber.yaml:
ここで、all_features ファイルは単に .feature で終わるすべてのグロブを実行します。これは、features ディレクトリ全体を取得すると、すべてのプロファイル ファイルなどを含む、その下のすべてが取得されるためです。各プロファイルファイルがデフォルトのカピバラドライバーを異なる値に設定するので、私は欲しかった. -rcucumber のオプションとして指定すると、ファイルのすべての自動ロードが停止します。

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
これはすべての機能をセレリティでのみ実行します。

しかし、キュウリに渡される 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 のタグを付けると、1 つのトール コマンドでマルチスレッド環境ですべてのカピバラ ドライバーに対してテストするスイートを構築できます。
thor feature_runner:all_drivers_runner --threaded --tags=@all_browsers

または、celerity で実行されるスモーク テスト スイートを構築します。
thor feature_runner:celerity_runner --tags=@smoke_test

于 2010-12-09T22:47:38.077 に答える
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"
    
于 2010-12-09T23:32:42.270 に答える
0

これは、SauceLabs によるホスト サービスを通じて可能です。Cucumber Sauce gem は、並列のマルチブラウザー テストを提供します。

または、自分で実装したい場合は、その gem のソースから借りることができる場合があります。

于 2010-12-08T13:11:35.990 に答える