有没有人找到在多个浏览器/网络驱动程序上运行相同的黄瓜场景的方法?

2024-02-11

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

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

沿着钩子线,有来自黄瓜文档的内容:
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...
这可能是一条潜在的路径,但我也没有想出任何在这里可行的方法。

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

我已经研究过重写黄瓜的功能运行器,但这看起来并不容易或不友好。
请帮助?任何人?


所以,我最终推出了自己的解决方案。不确定这是否是最好的或最优雅的方法,但我实际上只是结束了:

  1. 将所有常见环境的东西抽象成env.rb
  2. 使用 Cucumber 配置文件需要特定的环境文件(例如 firefox.rb)env.rb然后将 Capybara 的默认驱动程序设置为适当的驱动程序。
  3. 写了一篇大文thor https://github.com/wycats/thor#readme类任务捆绑了一堆黄瓜命令,并调用使用正确的配置文件运行坏男孩。
  4. 编写了一个“all_browsers”任务,它将命令捆绑在一起,然后调用每个特定的驱动程序任务,因此我现在可以有一个任务来运行我在所有支持的驱动程序上提供的任何一组场景。

像魅力一样工作,我认为最终可能比我上面尝试的任何东西都更好,因为在 Thor 文件中我能够添加诸如基准测试选项之类的东西,以及是否拆分功能运行分成多个线程。 仍然好奇是否有其他人为此提出解决方案。

黄瓜.yaml:
在这里,all_features 文件只执行以 .feature 结尾的所有内容,因为如果我拉入整个 features 目录,它就会拉入一切在它下面,包括所有配置文件等,这不是我想要的,因为每个配置文件将默认的水豚驱动程序设置为不同的值。一旦您指定-r作为黄瓜的选择,all自动加载any文件已停止。

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

Or
thor feature_runner:celerity_runner
这将仅快速运行所有功能。

但我现在还可以为 thor 命令提供一些其他选项,这些选项会传递给 cucumber,例如:
--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 命令:
thor feature_runner:all_drivers_runner --threaded --tags=@all_browsers

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

