[英]How to reuse and run same test cases in every page of application using PROTRACTOR E2E?
REQUIREMENT:要求:
WHAT I'VE TRIED:我试过的:
// login.e2e-spec.ts
import { browser, by } from 'protractor';
import { LoginPage } from '../../pages/login.po';
describe('LOGIN page', () => {
let page: LoginPage;
beforeEach(async () => {
page = new LoginPage();
await page.navigateTo();
});
describe('Login form', async () => {
it('should navigate to page containing login form', async () => {
await expect(browser.getCurrentUrl()).toEqual(
'http://localhost:4200/#/login'
);
});
it('buttons suite', async () => {
const buttons = await page.buttons.getAllButtonsByTag();
page.buttons.testButtonsClasses(buttons);
});
});
});
//login.po.ts
import { Page } from '../classes/page';
export class LoginPage extends Page {
public constructor() {
super('login');
}
}
//page.ts
import { browser } from 'protractor';
import { Buttons } from './buttons';
import { Url } from './url';
export class Page {
public url: Url;
public buttons: Buttons;
public constructor(pageId) {
this.url = new Url(pageId);
this.buttons = new Buttons();
}
public async navigateTo(endpoint?) {
const url = this.url['fullUrl'] + endpoint ? `/${endpoint}` : '';
await browser.get(url);
}
}
//buttons.ts
import { by, element } from 'protractor';
export class Buttons {
public constructor() {}
public getAllButtonsByTag() {
return element.all(by.tagName('button'));
}
public async testButtonsClasses(buttons) {
for (let i = 0; i < buttons.length; i++) {
const classAttribute = await buttons[i].getAttribute('class');
expect(classAttribute).toContain('btn');
}
}
}
QUESTION:问题:
Can someone assist in solving this issue.有人可以协助解决这个问题。 I'm trying to reuse the same test cases in every page.我试图在每个页面中重用相同的测试用例。
the question is too broad.这个问题太宽泛了。 First shot in the dark would be - you need to use this在黑暗中的第一枪将是 - 你需要使用这个
await buttons.get(i).getAttribute('class')
instead of this而不是这个
await buttons[i].getAttribute('class')
you have a method你有一个方法
public async testButtonsClasses(buttons) {
for (let i = 0; i < buttons.length; i++) {
const classAttribute = await buttons[i].getAttribute('class');
expect(classAttribute).toContain('btn');
}
}
First of all use .get(i)
instead of [i]
and I mentioned above.首先使用.get(i)
而不是[i]
和我上面提到的。 Then the method is asynchronous.然后该方法是异步的。 When something is async, it returns a promise.当某些东西是异步的,它返回一个 promise。 Any promise needs to be resolved.任何 promise 都需要解决。 The best way to resolve protractor's promises is to use await
when you call the method解决量角器承诺的最好方法是在调用方法时使用await
await page.buttons.testButtonsClasses(buttons);
additionally, expect
is synchronous, and doesn't need await
.另外, expect
是同步的,不需要await
。 What you pass as a parameter returns a promise and DOES need await
您作为参数传递的内容返回 promise 并且需要await
await expect(await browser.getCurrentUrl()).toEqual('http://localhost:4200/#/login');
Lastly, I think you're on the right track for externalizing your tests, but keep in mind it is a bad practice to put assertions in page objects.最后,我认为您在外部化测试方面处于正确的轨道上,但请记住,将断言放在页面对象中是一种不好的做法。 I can't name a good reason why from the top oh my head, but better to avoid it我无法从头顶上说出一个很好的理由,但最好避免它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.