cy.visit() in before(..) block result in CypressError: Cannot call cy.visit() outside a running test

Viewed 719

I am writing spec file for Cypress. I have one spec file including 3-4 tests. each test using the same url so instead of writing cy.visit in each test, I am trying to move in before(..) block.

  before(){
    cy.visit('https://rahulshettyacademy.com/angularpractice/');
  }

but I am getting CypressError: Cannot call cy.visit() outside a running test. How can use cy.visit in before(..) block?

1 Answers

You might want to use the arrow function, try like this

        before(() => {
            cy.visit('https://rahulshettyacademy.com/angularpractice/');
        });

cypress docs

Related