2012-04-16 114 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