【问题标题】:How can we read multiple test files from a directory and run one by one in cypress?我们如何从一个目录中读取多个测试文件并在 cypress 中一个一个运行?
【发布时间】:2022-09-23 03:59:02
【问题描述】:

我想在一个 spec.ts 文件中集成一个函数,它将从一个目录中一个一个地读取多个 xml 数据,并且只要目录长度就会重复测试。

有没有办法从 Cypress 或 JavaScript 的文件夹中读取多个文件或文件名?

  • 请提供足够的代码,以便其他人可以更好地理解或重现该问题。

标签: javascript automation filesystems cypress cypress-testing-library


【解决方案1】:

您可以使用fsread the content of a directory,然后我们可以使用 Cypress Lodash 遍历返回的数组。

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(...);
    });
  });
});

【讨论】:

    【解决方案2】:

    您需要阅读cypress.config.js 中的文件列表

    const { defineConfig } = require('cypress')
    const fs = require("fs");
    
    module.exports = defineConfig({
      e2e: {
        setupNodeEvents(on, config) {
    
          const files = fs.readdirSync(`${__dirname}/xml-files/`)
            .map(file => `${__dirname}/xml-files/${file}/`)
          config.xmlFiles = files
          return config
        }
      }
    })
    

    在测试中,

    describe('Creating one test for each XML file', () => {
    
      Cypress.config('xmlFiles')
        .forEach(fileName => {
    
          it(`Testing ${fileName}`, () => {
    
            cy.readFile(fileName)
              .then(xml => {
                ...
              })
          });
        });
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-27
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2018-04-29
      • 2012-08-31
      相关资源
      最近更新 更多