量角器 - 在每次测试之前和每次测试之后运行代码

时间:2017-05-04 09:38:45

标签: angular protractor e2e-testing

我需要在每次测试之前运行一些代码。 如果我有一个spec文件,这可能是一个简单的解决方案,例如main.e2e.ts但为大型应用程序制作一个spec文件听起来太脏了。

我将测试分成多个规范文件(例如login.e2e.ts&amp; dashboard.e2e.ts),但我想在每个it之前运行一些代码,无论spec文件是什么。< / p>

我发现可以使用beforeEach(() => {}afterEach(() => {},我现在正在每个文件中执行此操作。但我相信这种方法必须有一些更漂亮的解决方案。

使用多个spec文件时,有没有办法在每个it之前和之后运行代码?

1 个答案:

答案 0 :(得分:1)

如果您有方法之前和之后的共同和共享,则可以将其写入公共文件。然后将这些函数导入到spec文件中,并在beforeEachbeforeAllafterEachafterAll中调用它们。我在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
}

login.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 );
});
相关问题