简体   繁体   English

当页面上出现JavaScript错误时,如何使用Capybara-Webkit在功能测试中失败?

[英]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

Full gist here 这里充分要点

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.

相关问题 使用Capybara-webkit驱动程序时,Omniauth测试模拟不起作用 - Omniauth Test Mock Doesn't Work When Using Capybara-webkit Driver 当使用capybara-webkit和一个使用async = true加载外部脚本的页面时,Rspec会定期挂起 - Rspec periodically hangs when using capybara-webkit with a page that loads an external script with async = true 在capybara-webkit中禁用ClickFailed屏幕截图 - Disabling ClickFailed screenshots in capybara-webkit 在Rails中使用capybara-webkit模拟按​​键 - Simulating a keypress using capybara-webkit in rails 当页面上出现javascript错误时,失败的behat @javascript测试 - Fail a behat @javascript test when there is a javascript error on the page 检查RSpec + capybara-webkit是否存在JS框 - Check if JS box exists with RSpec + capybara-webkit capybara-webkit中的未定义方法`invalid_element_errors&#39; - undefined method `invalid_element_errors' in capybara-webkit $ .ajax删除请求不在capybara-webkit中发送数据参数 - $.ajax delete request not sending data parameters in capybara-webkit 如何在Capybara中测试页面是否*没有重新加载(JavaScript onClick intercept有效)? - How can I test in Capybara that a page has *not* reloaded (JavaScript onClick intercept has worked)? 当angularjs抛出错误时,如何使TestCafe测试失败? - How do I make a TestCafe test fail when angularjs throws an error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM