8

我有:

When /^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/ do |link, selector|  
  with_scope(selector) do
   click_link(link)
  end
end

我打电话给:

Background:
  Given I am an existing admin user
  When I follow "CLIENTS"

我的 HTML 是这样的:

<a class="active" href="/companies"><h2>CLIENTS</h2></a>

我不断收到这个错误:

.F-.F--U-----U

(::) failed steps (::)

no link with title, id or text 'CLIENTS' found (Capybara::ElementNotFound)
(eval):2:in `click_link'
./features/step_definitions/web_steps.rb:54:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:53:in `/^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/'
features/client_add.feature:8:in `When I follow "CLIENTS"'

我尝试了一些事情:

When I follow "<h2>CLIENTS</h2>"

甚至尝试了save_and_open_page哪个应该打开浏览器并仍然得到相同的结果:

Given /^I am an existing admin user$/ do
  role_user = FactoryGirl.create(:role_user)
  admin_user = role_user.user
  sign_in(admin_user)
  save_and_open_page
end

有没有办法打印 HTML 或找出我的测试失败的原因?

4

4 回答 4

10

我最喜欢调试黄瓜步骤的方法是调用binding.pry.

确保prygem 包含在您的 gem 文件中:development, test,然后将binding.pry调用放在引发错误的行之前。然后,您应该能够使用该ls命令自省环境,如果您可以找到正在运行的 capybara 会话(如果 capybara 会话存储为名为 page 的变量)page.htmlpage.text查看可见的内容。

希望有帮助。

于 2012-04-16T15:23:25.143 回答
2

添加以下内容作为 features/support/debugging.rb 的内容有助于调试失败的步骤:

# `LAUNCHY=1 cucumber` to open page on failure
After do |scenario|
  save_and_open_page if scenario.failed? && ENV['LAUNCHY']
end

# `FAST=1 cucumber` to stop on first failure
After do |scenario|
  Cucumber.wants_to_quit = ENV['FAST'] && scenario.failed?
end

# `DEBUG=1 cucumber` to drop into debugger on failure
After do |scenario|
  next unless ENV['DEBUG'] && scenario.failed?
  puts "Debugging scenario: #{scenario.title}"
  if respond_to? :debugger
    debugger
  elsif binding.respond_to? :pry
    binding.pry
  else
    puts "Can't find debugger or pry to debug"
  end 
end

# `STEP=1 cucumber` to pause after each step
AfterStep do |scenario|
  next unless ENV['STEP']
  unless defined?(@counter)
    puts "Stepping through #{scenario.title}"
    @counter = 0
  end
  @counter += 1
  print "At step ##{@counter} of #{scenario.steps.count}. Press Return to"\
        ' execute...'
  STDIN.getc
end

通过设置一个环境变量,可以让Cucumber使用各种调试工具,也可以通过设置多个环境变量来组合它们。

于 2015-06-17T03:52:24.127 回答
0

您的测试失败,因为您必须导航到一个页面(打开它)。您可以使用 capybara 的内置方法来做到这一点:

visit path_to(url)

您也可以使用标准的 ruby​​ 调试器进行调试。请参阅此导轨指南以获取更多信息。

于 2012-04-16T17:31:19.383 回答
0

根据我的经验:

  • 首先:您应该在 capybara 中使用“save_and_open_page”方法来检查该页面上的内容并考虑并查看它是否呈现到您期望的正确页面。(我认为页面有“客户”链接尚未显示)
  • 第二:您更改重定向到页面的路由路径具有“客户”链接

希望它可以帮助你。

p / s:如果您按照我的指示进行操作,但它不起作用。请给我您的功能和步骤定义。我会尽力帮助你

于 2014-05-20T02:59:08.173 回答