当页面上存在 JavaScript 错误时,如何使用 Capybara-Webkit 进行功能测试中的某个步骤失败?
我正在使用 Cucumber、Capybara 和 Capybara-webkit 来测试 Ruby on Rails 应用程序中的不同场景。
有没有一种方法可以在场景运行时检测页面上的任何 JavaScript 错误并导致测试失败?我们使用这些测试来确保在自动化测试运行过程中不会破坏更改之间的功能(包括 JavaScript)。
我可以在测试输出中看到失败,但它不会使测试失败:
http://127.0.0.1:54928/...|16|ReferenceError: Can't find variable: $
http://127.0.0.1:54928/...|16|ReferenceError: Can't find variable: $
...
谢谢!
更新:我发现的一种方法是尝试执行一些 JavaScript,只有在以前的错误没有发生的情况下才可能执行。在这种情况下,我会得到这样的错误:
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'
有更好的方法吗?
I am using Cucumber, Capybara and Capybara-webkit for testing different scenarios in my Ruby on Rails app.
Is there a way I can detect any JavaScript error on the page while the scenarios are running and fail the test? 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.
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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以使用
page.driver.console_messages
访问所有控制台消息,也可以使用page.driver.error_messages
仅访问错误消息。为了测试没有 javascript 错误,我建议执行以下操作:
旁注:capybara-webkit 还包含一个匹配器
:have_errors
来编写一个漂亮的page.should_not have_errors
。不幸的是,这在当前版本中似乎被破坏了(至少对我来说;另请参阅: https: //github.com/thoughtbot/capybara-webkit/pull/201)Yes, you can access all console messages with
page.driver.console_messages
or only the error messages withpage.driver.error_messages
.To test for no javascript errors I would suggest something along of:
Side note: capybara-webkit also includes a matcher
:have_errors
to write a nicepage.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)尽管收到 JavaScript 错误通知有时可能会有所帮助,但您应该致力于测试实际的 JavaScript 行为。如果存在 JavaScript 错误,这应该表现为失败场景。
Although being notified of JavaScript errors could occasionally be helpful, you should aim to test the actual JavaScript behaviour. If there's a JavaScript error this should be manifest itself as a failing scenario.
capybara-webkit
在我们的环境中似乎总是有两条虚假消息。我试图尽可能具体并过滤掉这些内容,但是以下内容可以让我们在使用 capybara-webkit 检测到 javascript 错误时自动失败:webkit?
是:完整要点在这里
capybara-webkit
appears to always have two bogus messages in our environment. 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:Where
webkit?
is:Full gist here
运行每个步骤的最佳方式是“显示页面”并检查您的网络控制台。然后您可以将 console.log 放入您的代码中。您可以在其中为选定的步骤执行操作。
The best way for you to run on each step 'show me the page' and check your web console. Then you can place console.log in the your code. where you making actions for the selected steps.