[英]How do I fail a step in a feature test with Capybara-Webkit when there is a JavaScript error on a page?
I am using Cucumber, Capybara and Capybara-webkit for testing different scenarios in my Ruby on Rails app. 我使用Cucumber,Capybara和Capybara-webkit在我的Ruby on Rails应用程序中测试不同的场景。
Is there a way I can detect any JavaScript error on the page while the scenarios are running and fail the test? 有没有办法在场景运行时检测到页面上的任何JavaScript错误并且测试失败? We are using these tests to make sure we don't break the functionality (including JavaScript) between changes as part of our automated test runs. 我们正在使用这些测试来确保我们不会在更改之间破坏功能(包括JavaScript),这是我们自动化测试运行的一部分。
I can see the failures in the test output, but it doesn't fail the test: 我可以在测试输出中看到失败,但它不会使测试失败:
http://127.0.0.1:54928/...|16|ReferenceError: Can't find variable: $
http://127.0.0.1:54928/...|16|ReferenceError: Can't find variable: $
...
Thanks! 谢谢!
Update: one way I have found is to have a step that tries to execute some JavaScript that would only be possible if previous errors had not happened. 更新:我发现的一种方法是尝试执行一些只有在以前的错误没有发生时才可能执行的JavaScript。 In that case, I would get an error like this: 在这种情况下,我会得到这样的错误:
Javascript failed to execute (Capybara::Driver::Webkit::WebkitInvalidResponseError)
./features/step_definitions/....rb:19:in `/^I should not see any JavaScript errors$/'
features/....feature:34:in `Then I should not see any JavaScript errors'
Is there a better way? 有没有更好的办法?
Yes, you can access all console messages with page.driver.console_messages
or only the error messages with page.driver.error_messages
. 是的,你可以访问所有控制台消息page.driver.console_messages
或仅与错误信息page.driver.error_messages
。
To test for no javascript errors I would suggest something along of: 为了测试没有javascript错误,我会建议以下内容:
Then /^I should see no Java\-Script errors$/ do
page.driver.error_messages.length.should == 0
end
Side note: capybara-webkit also includes a matcher :have_errors
to write a nice page.should_not have_errors
. 旁注:capybara-webkit还包括一个matcher :have_errors
来编写一个很好的page.should_not have_errors
。 Unfortunately this seems to be broken in the current version (at least for me; see also: https://github.com/thoughtbot/capybara-webkit/pull/201 ) 不幸的是,这似乎在当前版本中被破坏了(至少对我而言;另见: https : //github.com/thoughtbot/capybara-webkit/pull/201 )
capybara-webkit
appears to always have two bogus messages in our environment. capybara-webkit
似乎总是在我们的环境中有两条虚假的消息。 I have tried to be as specific as possible and filter these out, but the following is working for us to automatically fail upon detection of a javascript error with capybara-webkit: 我试图尽可能具体,并过滤掉这些,但以下是我们在使用capybara-webkit检测到javascript错误时自动失败:
AfterStep do
if webkit?
real_error_messages = []
page.driver.error_messages.each_with_index do |e, i|
# first two messages appear to be bogus, always.
if (e[:line_number] == 0) && (e[:source].eql? 'undefined') && (e[:message].eql? 'TypeError: \'null\' is not an object') && i <= 1
# discard message
else
real_error_messages << e
end
end
raise "Javascript errors: #{real_error_messages}" if real_error_messages.length > 0
end
end
Where webkit?
webkit?
在哪里webkit?
is: 是:
def webkit?()
[:webkit, :webkit_debug].include? Capybara.javascript_driver
end
Although being notified of JavaScript errors could occasionally be helpful, you should aim to test the actual JavaScript behaviour. 尽管收到JavaScript错误的通知有时可能会有所帮助,但您应该尝试测试实际的JavaScript行为。 If there's a JavaScript error this should be manifest itself as a failing scenario. 如果存在JavaScript错误,则应将其自身表现为失败方案。
The best way for you to run on each step 'show me the page' and check your web console. 您在每个步骤上运行的最佳方式是“向我显示页面”并检查您的Web控制台。 Then you can place console.log in the your code. 然后,您可以在您的代码中放置console.log。 where you making actions for the selected steps. 您为所选步骤执行操作的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.