2012-04-16 115 views
4

我:如何调试黄瓜测试?

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或某种方式弄清楚为什么我的测试失败了?

+0

使用capybara的save_and_open_page,如http://berk.es/2013/01/08/make-cucumber-open-the-browser-with-the-current-page/ – deepak 2014-04-15 09:22:55

+0

上给出的也使用pry_remote(https:/ /github.com/Mon-Ouie/pry-remote)在功能测试中进行调试。可能需要睡觉,以便在规范中不发生超时 – deepak 2014-04-15 09:26:48

回答

7

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

确保pry宝石已包含在您的用于:development, test的宝石文件中,然后将binding.pry调用放在引发错误的行之前。然后,您应该能够通过ls命令反思环境,并且如果您可以找到正在运行的水豚会话,则可以(如果将水豚会话存储为名为page的变量)page.htmlpage.text以查看可见的内容。

希望有所帮助。

0

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

visit path_to(url) 

您也可以调试使用标准的Ruby调试。请参阅指南this rails guide以获取更多信息。

0

我的经验:

  • 首先:你应该使用“save_and_open_page”法水豚检查什么是网页上,并考虑,并期待它呈现给你想到还是不正确的页面。 (我认为页面有“客户”链接还没有显示)
  • 二:您更改重定向到页面有“顾客”链接

希望它可以帮助你的路由路径。

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

1

添加以下内容作为功能/支持/调试。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 

通过设置环境变量,你可能会导致黄瓜使用各种调试工具,你可以通过设置多个环境变量将它们结合起来。

+0

这很好,谢谢。您可以直接在命令行上设置环境变量。例如 bin/cucumber features/mytest.feature LAUNCHY = 1' 或将它们设置为整个终端会话与 'export LAUNCHY = 1' – CodeKid 2016-09-21 20:33:25