I'm testing Cypress and I have this code for a custom Cypress command:
Cypress.Commands.add('login', (user, password) => {
if (!user || !password) {
user = 'user@test.com.br';
password = '123321';
}
cy.visit('');
cy.contains('Entrar').click();
cy.get('input[name=_username]')
.type(user);
cy.get('input[name=_password]')
.type(password);
cy.get('.btn').click();
});
If you call cy.login() without any arguments, user and password are assigned within the if block. Otherwise, it uses the passed parameter values.
I tried to add fixtures here, and came up with this code:
Cypress.Commands.add('login', (user, password) => {
if (!user || !password) {
cy.fixture('users').then((json) => {
var user, password;
user = json[0].email;
password = json[0].password;
login2(user,password);
});
} else {
login2(user, password);
}
function login2(user, password) {
cy.visit('');
cy.contains('Entrar').click();
cy.get('input[name=_username]')
.type(user);
cy.get('input[name=_password]')
.type(password);
cy.get('.btn').click();
}
});
When I set user = json[0].email, it has the value just inside the .then,
so I created the function login to fix that.
I imagine there is a better way to do this. Any ideas?
