簡體   English   中英

如何從使用 Capybara 的 Cucumber Step 獲取整個頁面的響應 HTML?

[英]How do i get the response HTML of an entire Page from a Cucumber Step that uses Capybara?

在運行我的規范時,我遇到錯誤,其中 Capybara 無法根據標簽等找到元素。 然后我想我想要 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'

在這里和 Cucumber 一起工作很艱難..

1)為什么我得到上述

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

當您的頁面 HTML 不包含任何帶有 name、id 或 label 'user_login' inputtextarea元素時,會出現此錯誤。 如果您將launchy gem 添加到您的 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 並查看它的外觀。

希望能幫助到你! :)

它認為上述代碼的問題在於“頁面”在您結束該部分后才可用。

嘗試將您的“投入”放入“何時”或“然后”步驟

您可以按如下方式查詢頁面(假設您使用的是 RSpec)

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

希望這可以幫助

您還可以通過將page.body打印到標准輸出來查看 HTML 響應。

Given /^I am not an admin$/ do
  visit '/accounts/login'
  puts page.body
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM