Puppeteer console.log - 如何查看 JSHandle@object 内部? [英] Puppeteer console.log - How to look inside JSHandle@object?

查看:42
本文介绍了Puppeteer console.log - 如何查看 JSHandle@object 内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 React/Redux 应用程序,我正在使用 Puppeteer 进行测试.根据文档,我使用以下代码来显示控制台输出:

I have a React/Redux app that I am testing using Puppeteer. Based on the documentation, I am using the following code to show the console outputs:

page.on('console', msg => {
    for(let i = 0; i < msg.args().length; ++i) {
        let text = msg.args()[i];
        console.log(`${i}: ${text}`);
    }
});

但是,当 redux-logger 将对象记录到控制台(prevState、nextState)时,Puppeeter 会在我的控制台输出中显示 JSHandle@object.如何查看该对象内部的键和属性?

However, when the redux-logger logs an object to console (prevState, nextState), Puppeeter shows JSHandle@object in my console outputs instead. How do I see the keys and properties inside this object?

推荐答案

如果你放置了 consoleMessage.args() 直接在 console.log() 不将它们包含在 模板文字,您可以打印 JSHandles 表示传递给 console.log() 在页面上下文中:

If you place the consoleMessage.args() directly inside console.log() without enclosing them in template literals, you can print the keys and values of the JSHandles that represent each argument passed to console.log() in the page context:

page.on('console', msg => {
  for (let i = 0; i < msg.args().length; i++) {
    console.log(msg.args()[i]);
  }
});

结果的开头看起来像这样:

The beginning of the result will look something like this:

JSHandle {
  _context:
   ExecutionContext {
     _client:
      CDPSession {
        domain: null,
        _events: [Object],
        ...

这篇关于Puppeteer console.log - 如何查看 JSHandle@object 内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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