Vince
Vince

Reputation: 7

How to return a value based on fixture value in Cypress

I'm trying to return a value based on fixture value and when I call the method from another class i am getting result as undefined.

Class A:
    getTestEnv() {
            let test;
            cy.fixture("Properties/env.json").then((profile) => {
              var df = profile.test_env;
              if (df.match(/d\d+/)) {
                test = `random.${df}.test.com`;
              } else {
                test = `random.${df}.autotest.com`;
              }
              return test;
            });
          }


Class B:
getDetails() {
  let env = this.getTestEnv();
  cy.log("env: " + env);   // Undefined
}

Upvotes: 0

Views: 533

Answers (1)

Martina Gusman
Martina Gusman

Reputation: 52

I think you have missed some return values.

The first method returns the fixture, then the second method will call that first method in the same way it would call the cy.fixture() itself, that is it will use a .then() to obtain the fixture value from the returned value

Class A:
   getTestEnv() {
            let test;
      return cy.fixture("Properties/env.json").then((profile) => {
              var df = profile.test_env;
              if (df.match(/d\d+/)) {
                test = `random.${df}.test.com`;
              } else {
                test = `random.${df}.autotest.com`;
              }
              return test;
            });
          }

getDetails() {
  this.getTestEnv().then(env => {
    cy.log("env: " + env);   
  }

Upvotes: 3

Related Questions