检查是否已将错误写入控制台 [英] Check if an error has been written to the console

查看:92
本文介绍了检查是否已将错误写入控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来运行赛普拉斯单元测试时检查是否向控制台写入了错误。

I'm trying to find a way to check if an error has been written to the console when running a cypress unit test.

我知道如何记录某些内容到控制台

I know how to log something to the console

cy.log('log this to the console');

而不是如何检查是否已写入错误。

but not how to check if an error has been written to it.

关于如何从(浏览器)控制台日志中读取错误的任何建议?

any suggestions how to read errors from the (browser) console log?

注意:可能不是测试的智能方式,但有时我使用的js库会投诉并将错误写入浏览器日志。

note: probably not the "smart" way to test but sometimes my js libraries which I use would "complain" and write the errors to the browser log. this is to simplify testing.

推荐答案


编辑:以下不会在无头模式下直接登录到终端,但是它仍然无法通过AUT的 console.error 的测试,并间接显示错误消息,即使在无头终端,可能就是您想要的。

the following does not directly log to terminal when in headless mode, but it nonetheless fails the test on AUT's console.error and displays the error message indirectly, even in the headless terminal, which may be what you want.

我不确定您的意思是什么,但让我们仔细看看可以在赛普拉斯上记录输出的地方,以及如何处理几种情况。

I'm not sure exactly what you mean, but let's go through all the places where an output can be logged in cypress, and how to handle several cases.


  1. 登录到命令日志 >,则使用:

// from inside your test
cy.log('foo');


  • 要登录 devTools控制台

    // from inside your test
    console.log('bar');
    


  • 要登录终端,您需要从内部登录赛普拉斯的节点过程:

  • To log into terminal, you need to log from within the Cypress' node process:

    // from within e.g. your plugin/index.js file
    console.log('baz');
    




  • 如何将AUT的错误记录到Terminal,Command登录并通过测试



    (请注意,此处的AUT代表被测应用,表示您的应用)。

    How to log AUT's errors to Terminal, Command Log, and fail the test

    (note, AUT here stands for Application under test, meaning your application).

    我也在使用 ansicolor 包,使错误在终端中变为红色,这是可选的。

    I'm also using ansicolor package to make the error red-colored in the terminal, which is optional.

    // plugins/index.js
    const ansi = require(`ansicolor`);
    module.exports = ( on ) => {
        on(`task`, {
            error ( message ) {
                // write the error in red color
                console.error( ansi.red(message) );
                // play `beep` sound for extra purchase
                process.stdout.write(`\u0007`);
                return null;
            }
        });
    };
    

    注意:使用内部 cy.now()可以解决Cypress抛出倾向的命令。Cypress检测到您在国际海事组织(IMO)不应这样做的情况下返回了诺言

    Note: using internal cy.now() command to work around Cypress' tendency to throw Cypress detected that you returned a promise when it (IMO) shouldn't.

    (改编自 https://github.com/cypress- io / cypress / issues / 300#issuecomment-438176246

    // support/index.js or your test file
    Cypress.on(`window:before:load`, win => {
    
        cy.stub( win.console, `error`, msg => {
            // log to Terminal
            cy.now(`task`, `error`, msg );
            // log to Command Log & fail the test
            throw new Error( msg );
        });
    });
    

    这篇关于检查是否已将错误写入控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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