Des*_*tio 1 javascript cypress
我正在尝试测试几种类型的用户是否成功登录。他们中的一些人有一些不同的行为。我试图在不复制粘贴相同代码部分的情况下执行此操作,因为逐一维护它们(不同文件或案例中的每个用户类型)是疯狂的。
我有自己的测试沙箱,因此文件中的凭据不会使安全性受到攻击(我希望?)
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.
}):
});
});
Run Code Online (Sandbox Code Playgroud)
尽管您所写的内容可行,但我个人不喜欢这种方法。对我来说,一般的经验法则是,如果要在测试用例中添加条件块,则值得将测试拆分为更小的测试用例。我会这样做:
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
});
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,测试描述也更具描述性,可以帮助读者第一眼就明白哪个测试失败了。
您还可以通过将所有断言信息(例如 url、passwordError 等)存储在您要迭代的对象中来执行相同的操作,但此时,您也可以创建单独的测试用例,这样更易于阅读和使用了解发生了什么事。
| 归档时间: |
|
| 查看次数: |
4039 次 |
| 最近记录: |