有没有人找到在多个浏览器/网络驱动程序上运行相同的黄瓜场景的方法? 的相关文章

  • 如何制作可选的强参数键但过滤嵌套参数?

    我的控制器中有这个 params require item permit 让我们假设这个 rspec 规范 它按预期工作 put update id item id item name new name 然而 以下原因ActionContr
  • 如何使用 new 干净地初始化 Ruby 中的属性?

    class Foo attr accessor name age email gender height def initalize params name params name age params age email params e
  • 如何在自定义类上使用 ActiveModel 验证和回调?

    我正在尝试在自定义类上使用 ActiveModel 验证和回调 class TestClass include ActiveModel Validations define model callbacks validate attr acc
  • 在 Ruby 中,如何指定另一个目录中的文件作为输入?

    这可能有一个简单的答案 但我正在开发一个测试套件 它需要位于不同文件夹中的输入文件 我想使用相对路径 如下所示 graph Graph new lib test input txt 但鲁比不喜欢这样 使用这样的相对文件路径的最佳方法是什么
  • Application.css.scss 导致 Rails 4 出现问题

    所以我一直在学习 ruby on Rails 我决定在 myrubyblog 应用程序上的新帖子页面添加一些样式 一切正常 几天后 我决定将 posts scss 文件中的 scss 导入到 application css scss 中 然
  • 从 Promise 块返回函数值

    我正在尝试编写一个函数 使用 WebdriverJS lib 来迭代元素列表 检查名称并构建与该名称对应的 xpath 定位器 我这里简化了xpath定位器 大家不用关注 我在这里面临的问题是 1 调用该函数返回未定义 据我了解 这是因为
  • 使用 selenium 在 python 中切换到弹出窗口

    如何在下面的 selenium 程序中切换到弹出窗口 我已经查找了所有可能的解决方案 但无法解决它们 请帮忙 from selenium import webdriver from splinter import Browser from
  • 不使用 RVM 时的 Cron +

    我使用的是RVM环境 RUby 版本 2 1 2 导轨 4 1 1 schedule rb every 1 minute do runner note send mail end I used whenever update crontab
  • Ruby:邮件 gem 在邮件中的 60 个字符后添加 \r\n

    我要移植Actionmailer x509 https github com petRUShka actionmailer x509到 Rails 3 为了做到这一点 我尝试从带有签名电子邮件的大字符串创建 Mail 对象 您可以在这一行看
  • 没有路线匹配... Rails Engine

    所以我不断收到错误 No route matches action gt create controller gt xaaron api keys 测试中抛出的是 it should not create an api key for th
  • 未定义符号:尝试运行瘦网络服务器时的 SSLv2_method

    我已经用 rvm 安装了 OpenSSL rvm pkg install openssl 然后做了rvm reinstall 1 9 3 with openssl dir rvm path usr 当我尝试运行瘦网络服务器时 出现以下错误
  • Rails 中带有 text_field 的逗号分隔数组

    我有一些users可以有很多posts 并且每个帖子都可以有很多tags 我已经使用一个实现了拥有并属于许多帖子和标签之间的关系 创建新帖子时 用户可以使用逗号分隔的值列表对其进行标记 就像在 SO 上发布新问题时一样 如果任何标签尚不存在
  • #freeze 除了防止修改之外还有其他用途吗?

    Ruby s 标准uri library https github com ruby ruby tree trunk lib uri对于无法修改或修改不会造成损害的对象 冻结有很多用途 user password ui split free
  • 使用无头 Chrome 浏览器时出现 ElementNotVisibleException

    当我在无头模式 Chrome 浏览器中运行测试脚本时 元素链接不可见 无法执行linkElement click 在头部模式下一切正常 所有其他信息都在堆栈跟踪中 请问有人知道该怎么办吗 堆栈跟踪 发生错误 消息 元素不可见 会话信息 he
  • 无法在 Windows 7 上安装 Rmagick 和 Imagemagick

    当我跑步时gem install rmagick 2 13 1 gem从 rmagick 2 13 1 gem 所在的目录中 我收到一个错误 指出它无法构建 gem 本机扩展 下面显示 c Ruby192 bin ruby exe extc
  • rvm + Rails3 + gmaps4rails -acts_as_gmappable

    我是一个红宝石导轨之类的菜鸟 抱歉 如果我的问题很愚蠢 我设置了一个运行 ruby 1 8 7 p334 的 rvm 作为用户 环境 我已经为我的 应用程序 创建了一个 gemset 现在我想在页面上显示地址的地图 我四处寻找 发现了 gm
  • 使用 Ruby Curb 传递 GET 参数

    我正在尝试使用 Curb curb rubyforge org 调用需要在 get 请求中提供参数的 RESTful API 我想获取一个像这样的URLhttp foo com bar xml bla blablabla 我希望能够做类似的
  • 创建并初始化具有连续名称的类的实例

    我有一个BankAccount班级 我试图创建这个类的多个实例并将它们放入一个数组中 例如 accounts Ba1 BankAccount new 100 Ba2 BankAccount new 100 我想初始化包含大量实例的数组 假设
  • Rspec 通过 mTurk 测试实时结果

    我正在通过 Rspec 测试代码在 mTurk 上创建点击 但同时我需要测试必须从 mTurk 发回的结果 为了节省每次测试的复杂性 我使用 VCR 将 HTTP 请求记录在盒式磁带中 我该如何实施这个测试 好吧 我为此做了一些修改 我使用
  • webdriver-manager 10.2.9 上的语法错误

    发帖自问题 170 https github com angular webdriver manager issues 170 更新到最新版本后 运行 webdriver manager 命令时出现以下错误 C Users user App

随机推荐