Search code examples
node.jstypescripttagscypresscypress-cucumber-preprocessor

How to get the Tag name passed while running Cypress Framework


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 ?


Solution

  • 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
      ...
    })