如何使用量角器向流量控制队列添加承诺?
在我的测试中,在使用量角器运行一些 ui 测试之前,我正在调用和外部库将数据播种到我们的后端.
In my test I am calling and outside library to seed data into our backend before running some ui tests using protractor.
'use strict'
var dataBuilder = require('data_builder.js');
describe('test', function () {
var testData = {
name: 'foo',
title: 'bar',
...
};
beforeEach(function () {
//create test data on the backend
dataBuilder.create(testData).then(function (id) {
testData.id = id.id;
});
});
it('test something', function () {
...
});
因此,dataBuilder 返回的承诺在 it() 实际完成之前没有得到解决.如何将dataBuilder返回的promise添加到webDriver的流控中?
As such the promise returned by the dataBuilder isn't resolved before the it() actually finishes. How can I add the promise returned by the dataBuilder into webDriver's flow control?
Protractor 在量角器对象上公开 WebDriverJS 承诺,因此您可以使用 flow.await
方法或创建新的承诺并使用 flow.execute.
Protractor exposes WebDriverJS promises on the protractor object so you could use either the flow.await
method or create a new promise and use flow.execute
.
前者可以实现如下:
flow = protractor.promise.controlFlow()
flow.await(dataBuilder.create(testData)).then( function(id) {
testData.id = id.id;
})
您可以在此 博文.
这可以在 it
函数本身中完成,或者如果这对所有测试都是通用的,请考虑将它放在量角器配置的 onPrepare
函数中.
This could be done in the it
function itself or if this is common to all your tests consider placing it in the onPrepare
function of your protractor config.