我有:
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 :(得分:8)
我最喜欢调试黄瓜步骤的方法是调用binding.pry
。
确保pry
gem包含在:development, test
的gem文件中,然后将binding.pry
调用放在引发错误的行之前。然后,您应该能够使用ls
命令对内部环境进行内省,如果您可以找到运行的capybara会话,您可以(如果capybara会话存储为名为page的变量)page.html
和{{1看看什么是可见的。
希望有所帮助。
答案 1 :(得分:1)
添加以下内容作为features / support / debugging.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
通过设置环境变量,可以使Cucumber使用各种调试工具,您可以通过设置多个环境变量来组合它们。
答案 2 :(得分:0)
您的测试失败,因为您必须导航到某个页面(打开它)。您可以使用capybara的内置方法来执行此操作:
visit path_to(url)
您也可以使用标准的ruby调试器进行调试。请参阅指南this rails guide以获取更多信息。
答案 3 :(得分:0)
根据我的经验:
p / s:如果你遵循我的指示,但它不起作用。请给我你的功能和步骤定义。我会尽力帮助你