如何在运行Cypress框架时获取传递的标签名称

huangapple go评论111阅读模式
英文:

How to get the Tag name passed while running Cypress Framework

问题

I'll provide the translation for the code-related content:

我正在使用 Cypress 12.4TypeScript 4.9  Cucumbercucumber-pre-processor 15框架
我有一些测试用例标记为 @Sanity还有一些测试用例标记为 @Regression
以下是我的 package.json 脚本

```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 程序的 process.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)],
        })
      );
      // 确保返回配置对象,因为它可能已被插件修改。
      return config;
    },    
  },
});

在这里问了一下,我需要捕获哪个标记(@Sanity/@Regression),它是在 package.json 脚本中执行的。
在我的配置文件中需要做出任何更改吗?process.argv 代码中需要修改什么吗?


<details>
<summary>英文:</summary>

I am working on **Cypress 12.4,TypeScript -4.9,Cucumber(cucumber-pre-processor -15) framework**.
I have few Test cases marked as @Sanity and few Test cases marked as @Regression 
Below is my package.json script

"cy:smoke": "npx cypress run -- --env tags=&quot;@Sanity&quot;
"cy:regression": "npx cypress run -- --env tags=&quot;@Regression&quot;

When I run cy:smoke, all the test case with tag @Sanity get triggered and When I run cy:regression, all the test case with tag @Regression get triggered (this is done through CI/CD pipeline)
So I **need to capture this tag(Here I have to determine  Sanity or Regression which one has been triggered)** in a variable which is been triggered so that I can perform action I want to.
Since this is based on node.js and the script is triggered as command line argument.
I tired to use **node.js program process.argv Property** as below 

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

Adding my cypress.config.ts here 

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;
},
},
});

Ask here need to capture the tag (@Sanity/@Regression)which pacakge.json script is executed.
Is anything I need to change in my config file?,anything to modify in process.argv code ?

</details>


# 答案1
**得分**: 3

Since you have correctly set the tag as a Cypress env-var `--env tags=\&quot;@Sanity\&quot;`, you can just access it inside the spec with `Cypress.env(&#39;tags&#39;)`

```js
const tag = Cypress.env(&#39;tags&#39;)

it(&#39;tests with tag passed in&#39;, () =&gt; {
  console.log(tag); // **@Sanity**
  console.log(&quot;number of arguments is &quot;+process.argv.length)  // 1
  ...
})
英文:

Since you have correctly set the tag as a Cypress env-var --env tags=\&quot;@Sanity\&quot;, you can just access it inside the spec with Cypress.env(&#39;tags&#39;)

const tag = Cypress.env(&#39;tags&#39;)

it(&#39;tests with tag passed in&#39;, () =&gt; {
  console.log(tag); // **@Sanity**
  console.log(&quot;number of arguments is &quot;+process.argv.length)  // 1
  ...
})

答案2

得分: -1

As a possible solution you can pass your tag to Cypress as an environment variable, capture it in your config file and then re-use it in your tests.

Update package.json file

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

In your config file, capture the tags variable and set it as the configuration property value:

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 || ""
});

Right now you can access the tags variable from your tests. Good luck!

英文:

As a possible solution you can pass your tag to Cypress as an environment variable, capture it in your config file and then re-use it in your tests.

Update package.json file

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

In you config file capture tags variable and set it as the configuration property value:

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

Right now you can access tags variable from your tests. Good luck!

huangapple
  • 本文由 发表于 2023年5月25日 00:13:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定