简体   繁体   中英

Cypress: How to Login during beforeEach

beforeEach('Login', function() 
{
cy.visit('/')
    
const userCred = new Map(users);    
for (const [key, value] of userCred.entries()) 
{
    
        cy.login(key,value)
    })
}
})

I get following error Message [1]: https://i.stack.imgur.com/f0xKU.png [![error message][1]][1]

The Login Command works when I use it in the describe ('login', ()=> { it('login', ()=>{ cy.login(key, value) })})

How can I get the login work in the beforeEach?

It happens because you mess synchronous and asynchronous code. Here is how you should do it:

for (const [key, value] of userCred.entries()) {
    beforeEach(function() {
        cy.visit('/')
        cy.login(key, value);
    })
}

Also, I would recommend you to put cy.visit('/') inside the login custom command, as:

  1. .login() cannot be called without it;
  2. you are not repeating yourself on every test;

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