I'm trying to test a successful login by a few types of users. Some of them have a bit different behavior. I am trying to do this without copy-pasting same parts of code because maintaining them one-by-one (each userType in different file or case) was crazy.
I have my own test-sandbox, so the credentials in files will not put security under attack (I hope?)
- Is it OK to try to create more or less dynamically generated tests?
- Is my code over-plicated and there is just easier way?
- Am I missing something and doing some/everything wrong?
- Should I divide last else for one more "if" for all good cases with same behavior and else just for something unexpected?
const credentials = [
{userType: 'UserType1', login: 'UserType1Login', password: 'UserType1Password'},
{userType: 'UserType2', login: 'UserType2Login', password: 'UserType2Password'}
//6 user types and credentials for NonExistingUser and blocked
];
describe('Checks login', () => {
beforeEach('Go to Login Modal', () => {
cy.visit('/');
cy.get('[data-cy=loginModalOpen]').click();
});
// dynamically create a single test for each credential obj in the list
credentials.forEach(credential => {
it(`Checks Authorization by ${credential.userType} user`, () => {
cy.get('[data-cy=login]').type(credential.login);
cy.get('[data-cy=password]').type(credential.password);
cy.get('[data-cy=loginSubmit]').click();
if (credential.userType.includes('blocked')) {
cy.get('[data-cy=passwordError]').should('contain', 'User blocked');
cy.url().should('not.include', '/orders/published');
} else if (credential.userType.startsWith('UserType2')) {
cy.url().should('include', '/stores');
} else if (credential.userType.includes('nonExisting')) {
cy.get('[data-cy=passwordError]').should('contain', 'No user with such login');
cy.url().should('not.include', '/orders/published');
} else if (credential.userType.includes('UserType1')) {
cy.url().should('include', 'orders_all/published');
} else {
//all other cases.
cy.url().should('include', '/orders/published');
}
// i have some more "it" cases for error messages, but i THINK they are ok.
}):
});
});
I'm trying to test a successful login by a few types of users. Some of them have a bit different behavior. I am trying to do this without copy-pasting same parts of code because maintaining them one-by-one (each userType in different file or case) was crazy.
I have my own test-sandbox, so the credentials in files will not put security under attack (I hope?)
- Is it OK to try to create more or less dynamically generated tests?
- Is my code over-plicated and there is just easier way?
- Am I missing something and doing some/everything wrong?
- Should I divide last else for one more "if" for all good cases with same behavior and else just for something unexpected?
const credentials = [
{userType: 'UserType1', login: 'UserType1Login', password: 'UserType1Password'},
{userType: 'UserType2', login: 'UserType2Login', password: 'UserType2Password'}
//6 user types and credentials for NonExistingUser and blocked
];
describe('Checks login', () => {
beforeEach('Go to Login Modal', () => {
cy.visit('/');
cy.get('[data-cy=loginModalOpen]').click();
});
// dynamically create a single test for each credential obj in the list
credentials.forEach(credential => {
it(`Checks Authorization by ${credential.userType} user`, () => {
cy.get('[data-cy=login]').type(credential.login);
cy.get('[data-cy=password]').type(credential.password);
cy.get('[data-cy=loginSubmit]').click();
if (credential.userType.includes('blocked')) {
cy.get('[data-cy=passwordError]').should('contain', 'User blocked');
cy.url().should('not.include', '/orders/published');
} else if (credential.userType.startsWith('UserType2')) {
cy.url().should('include', '/stores');
} else if (credential.userType.includes('nonExisting')) {
cy.get('[data-cy=passwordError]').should('contain', 'No user with such login');
cy.url().should('not.include', '/orders/published');
} else if (credential.userType.includes('UserType1')) {
cy.url().should('include', 'orders_all/published');
} else {
//all other cases.
cy.url().should('include', '/orders/published');
}
// i have some more "it" cases for error messages, but i THINK they are ok.
}):
});
});
Share
Improve this question
edited Jun 17, 2022 at 7:22
jonrsharpe
122k30 gold badges268 silver badges476 bronze badges
asked Jun 17, 2022 at 7:13
DestioDestio
211 silver badge2 bronze badges
1 Answer
Reset to default 5Even though what you have written will work, I personally do not like this approach. For me as a general rule of thumb is that if you are going to add conditional blocks in your test cases, it is worth splitting the test into smaller test cases. Here is how I would have done this:
const credentials = {
UserType1: {
login: 'UserType1Login',
password: 'UserType1Password',
},
UserType2: {
login: 'UserType2Login',
password: 'UserType2Password'
}
};
const logInTheUser = (credential) => {
cy.get('[data-cy=login]').type(credential.login);
cy.get('[data-cy=password]').type(credential.password);
cy.get('[data-cy=loginSubmit]').click();
}
describe('Checks login', () => {
beforeEach('Log in the user', () => {
cy.visit('/');
cy.get('[data-cy=loginModalOpen]').click();
});
it('prints error and does not redirect if user is blocked', () => {
logInUser(credentials.blocked);
cy.get('[data-cy=passwordError]').should('contain', 'User blocked');
cy.url().should('not.include', '/orders/published');
});
it('redirects user of type 2 to stores page', () => {
logInUser(credentials.UserType2);
cy.url().should('include', '/stores');
});
it('prints error and does not redirect if user does not exist', () => {
loginUser(credentials.nonExisting);
cy.get('[data-cy=passwordError]').should('contain', 'No user with such login');
cy.url().should('not.include', '/orders/published');
});
it('redirects user of type 1 to published orders page', () => {
loginUser(credentials.UserType1);
cy.url().should('include', 'orders_all/published');
});
// ...other tests
});
As you can see, the test descriptions are also more descriptive and helps the reader to understand at first glance which test failed.
You you can also do the same by storing all the assertion information in (e.g url, passwordError etc) in the objects that you were iterating over but at that point, you might as well just create separate test cases, which is easier to read and understand what's going on.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745343790a4623450.html
评论列表(0条)