英文:
How to get the Tag name passed while running Cypress Framework
问题
I'll provide the translation for the code-related content:
我正在使用 Cypress 12.4、TypeScript 4.9 和 Cucumber(cucumber-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="@Sanity"
"cy:regression": "npx cypress run -- --env tags="@Regression"
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=\"@Sanity\"`, you can just access it inside the spec with `Cypress.env('tags')`
```js
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
...
})
英文:
Since you have correctly set the tag as a Cypress env-var --env tags=\"@Sanity\"
, you can just access it inside the spec with 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
...
})
答案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
"cy:smoke": "npx cypress run --env TAGS=\"@Sanity\"",
"cy:regression": "npx cypress run --env TAGS=\"@Regression\""
In you config file capture 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 tags
variable from your tests. Good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论