简体   繁体   中英

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
}

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);   
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM