简体   繁体   English

如何从使用 Capybara 的 Cucumber Step 获取整个页面的响应 HTML?

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

When running my specs i get errors where Capybara couldn't find elements based on their labels and such.在运行我的规范时,我遇到错误,其中 Capybara 无法根据标签等找到元素。 Then i thought i wanted the HTML, or somehow debug the errors.然后我想我想要 HTML,或者以某种方式调试错误。

This is the /features/merge.feature这是 /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

And this is: /feature/step_definitions/merge.rb - and it only contains "Given" statements!这是:/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

Then i get this error:然后我得到这个错误:

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'

Havin a rough time working with Cucumber here ..在这里和 Cucumber 一起工作很艰难..

1) Why do i get the above 1)为什么我得到上述

2) How do i debug it, i wanna see the HTML code! 2)我如何调试它,我想看看HTML代码!

This errors appears when your page HTML does not contain any input or textarea element with name, id or label 'user_login' .当您的页面 HTML 不包含任何带有 name、id 或 label 'user_login' inputtextarea元素时,会出现此错误。 If you add the launchy gem to your Gemfile you will be able to see how your page looks:如果您将launchy gem 添加到您的 Gemfile 中,您将能够看到您的页面的外观:

In your Gemfile :在您的Gemfile

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

Then, inside your feature/step_definitions/merge.rb file:然后,在您的feature/step_definitions/merge.rb文件中:

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

The save_and_open_page will open the current page in your browser, so then you can inspect the HTML and see what it looks like. save_and_open_page将在您的浏览器中打开当前页面,这样您就可以检查 HTML 并查看它的外观。

Hope it helps!希望能帮助到你! :) :)

It think the issue with the above code is that 'page' doesn't become available until after you end the section.它认为上述代码的问题在于“页面”在您结束该部分后才可用。

Try putting your 'puts' into the 'When'or 'Then' step尝试将您的“投入”放入“何时”或“然后”步骤

You can query page as follows (assuming you're using RSpec)您可以按如下方式查询页面(假设您使用的是 RSpec)

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

Hope this helps希望这可以帮助

You can also see the HTML response by printing page.body to stdout.您还可以通过将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