简体   繁体   English

如何使用Intern在功能测试期间报告JavaScript错误?

[英]How to report JavaScript errors during functional tests using Intern?

How can I report JavaScript errors that occur during test execution using Intern ? 如何报告使用Intern执行测试期间发生的JavaScript错误? Basically, if there are any JavaScript errors on the page (even as part of things that aren't explicitly tested) I want to know. 基本上,我想知道页面上是否有JavaScript错误(甚至是未经过明确测试的部分错误)。

Background 背景

I'm just getting started with Intern and testing in general and I'm trying to test all major pages on my site in all browsers because I just changed all our JavaScript to load via require.js. 我刚开始使用Intern并进行了总体测试,并且尝试在所有浏览器中测试网站上的所有主要页面,因为我只是更改了所有JavaScript,以通过require.js进行加载。 While it looks good in Chrome, I've had issues with require.js and random browsers in the past so I wanted to automate everything. 尽管它在Chrome中看起来不错,但过去我在require.js和随机浏览器方面遇到了问题,因此我想使一切自动化。 The most likely issue that will arise is that some random JS will fail to execute due to asynchronous loading and load of an expected global. 最有可能出现的问题是,由于异步加载和预期全局变量的加载,某些随机JS将无法执行。 Since there are no current tests setup, I basically want to start by running a 'test' go to through all major pages and report any JavaScript errors. 由于没有当前的测试设置,我基本上想从所有主要页面上运行“测试”开始,并报告所有JavaScript错误。

In order to report uncaught errors, you need to hook the window.onerror method of the page. 为了报告未捕获的错误,您需要挂钩页面的window.onerror方法。 This is possible, but the page load will need to be finished before you add the hook, which means that any errors that occur before/during the page load (or that occur while the page unloads) simply cannot be caught and reported. 这是可能的,但是在添加挂钩之前必须完成页面加载,这意味着根本无法捕获和报告在页面加载之前/期间发生的任何错误(或在页面卸载期间发生的任何错误)。 It also means if you perform an action that moves to a new page (like a form submission), you will need to make sure you retrieve the list of errors before you perform the action that causes navigation, and reconfigure the window.onerror handler after you get to the new page. 这也意味着,如果执行的操作会移动到新页面(例如表单提交),则在执行导致导航的操作之前,需要确保检索到错误列表,并在之后重新配置window.onerror处理程序。您会转到新页面。

To perform such reporting with a functional test, your test would end up looking something like this: 为了通过功能测试执行此类报告,您的测试最终将看起来像这样:

return this.remote
  .get('http://example.com')
  .execute(function () {
    window.__internErrors__ = [];
    window.onerror = function () {
      __internErrors__.push(Array.prototype.slice.call(arguments, 0));
    };
  })
  // ... interact with the page ...
  .execute(function () {
    return window.__internErrors__;
  })
  .then(function (errors) {
    // read `errors` array to get list of errors
  });

Note that (as of August 2014) errors from window.onerror in all browsers except recent versions of Chrome provide only the message, script source, line number, and (sometimes) column number, so this information would only be useful to say “this action caused an error, go do it manually to get a stack trace”. 请注意,(截至2014年8月)所有浏览器中window.onerror错误(最新版本的Chrome除外)仅提供消息,脚本源,行号和(有时)列号,因此,此信息仅对说“ this操作导致错误,请手动执行以获取堆栈跟踪”。

During unit tests, Intern already tries to automatically catch any unhandled errors and treats them as fatal errors that halt the system (since you should never have code that generates this kind of unhandled error). 在单元测试期间,Intern已经尝试自动捕获任何未处理的错误,并将它们视为致命错误,从而使系统停止运行(因为您永远不应该拥有生成此类未处理错误的代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM