Neval
Neval

Reputation: 23

Cypress - How to redeclare the variable in Cypress synchronously?

So, I want to wait for the Network Request in the searchbox.

Take this for example:

I want to search for "Base Data for A".

In the Network Request, it will look like this:

/api/search/Base+Data+for+A.

let branchName = "Base Data for A"
let bankBranch = branchName;
cy.intercept("GET", `/api/core/dropdown/bank-branch?*${bankBranch}*`).as("bankBranch");

  cy.get("#bankBranch")
      .type(bankBranch)
      .then(() => {
        bankBranch = bankBranch.replace(/\s+/g, "+");
        return cy.wrap(bankBranch);
      });
  cy.log(bankName) // This still showed as "Base Data for A", not "Base+Data+for+A"
      .wait("@bankBranch")

Because of that, the @bankBranch will not be called. How to redefine the bankBranch into "Base+Data+for+A" so the @bankBranch will be called?

I couldn't explain it clearer than this, but I hope someone gets it and help me.

Thank you.

Upvotes: 0

Views: 52

Answers (1)

Grainger
Grainger

Reputation: 154

It's the same answer as your last question, the log must be done in a callback to receive the changed value

cy.get("#bankBranch")
  .type(bankBranch)
  .then(() => {
    bankBranch = bankBranch.replace(/\s+/g, "+");
    cy.log(bankName)                                   // changed value
  })
cy.log(bankName)                                       // original value

Because of that, the @bankBranch will not be called

That's a different issue, the change is made after the value is typed.

let branchName = "Base Data for A"
let bankBranch = branchName.replace(/\s+/g, "+")

cy.intercept("GET", `/api/core/dropdown/bank-branch?*${bankBranch}*`)
  .as("bankBranch")

cy.get("#bankBranch")
  .type(bankBranch)
cy.wait("@bankBranch")    // wait for call

Upvotes: 2

Related Questions