如何将多个插件添加到 cypress/plugins/index.js 文件?

How do your add multiple plugins to cypress/plugins/index.js file?

我的 cypress/plugins/index.js 文件看起来像这样,我知道只能有一个 module.exports,但我正在努力将 cypressBrowserPermissionsPlugin 和 getConfigurationByFile 添加到一起

const cucumber = require('cypress-cucumber-preprocessor').default
const { cypressBrowserPermissionsPlugin } = require('cypress-browser-permissions')
const path = require('path')
const fs = require('fs')
require('@applitools/eyes-cypress')(module);

function getConfigurationByFile(file) {
  const pathToConfigFile = path.resolve('cypress/config', `${file}.json`);
  return fs.readJson(pathToConfigFile);
}
const file = config.env.configFile || 'qa';

module.exports = (on, config) => {
  on('file:preprocessor', cucumber())
  config = cypressBrowserPermissionsPlugin(on, config)
  return config
  // I want to also return the getConfigurationByFile(file) function
}

您需要将函数 getConfigurationByFile 包装到 task

function getConfigurationByFile(file) {
  const pathToConfigFile = path.resolve('cypress/config', `${file}.json`);
  return fs.readJson(pathToConfigFile);
}

module.exports = (on, config) => {
  on('file:preprocessor', cucumber());
  on('task', {
    getConfiguration (fileName) {
      const file = fileName || config.env.configFile || 'qa'; // filename defaults
      return getConfigurationByFile (file);
    }
  })
  config = cypressBrowserPermissionsPlugin(on, config);
  return config;
}

然后在测试中这样调用

cy.task('getConfiguration', 'my-config-file').then(config => {
   ...
});

或使用默认文件名

cy.task('getConfiguration').then(config => {
   ...
});