NodeJS 如何在运行Cypress Framework时获取传递的Tag名称

sh7euo9m  于 2023-05-28  发布在  Node.js
关注(0)|答案(2)|浏览(167)

我正在做Cypress 12.4,TypeScript-4.9,Cucumber(cucumber-pre-processor -15)framework。我有几个测试用例标记为@Sanity和几个测试用例标记为@Regression下面是我的package.json脚本

"cy:smoke": "npx cypress run -- --env tags=\"@Sanity\"
 "cy:regression": "npx cypress run -- --env tags=\"@Regression\"

当我运行cy:smoke时,所有带有标签@Sanity的测试用例都会被触发,当我运行cy:regression时,所有带有标签@Regression的测试用例都会被触发(这是通过CI/CD管道完成的)所以我需要在一个被触发的变量中捕获这个标签(这里我必须确定Sanity或Regression中哪个被触发),这样我就可以执行我想要的操作。因为这是基于node.js的,脚本是作为命令行参数触发的。我尝试使用node.js程序进程.argv属性如下

const process = require('process');
console.log(process.argv); //null
console.log("number of arguments is "+process.argv.length); //0

在这里添加我的cypress.config.ts

import { defineConfig } from "cypress";
import createBundler from "@bahmutov/cypress-esbuild-preprocessor";
import { addCucumberPreprocessorPlugin } from "@badeball/cypress-cucumber-preprocessor";
import createEsbuildPlugin from "@badeball/cypress-cucumber-preprocessor/esbuild";

export default defineConfig({
  e2e: {
    specPattern: '**/*.feature',
    baseUrl: "",
    watchForFileChanges:true,
    experimentalWebKitSupport:true,  
    async setupNodeEvents(on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions): Promise<Cypress.PluginConfigOptions> {
      await addCucumberPreprocessorPlugin(on, config);
      on(
        "file:preprocessor",
        createBundler({
          plugins: [createEsbuildPlugin(config)],
        })
      );
      // Make sure to return the config object as it might have been modified by the plugin.
      return config;
    },    
  },
});

在这里询问需要捕获执行pacakge.json脚本的标记(@Sanity/@Regression)。我需要在配置文件中更改什么吗?,有什么要修改的吗?

qq24tv8q

qq24tv8q1#

由于您已经将标记正确地设置为Cypress env-var --env tags=\"@Sanity\",因此您可以在规范中使用Cypress.env('tags')访问它

const tag = Cypress.env('tags')

it('tests with tag passed in', () => {
  console.log(tag); // **@Sanity**
  console.log("number of arguments is "+process.argv.length)  // 1
  ...
})
snz8szmq

snz8szmq2#

作为一种可能的解决方案,您可以将标记作为环境变量传递给Cypress,在配置文件中捕获它,然后在测试中重用它。

更新package.json文件

"cy:smoke": "npx cypress run --env TAGS=\"@Sanity\"",
   "cy:regression": "npx cypress run --env TAGS=\"@Regression\""

在您的配置文件中捕获tags变量,并将其设置为配置属性值:

const { defineConfig } = require('cypress');
const tag = Cypress.env("TAGS");
//
export default defineConfig({
  e2e: {
    specPattern: "**/*.feature",
    baseUrl: "",
    watchForFileChanges: true,
    experimentalWebKitSupport: true,
    async setupNodeEvents(
      on: Cypress.PluginEvents,
      config: Cypress.PluginConfigOptions
    ): Promise<Cypress.PluginConfigOptions> {
      await addCucumberPreprocessorPlugin(on, config);
      on(
        "file:preprocessor",
        createBundler({
          plugins: [createEsbuildPlugin(config)],
        })
      );
      // Make sure to return the config object as it might have been modified by the plugin.
      return config;
    },
  },
  tags: tag || ""
});

**现在您可以从测试中访问tags变量。祝你好运!

相关问题