Protractor - beforeEach 中的异步任务

Protractor - Asynchronous tasks in beforeEach

我正在使用 Protractor / Jasmine 编写 UI 测试。 我希望在我所有的测试用例中执行一个特定的任务,即。读取跨度 (id="mytxt")。所以,我想在 beforeEach 中完成这个任务。像这样:

var mytext="";
beforeEach(function(){
    element(by.id('mytxt')).getText().then(function(txt){
        mytext = txt;
    });
});

it('should ...', function(){
    expect(mytext).toBe('xyz');
});

有没有办法让测试只在 beforeEach 中的异步任务完成后执行?

您可以像这样使用 done 回调:

var mytext="";
beforeEach(function(done){
    element(by.id('mytxt')).getText().then(function(txt){
        mytext = txt;
        done();
    });
});

it('should ...', function(){
    expect(mytext).toBe('xyz');
});