2013-03-16 19 views
0

運行我的規格時,我得到錯誤,其中水豚無法找到基於他們的標籤等元素。然後,我想我想要的HTML,或以某種方式調試錯誤。如何從使用水豚的黃瓜步驟中獲得整個頁面的響應HTML?

這是/features/merge.feature

#Just a test! 
Scenario: Cannot merge articles when not admin 
    Given I am not an admin 
    When I am on the edit article page 
    Then I should not see the "merge articles" form 

這就是:/feature/step_definitions/merge.rb - 它僅包含 「給定」 語句!

Given /am not an admin$/ do 
    visit '/accounts/login' 
    puts "TEZT" 
    puts page.native 
    puts page.native.text 
    fill_in 'user_login', :with => 'editor_rico' 
    fill_in 'user_password', :with => 'a' 
    click_button 'Login' 
end 

Given /^I am an admin$/ do 
    visit '/accounts/login' 
    fill_in 'user_login', :with => 'admin_rico' 
    fill_in 'user_password', :with => 'a' 
    click_button 'Login' 
end 

然後我得到這個錯誤:

Scenario: Cannot merge articles when not admin  
# features/article_merging.feature:20 
Given I am not an admin     
#features/step_definitions/article_steps.rb:39 
cannot fill in, no text field, text area or password field with id, name, or label 
'user_login' found (Capybara::ElementNotFound) 
(eval):2:in `fill_in' 
./features/step_definitions/article_steps.rb:44:in `/am not an admin$/' 
features/article_merging.feature:21:in `Given I am not an admin' 

就吃一個粗略的時間與黃瓜在這裏工作..

1)爲什麼我得到上述

2)如何做我調試它,我想看看HTML代碼!

回答

3

當您的頁面HTML不包含任何inputtextarea元素(名稱,ID或標記'user_login')時,將顯示此錯誤。如果添加了launchy寶石到你的Gemfile中,你將能夠看到你的頁面的外觀:

在你Gemfile

group :test do 
    gem 'launchy' 
    gem 'capybara' 
    ... 
end 

然後,你feature/step_definitions/merge.rb文件中:

Given /^I am not an admin$/ do 
    visit '/accounts/login' 
    save_and_open_page 
end 

save_and_open_page將在您的瀏覽器中打開當前頁面,然後您可以檢查HTML並查看它的外觀。

希望它有幫助! :)

+0

謝謝@mrcasals - 這工作完美。我也讀過你實際上可以做的事情:放入page.body。 – imbageek 2013-03-17 11:15:17

+1

不錯的提示呢!但是使用我的例子的意義在於,如果你預編譯你的應用程序資產,你將會看到你的步驟正確地再現了什麼(也就是說,用HTML和CSS),這總是很好的:)但是,如果你急着要把page.body'放進去也可以工作:) – mrcasals 2013-03-20 11:42:30

1

它認爲上述代碼的問題是'頁面'直到結束該部分後纔會變得可用。

嘗試把你的「看跌期權」到「When'or‘然後’一步

可以按如下(假設你使用的RSpec)

page.should have_content 'foo' 
page.should have_selector 'h1', text: => 'Hello dave' 

希望這有助於

查詢頁面
+0

我編輯了問題@muttonlamb請再看一遍,因爲我不明白這個答案。我正在使用Cucumber和工作步驟定義。我在文檔中讀到*可能*是某種稱爲Page的東西。我不確定。 – imbageek 2013-03-16 16:18:50

+0

似乎水豚沒有找到頁面上的元素,你有沒有嘗試過在瀏覽器中正常打開頁面?然後,您可以檢查HTML並根據您的字段命名是否正確。 下面關於使用launchy的建議也是一個很好的建議 – muttonlamb 2013-03-17 06:55:04

+0

這也有助於應該斷言。謝謝! – imbageek 2013-03-17 11:16:12