我需要在每次测试之前运行一些代码。
如果我有一个单一的规范文件,例如,这可能是一个简单的解决方案main.e2e.ts
但为大型应用程序制作一个规范文件听起来太脏了。
我将测试分成多个规范文件(例如 login.e2e.ts
和 dashboard.e2e.ts
),但我想在每个测试之前和之后运行一些代码每个它
,无论规范文件如何。
我发现使用 beforeEach(() => {}
和 afterEach(() => {}
是可能的,我现在正在这样做在每个文件中。但我相信这种方法必须有一些更漂亮的解决方案。
有没有办法在使用多个规范文件时在每个 it
之前和之后运行代码?
最佳答案
如果您有公共(public)且共享的 before 和 after 方法,则可以将其写入公共(public)文件。然后将这些函数导入到规范文件中,并在 beforeEach
、beforeAll
、afterEach
和 afterAll
中调用它们。我用 TypeScript 编写了以下内容。
common.ts
// import some protractor global objects
import { browser } from 'protractor';
export let beforeMethod = () => {
// do some method
browser.get('/');
}
export let beforeAsyncMethod = (done) => {
// do something that is async
done();
}
export let afterMethod = () => {
// do some method
}
登录.e2e-spec.ts
import { beforeMethod, beforeAsyncMethod, afterMethod } from './common';
describe('login', () => {
// do some setup
beforeEach( beforeMethod );
beforeEach( beforeAsyncMethod );
it('should do your test', () => {
// your amazing test.
});
afterEach( afterMethod );
});
关于angular - Protractor - 在每次测试之前和之后运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779235/