Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

94
Visualizações
How can we read multiple test files from a directory and run one by one in cypress?

I want to integrate a function in a spec.ts file where it will read multiple xml data one by one from a directory and will repeat the tests as long as the directories length.

Is there any way to read multiple files or files name from a folder in Cypress or in JavaScript ?

almost 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use fs to read the content of a directory, and then we can use Cypress Lodash to iterate through that returned array.

import fs from 'fs';

const files = fs.readdirSync('/my/dir/');

describe('My Tests', () => {
  Cypress._.times(files.length, (index) => {
    it(`does some test for the file: ${files[index]}, () => {
      cy.readFile(files[index]).should(...);
    });
  });
});

almost 4 years ago · Santiago Trujillo Relatório

0

You will need to read the file list in cypress.config.js (for Cypress version 10 and above).

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

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {

      const xmlFolder = `${__dirname}/xml-files/`;
      const files = fs.readdirSync(xmlFolder)
        .map(file => `${xmlFolder}/${file}`)     // add folder path

      config.xmlFiles = files                    // put in config for test
      return config
    }
  }
})

In the test,

describe('Creating one test for each XML file', () => {

  Cypress.config('xmlFiles')
    .forEach(fileName => {

      it(`Testing ${fileName}`, () => {

        cy.readFile(fileName)
          .then(xml => {
            ...
          })
      });
    });
})

For Cypress version 9 and below use plugins.index.js:

module.exports = (on, config) => {
  on('before:run', (spec) => {

    const xmlFolder = `${__dirname}/xml-files/`;
    const files = fs.readdirSync(xmlFolder)
      .map(file => `${xmlFolder}/${file}`)     // add folder path

    config.xmlFiles = files                    // put in config for test
  })
  return config 
}
almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda