3

Below is mine scenario where in one scenario I'm getting data from page and saving it in a variable as alias. tHen I want to use same variable/data in other scenario to put in an input field.I'm using Alias but getting this error.

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

Even it alliased properly. Data stores in @Orderinfo but not accessable in other sceanrio 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);
  });
});
Muneeb
  • 67
  • 1
  • 7
  • Do you really want to access the variable from a different scenario (i.e. a different test)? Or do you just want to access it in another step of the same scenario? – Sebastiano Schwarz Feb 13 '22 at 13:10
  • @SebastianoVierk First I have to use in same scenario within differentSteps. But for future I have to use in other Scenario's too. – Muneeb Feb 13 '22 at 14:00

2 Answers2

4

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}`);
});
Sebastiano Schwarz
  • 1,060
  • 2
  • 13
  • 32
0

It can be done using "alias" .as in cypress. Store variable .as("vaiableName") and then access it in required function as this.variableName. It could be like this.

Then("Get Data from page", function () {
    cy.get(".os-order-number").then($Oid => {
        const Order = $Oid.text()
        const Order_id = Order.replace(/[^0-9]/g, "")
        cy.log("inside Then function" + Order_id)
        cy.wrap(Order_id).as("wrapText")
    })
})

Given("Go to Login", function () {

    cy.log(this.wrapText)
})
Muneeb
  • 67
  • 1
  • 7