我目前正在学习使用量角器编写测试,我陷入困境,无法理解编写简单登录/注销测试的正确方法。
describe('Login with dummy user', function () {
browser.ignoreSynchronization = true;
browser.get('https://localhost:44311');
element(by.id('userNameInput')).sendKeys('blabla');
element(by.id('passwordInput')).sendKeys('blablapassword');
element(by.id('submitButton')).click();
browser.ignoreSynchronization = false;
browser.sleep(2000);
it('page should have Inventory title', function () {
expect(browser.getTitle()).toEqual('Inventory');
});
it(' page should have logout button', function () {
var completedAmount = element.all(by.css('.logoutButton'));
expect(completedAmount.count()).toEqual(1);
});
describe('clicking loging out button', function () {
browser.sleep(2000);
element(by.css('[href="/account/signout"]')).click();
it('should redirect to account page', function () {
expect(browser.getCurrentUrl()).toEqual('https://localhost:44311/account');
});
it('should display a signed out message', function () {
expect(element(by.css('text-success')).getText()).toEqual('You have successfully signed out');
});
});
});
我希望前两个会在第二个描述之前运行,但浏览器会点击按钮,注销,浏览器关闭,然后才会运行测试并失败。
答案 0 :(得分:1)
我建议将所有代码保存在'it'块中并保持Login& “beforeAll”和“afterAll”函数内的LogOut功能分别如下:
describe('Login with dummy user', function () {
beforeAll(function() {
// Login Steps
// ignore synchronization set to true should have nested then statements
// since the synchronization is removed. Example:
//
// element(by.id('userNameInput')).sendKeys('blabla').then(() => {
// element(by.id('passwordInput')).sendKeys('blablapassword').then(() => {
// element(by.id('submitButton')).click();
// });
// });
});
it('page should have Inventory title', function () {
expect(browser.getTitle()).toEqual('Inventory');
});
it(' page should have logout button', function () {
var completedAmount = element.all(by.css('.logoutButton'));
expect(completedAmount.count()).toEqual(1);
});
afterAll(function() {
//Logout steps
});
});