在TestCafe中运行测试期间累积所有JS警告和错误 [英] Accumulate all JS warnings and errors during test runs in TestCafe

查看:256
本文介绍了在TestCafe中运行测试期间累积所有JS警告和错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在测试运行期间从浏览器控制台访问所有JS警告和错误.禁用"-e"(跳过JS错误)标志后,测试将在第一个错误处停止,因此很明显它正在寻找它们.启用此标志后,我希望能够查看在测试运行期间触发了哪些错误(最好是警告).

I would like to be able to access all JS warnings and errors from the Browser Console during test runs. With the "-e" (skip JS errors) flag disabled, the test stops at the first error, so clearly it is looking for them. With this flag enabled, I would like to be able to see which errors (and ideally warnings) fired during test runs.

我尝试使用ClientFunction和window.onerror方法.我还尝试了-r'reports'标志-我只看到TestCafe错误,而不是被测页面中的JS错误.我已经尝试从 https:访问控制台消息://devexpress.github.io/testcafe/documentation/test-api/accessing-console-messages.html ,但这只会提供console.log等抛出的消息(请注意,此方法仅返回通过console.error,console.warn,console.log和console.info方法.浏览器输出的消息(如页面上发生未处理的异常时)将不会返回."

I've tried using the ClientFunction and window.onerror methods. I've also tried the -r 'reports' flag -- I only see TestCafe errors, not JS errors from the page under test. I've tried "Accessing Console Messages" from https://devexpress.github.io/testcafe/documentation/test-api/accessing-console-messages.html but this only gives messaging thrown by console.log, etc ("Note that this method returns only messages posted via the console.error, console.warn, console.log and console.info methods. Messages output by the browser (like when an unhandled exception occurs on the page) will not be returned.")


    const installErrorHandler = ClientFunction(() => {
      window.onerror = error => {
          console.log("ERROR::::::");
          console.log(error);
      };
    });

在Clojure空间中,使用Etaoin Webdriver实现(在Chrome或Phantom.js上)时,随时只需执行

Over in the Clojure space, when using the Etaoin webdriver implementation (on Chrome or Phantom.js), at any point simply performing a


    (get-logs driver)

返回


    {:level :warning,
     :message "1,2,3,4  anonymous (:1)",
     :timestamp 1511449388366,
     :source nil,
     :datetime #inst "2017-11-23T15:03:08.366-00:00"}
 ....
 ....

包括任何未处理的异常".

Including any 'unhandled exceptions'.

我不能在TestCafe中这样做吗?

Can I not do this in TestCafe?

推荐答案

您的带有window.onerror的示例可能无法正常工作,因为它会在以后执行该错误.我建议您将错误处理程序提取到单独的* .js文件中,并使用新的testcafe功能将其注入:

Probably your example with window.onerror does not work because it executes later that error occurs. I suggest you extract the error handler into a separate *.js file and inject it using the new testcafe feature: Inject Scripts into Tested Pages.

看下面的例子:

要注入的脚本(log-errors.js):

script to inject (log-errors.js):

window.unhandledErrors = [];
window.onerror = (message, source, lineno, colno, error) => {
    window.unhandledErrors.push({ message, source, lineno, colno, error });
}

测试:

import { Selector } from 'testcafe';

fixture('Log errors')
    .page('http://example.com')
    .afterEach(async t => {
        const errors = await t.eval(() => window.unhandledErrors);
        console.log(errors);
    });

test('test', async t => {
    //...
});

通过以下命令运行测试:

Run tests by the following command:

testcafe chrome test.js -e --cs=./scripts/log-errors.js

请注意,脚本注入功能自1.4.0版本开始可用.

Note that Script Injection feature is available since 1.4.0 version.

这篇关于在TestCafe中运行测试期间累积所有JS警告和错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