2013-02-25 22 views
3

我想打印坐落在一個跨度和看起來像這樣的錯誤消息的內容:如何在Cucumber/Capybara中打印<span>的內容?

<span>Es exisitiert bereits ein Account mit dieser E-Mail Adresse. Wenn Sie sich sicher sind, dass dies Ihre Adresse ist, dann klicken Sie <a href="https://link-removed.com">hier</a> um das Passwort und den Zugang zu Ihrem Account zu erhalten.</span> 

在我的步驟定義,我嘗試以下

print page.find('.error-msg span') 

然而這,實際上並不奏效。谷歌搜索了一個小時後,我找不到任何有用的東西,這也可能是因爲我對這一切都非常陌生。

我基本上想調試,所以我想看看實際的內容是什麼。這對今後的努力也有幫助。

回答

0

您應該使用的RSpec匹配器要麼通過或失敗的bug。這樣做會在頁面上打印出有用的信息。

Given I visit a page 
Then I should see "foo" in ".error-msg span" 

Then /^I should see "([^\"]*)" in "([^\"]*)"$/ do |text, css| 
    page.should have_selector(tag, text: text) 
end 

如果您使用此,有一個「.error-MSG跨度」元素在頁面上,如果文本不匹配你所期望的,該錯誤消息顯示豚會告訴你什麼是內選擇。例如:

 expected to find css ".error-msg span" with text "foo" but there were no matches. Also found "Bar", which matched the selector but not all filte 

rs。 (水豚:: ExpectationNotMet)

翻譯:裏面「.error-MSG跨度」後面的文字是‘巴’而不是‘富’

0

你可以利用這一點,因爲所有你需要的是驗證在文本跨度:

Then /^I should see (.+) as content in xpath "([^"]*)"$/ do |content, xpath| 
    assert page.find(:xpath, xpath + "[contains(string(), '" + content +"')]") 
end 

給你span.It的XPath的用繩子只能

0

你可以試試這個方法。文件elements.rb添加到您的支持文件夾,這個在它:

def text(type=nil) 
    type ||= :all unless Capybara.ignore_hidden_elements or Capybara.visible_text_only 
    synchronize do 
    if type == :all 
    base.all_text 
    else 
     base.visible_text 
    end 
end 
end 

然後寫你的步驟定義如下(舉例):

within(:id, "") do 
    page.should have_content("") 
     puts text 
    end 
end 

您可以再補充puts text到年底在將來輸出元素文本的任何步驟定義