Question

(Cypress) "list" argument must be an Array of Buffers

I'm trying to use the speakeasy library to authenticate with a Cypress Automation. This is the auth code generator code:

const token = speakeasy.totp({
    secret: '(my secret key)',
    encoding: 'base32' 
});

getAuthCode = () => {
    return token; 
}

module.exports = { getAuthCode };

This script works just fine, and lets me log in in the website I want to use, but when I import it with Cypress I receive the "list" argument must be an Array of Buffers error. I read a little bit about it and it seems that it's because of an issue with the code generator, but it works perfectly. By the way, here is the Cypress code, it's a simple code to test some functionalities so I still didn't do almost anything yet:

const { getAuthCode } = require('../support/authcodegen')
describe('Test', () => {    
    const emailInput = '[id = "email"]' 
    const passInput = '[id = "password"]' 
    const loginBtn = '[type = "submit"]'  
    it('Login Test', () => {     
        cy.on('uncaught:exception', (err, runnable) => {return false})          
        cy.visit('My Website');      
        cy.get(emailInput).type('My email');     
        cy.get(passInput).type('My Password');     
        cy.get(loginBtn).click();      
        const authCode = getAuthCode();     
        cy.log(authCode); 
    })
})

Do you find any defect in the code? I already tried to refactor the code a couple of times but the problems seems to be from Javascript, my other option would be to execute the console and get the result but that wouldn't be very professional...

The error traceback itself:

at Function.concat (webpack://automation/../../Library/Caches/Cypress/13.13.0/Cypress.app/Contents/Resources/app/node_modules/buffer/index.js:423)
    at new Hmac (webpack://automation/../../Library/Caches/Cypress/13.13.0/Cypress.app/Contents/Resources/app/node_modules/create-hmac/browser.js:27)
    at Object.createHmac (webpack://automation/../../Library/Caches/Cypress/13.13.0/Cypress.app/Contents/Resources/app/node_modules/create-hmac/browser.js:61)
    at Object.digest (webpack://automation/./node_modules/speakeasy/index.js:56)
    at Object.hotpGenerate (webpack://automation/./node_modules/speakeasy/index.js:95)
    at Object.totpGenerate (webpack://automation/./node_modules/speakeasy/index.js:293)
    at ./cypress/support/authcodegen.js (webpack://automation/./cypress/support/authcodegen.js:3:24)
    at __webpack_require__ (webpack://automation/webpack/bootstrap:19)
    at eval (webpack://automation/./cypress/e2e/cocoshome.cy.js:1:24)
    at eval (http://localhost:49752/__cypress/tests?p=cypress/e2e/cocoshome.cy.js:39694:3)
From previous event:
    at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] (http://localhost:49752/__cypress/runner/cypress_runner.js:3486:19)
    at Promise._then (http://localhost:49752/__cypress/runner/cypress_runner.js:1239:17)
    at Promise.then (http://localhost:49752/__cypress/runner/cypress_runner.js:1132:17)
    at runScriptsFromUrls (http://localhost:49752/__cypress/runner/cypress_runner.js:111340:136)
    at Object.runScripts (http://localhost:49752/__cypress/runner/cypress_runner.js:111381:12)
    at $Cypress.onSpecWindow (http://localhost:49752/__cypress/runner/cypress_runner.js:40896:67)
 2  38  2
1 Jan 1970

Solution

 4

Speakeasy is a plugin library for NodeJs, but you are trying to run it in the browser.

To make it work, move the code to a task in cypress.config.js.

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('task', {
        generateToken(secret) {
          const speakeasy = require('speakeasy');
          const token = speakeasy.totp({
            secret,
            encoding: 'base32' 
          })
          return token
        },
      })
    },
  },
})

An example test using the task:

it('generates token', () => {
  cy.task('generateToken', '(my secret key)')
    .then(token => { 
      Cypress.log({displayName: 'token', message: token})
     })
    .should('have.length', 6)
})

enter image description here

2024-07-22
Eddy Gilmour