Search code examples
javascriptcypressmocha.jscypress-cucumber-preprocessor

Cypress Cucumber, how Get to data from page in one step and use it another scenario step


In one scenario I'm getting data from page and saving it in a variable as an alias.
Then I want to use the same variable/data in another scenario, to type into an <input> field. I'm getting this error:

cy.wait() could not find a registered alias for @Orderinfo.
You have not aliased anything yet.*

The data is stored in @Orderinfo but is not accessible in the other scenario step.

Then("Get Data from page", () => {
  cy.get(".os-order-number").invoke("text").then(($Oid) => {
    let Order = $Oid.text();
    let Order_id = Order.replace(/[^0-9]/g, "");
    cy.wrap(Order_id).as("Orderinfo");
  });
});


Given("Go to Login", () => {
  cy.visit("https://dev.simplifyshopping.com/register/");
});

When("Paste variable here", () => {
  cy.wait(2000);
  cy.wait("@Orderinfo")
  cy.get("@Orderinfo")).then((Orderinfo) => {
    console.log(Orderinfo);
    cy.get("#id_email").type(Orderinfo);
  });
});

Solution

  • So both, the use across several steps of the same scenario, as well as scenario overlapping are possible with Cypress using Cucumber Preprocessor.

    1. Use of values across multiple steps of the same scenario

    Referring to the example from the question, the Order_Id can be defined outside the steps and is thus accessible in the global scope from all steps. If I understood the code correctly, it would be something like this (probably unnecessary code commented out):

    let Order_id;
    
    Then("Get Data from page", () => {
      cy.get(".os-order-number").invoke("text").then(($Oid) => {
        let Order = $Oid.text();
        Order_id = Order.replace(/[^0-9]/g, "");
        // cy.wrap(Order_id).as("Orderinfo");
      });
    });
    
    
    Given("Go to Login", () => {
      cy.visit("https://dev.simplifyshopping.com/register/");
    });
    
    When("Paste variable here", () => {
      cy.wait(2000);
      // cy.wait("@Orderinfo")
      // cy.get("@Orderinfo")).then((Orderinfo) => {
      //  console.log(Orderinfo);
      //  cy.get("#id_email").type(Orderinfo);
      // });
    
      console.log(Order_id);
      cy.get("#id_email").type(Order_id);
    });
    

    2. Use of values across scenarios (hold state across tests)

    To make certain values accessible across the execution of different scenarios, for example, a helper.js file can be created containing the following code:

    export const stateStore = {};
    

    Inside your step definition files, you can then import the stateStore and fill it with values as you like:

    import { Given, When } from 'cypress-cucumber-preprocessor/steps';
    import { stateStore } from '../helpers';
    
    // step used in first scenario
    Given('some value is made available in scenario 1', () => {
      stateStore.someValue = 'this is a value';
    });
    
    // step used in second scenario
    When('this value can be used in another step of scneario 2', () => {
      console.log(`Print some value: ${stateStore.someValue}`);
    });