我有一个简单的场景,我想测试我是否能够删除一些对象。所以场景如下:
1通过休息添加一些独特的对象(不使用浏览器,我不想在每个/所有之前使用,因为它仅适用于此测试)
2删除Web应用程序中的上述对象
所以我写了我的代码模拟:
it('test promise', function(done) {
console.log('start');
d = protractor.promise.defer();
setTimeout(function() {
console.log("rest call");
d.fulfill('ok');
done();
}, 3000);
console.log('before expect');
expect(d).toBe('ok'); //code should be stoped here till rest above finish
console.log('after expect');
console.log('rest test based on deffer result');
console.log('change tab... find elements... click to delete...');
});
此代码的输出是:
start
before expect
after expect
change tab... find elements... click to delete...
rest call
因此,您可以看到在我将执行所有webdriver操作后运行休息呼叫... 一些想法?
编辑:
我添加了控制流,但它仍然不起作用。代码:
it('test promise', function(done) {
console.log('start');
flow = protractor.promise.controlFlow();
var d = protractor.promise.defer();
var restCall = function _makeRestCall() {
setTimeout(function () {
console.log("rest call");
d.fulfill('ok');
}, 3000);
return d.promise
};
console.log('before expect');
flow.execute(restCall);
// expect(d).toBe('ok'); //version 1 not work
expect(restCall).toBe('ok'); //version 2 not work
console.log('after expect');
console.log('rest test based on deffer result');
});
输出:
start
before expect
after expect
rest test based on deffer result
rest call
答案 0 :(得分:2)
你创造承诺的方式绝对正确
唯一需要的更改是将其添加到Protractor Control Flow。它需要添加两个步骤,并且几乎不需要重构代码
步骤1:启动量角器控制流程,然后使用flow.execute()
插入任何async function which returns promise into Control Flow
describe('sample test', function(){
flow = protractor.promise.controlFlow();
var restCall = function _makeRestCall() {
var d = protractor.promise.defer();
setTimeout(function () {
console.log("rest call");
d.fulfill('ok');
}, 3000);
return d.promise
}
flow.execute(restCall)
这会将您的异步调用放入控制流中,只有在解析了承诺后,浏览器命令才会执行
更新:添加了测试用例的完整流程
describe('sample test', function(){
it('test promise', function() {
browser.get('')
console.log('start');
flow = protractor.promise.controlFlow();
var d = protractor.promise.defer();
var restCall = function _makeRestCall() {
setTimeout(function () {
console.log("rest call");
d.fulfill('ok');
}, 3000);
return d.promise
};
console.log('before expect');
// Can directly add expect here as flow.execute() returns promise
expect(flow.execute(restCall)).toBe('ok');
// All subsequent browser command part of Protractor Control Flow will be executed only after the promise of restCall is resolved
browser.getCurrentUrl().then(function(value) {
console.log('after expect');
console.log('rest test based on deffer result');
});
});
});