黄瓜测试突然停止

时间:2014-02-04 10:57:31

标签: ruby-on-rails ruby-on-rails-4 cucumber capybara capybara-webkit

我的功能如下:

Feature: Searching chats
In order to find chats
As an user
I want to find different chats by username or ad name

Background:
  Given System prepares for chats
  And There is a few machines with names:
    | machine_1 |
    | machine_2 |
    | machine_3 |
  And There is a few services with names:
    | service_1 |
    | service_2 |
    | service_3 |
  And I have chats with ads owners

Scenario: Searching chats
  When I am logged in as a "user"
  And I go to chats page   # <- stops here
  Then I should see search results when I fill form with:
    | input  | results |
    | ma     | machine_1, machine_2, machine_3 |
    | se     | service_1, service_2, service_3 |                                                                         

当我启动黄瓜功能(或场景)时,它突然停在步骤“我去聊天页面”没有任何错误消息。结果如下:

[alex@MacBookPro ~/my_project | master]$ cucumber features/chat/search.feature
Using the default profile...
@javascript
Feature: Searching chats
  In order to find chats
  As an user
  I want to find different chats by username or ad name

  Background:                               # features/chat/search.feature:8
    Given System prepares for chats         # features/step_definitions/chats.steps.rb:11
    And There is a few machines with names: # features/step_definitions/machine.steps.rb:10
      | machine_1 |
      | machine_2 |
      | machine_3 |
    And There is a few services with names: # features/step_definitions/service.steps.rb:144
      | service_1 |
      | service_2 |
      | service_3 |
    And I have chats with ads owners        # features/step_definitions/chats.steps.rb:5

  Scenario: Searching chats                                 # features/chat/search.feature:20
    When I am logged in as a "user"                         # features/step_definitions/user.steps.rb:68
    And I go to chats page                                  # features/step_definitions/chats.steps.rb:17
[alex@MacBookPro~/my_project | master]$

这是我的“堕落”步骤:

When /^I go to chats page$/ do
  visit root_path

  within('.global-menu') do
    click_on username(@current_user)
    click_on I18n.t('views.menu.profile.links.dashboard')
  end

  click_on I18n.t('views.menu.profile.links.chats')
end

Then(/^I should see search results when I fill form with:$/) do |table|
  table.hashes.each do |search_data|
    ### searching ###
    @page.query.set search_data['input']

    # for AJAX search
    sleep 1

    ### show results ###
    search_data['results'].split(', ').each do |res|
      page.should have_content res.mb_chars.upcase
    end

    within('#chats') do
      page.all('.chat').length.should == search_data['results'].split(', ').size
    end
  end
end

我正在使用带黄瓜的capybara-webkit。那是我的env.rb:

require 'rubygems'
require 'spork'
require 'capybara'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'site_prism'
require 'capybara-screenshot/cucumber'
# require 'cucumber/rails'


# 1) Tag your scenario (or feature) with @allow-rescue
#
# 2) Set the value below to true. Beware that doing this globally is not
# recommended as it will mask a lot of errors for you!
#
# ActionController::Base.allow_rescue = false

#############################################################################

ENV['SKIP_RAILS_ADMIN_INITIALIZER'] = 'false'                                   # This fixes weird errors with cucumber + rails_admin (http://makandracards.com/makandra/9597-rake-spec-+-rails_admin-weirdly-failing-specs).

#############################################################################

Before do
  DatabaseCleaner.strategy = :truncation

  DatabaseCleaner.clean
  FactoryGirl.create(:setting)
  ContactType.generate_contact_types
  ContactType.generate_ims
end

Spork.prefork do
  require 'cucumber/rails'
  require 'email_spec' # add this line if you use spork
  require 'email_spec/cucumber'
  Capybara.default_selector = :css
end

Spork.each_run do
  ActionController::Base.allow_rescue = false

  begin
    DatabaseCleaner.strategy = :truncation
  rescue NameError
    raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
  end

  Capybara.register_driver :webkit do |app|
    Capybara::Webkit::Driver.new(app, :stderr => nil)
  end

  Capybara.javascript_driver = :webkit

  Cucumber::Rails::Database.javascript_strategy = :truncation
end

当我将项目更新为rails4时出现问题。有什么想法吗?

4 个答案:

答案 0 :(得分:3)

我管理的许多开发人员发现Capybara-webkit确实存在问题且不一致。

poltergeist/PhantomJS比它有很多优点。一般来说:

  • 更具确定性,因为问题场景更容易失败而不是片状
  • 与机器相关性较小;我们的套件现在在我们所有的测试环境中都表现得非常相似
  • 提供更好的错误消息
  • 当有Javascript错误时,
  • 会失败,即使测试会通过
  • 也是如此
  • 不会挂起,
  • 更容易安装。

在2月份自动化测试SF聚会上,这是一个关于 good post from Dave Schweisguth his presentation ,在那里他讨论了他的公司(Fandor)的测试设置/环境,问题和故障排除以及快速比较。它可能有助于您追踪问题。

答案 1 :(得分:2)

好的,我没有答案,但我有更多证据可以解决问题。

这适用于rspec,但我认为它对Cucumber也应该是相同的:

rspec spec/ --formatter progress --out rspec.output.txt

看起来像STDOUT的指针正在以某种方式被捣碎。通过指定输出文件并拖尾它,您应该看到完整的输出。

我尝试了所有不同的格式化程序,无论如何,如果它们输出到STDOUT,输出会在某个地方丢失。

答案 2 :(得分:1)

在使用Selenium网络驱动程序时,我遇到了类似的问题,原因不明。但当我转向Poltergeist(PhantomJS)时,它开始起作用了。

另外,我注意到,您需要selenium驱动程序,但是,您正在使用Webkit。

更改驱动程序后,尝试在没有Spork运行的情况下运行所有​​内容。

答案 3 :(得分:0)

使用thin web server代替webkit,并将以下代码放入features/support/env.rb

Capybara.server do |app, port|
 require 'rack/handler/thin'
 Rack::Handler::Thin.run(app, :Port => port)
end

使用thin服务器从以下链接了解有关此解决方案的更多信息:

Solution of same problem using thin web server,并阅读此解决方案same solution by another one