You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Cypress扫描二维码遇TypeError错误,寻求解决指导

解决Cypress中@nuintun/qrcode扫描二维码报错问题

错误原因

当前导入方式不正确,导致qrcode对象未被正确加载,调用scan方法时触发undefined错误。

修复步骤

  • 修正导入语句
    @nuintun/qrcode的正确导入方式通常为默认导入,替换原导入代码:

    import Qrcode from "@nuintun/qrcode";
    

    若使用命名导出,需确认库的实际导出成员名(注意大小写差异)。

  • 调整扫描逻辑代码
    基于修正后的导入,修改调用scan方法的代码:

    cy.get('img[alt="QR Code"]').then(async (image) => {
      const src = image.prop('src');
      console.log('Image source:', src);
      const decoded = await Qrcode.scan(src);
      const totp = OTPAuth.URI.parse(decoded.data);
      const generateOTP = totp.generate();
      cy.get('input#code').type(generateOTP);
    });
    
  • 验证库的安装状态
    确保已正确安装该库,若未安装或版本兼容有问题,执行以下命令:

    npm install @nuintun/qrcode --save-dev
    

    必要时更新到最新版本。

  • 优化Cypress异步处理
    也可改用Cypress链式then处理异步逻辑,避免上下文冲突:

    cy.get('img[alt="QR Code"]').then((image) => {
      const src = image.prop('src');
      console.log('Image source:', src);
      return Qrcode.scan(src);
    }).then((decoded) => {
      const totp = OTPAuth.URI.parse(decoded.data);
      const generateOTP = totp.generate();
      cy.get('input#code').type(generateOTP);
    });
    

内容的提问来源于stack exchange,提问作者Pedro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 01:11:02