创建和解决量角器承诺 [英] creating and resolving promises in protractor

查看:210
本文介绍了创建和解决量角器承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用量角器在角应用程序的页面添加存储信息编写测试用例,在最初我指望门店数我已经有和试块完成后,我预计算一个这样增加对于我按照创建承诺此链接<一个这样做href=\"http://stackoverflow.com/questions/21055400/how-to-create-and-manipulate-promises-in-protractor\">How创建和量角器操纵的承诺?

 描述('对myApp',函数(){
  VAR项目,startCount;  VAR testPromise =功能(){
    项目= element.all(by.repeater('店storelist'));
    VAR递延= protractor.promise.defer();
    items.count()。然后(功能(orgCount){
       startCount = orgCount;
       的console.log('开始计数:'+ startCount); //输出正确的值例如,6
    },功能(错误){
       的console.log(错误);
    });
    返回deferred.promise;
  };它('应该接受有效数据增加新店',函数(){
   VAR CNUM = NULL;
   testPromise()。然后(功能(结果){
      CNUM =结果;
      的console.log('CNUM值:'+ CNUM); //这个值不打印控制台
   });
   / * code在商店页面中添加测试字段* /   期待(items.count())TOBE(CNUM + 1)。
 });
});

我期待储存次数是在测试结束时相同。计数()被解析的承诺和门店数量的正确值将被印在testPromise(),但它阻止当我打电话testPromise()方法,然后它永远不会在'然后'块

和最终的结果说

 消息:
 预期6为1。
 堆栈跟踪:
  错误:无法预期

我也是从这个链接<一个研究多一点的webdriver.promise.Promise() href=\"http://selenium.google$c$c.com/git/docs/api/javascript/class_webdriver_promise_Promise.html\">http://selenium.google$c$c.com/git/docs/api/javascript/class_webdriver_promise_Promise.html并使用创建承诺,解决其价值,但不知道是什么问题尝试。无论是我得到错误信息说预计6为NaN或预计6为1我这不是解决一个承诺或书面形式,然后块是否正确?就这个问题AP preciate一些见解/帮助。


解决方案

创建

有关创建于量角器一个承诺,你必须写:

  VAR递延= protractor.promise.defer();
VAR承诺= deferred.promise;


回调

的回调异步调用。
您可以注册成功回调一个(或多个):

  promise.then(函数(){
   ...
});

您也可以注册的错误回调一个(或多个):

  promise.then(NULL,函数(){
   ...
});

这些注册可能被链接:

  promise.then(函数(){
   ...
})。然后(函数(){
   ...
}),然后(NULL,函数(){
   ...
})。然后(函数(){},函数(){
   ...
}),然后(的onSuccess,onFailure处)。


分辨率

成功

时的承诺是成功地解决了成功回调调用:

  deferred.fulfill(值);

失败

时的承诺是成功地解决了失败回调调用:

  deferred.reject(新错误('出现问题'));


在code

您错过了解析步骤。你必须履行诺言。

一个更完整的引用是 Webdriver.js文档

I am writing a test case for adding store information in the page for Angular app using Protractor, where initially I am counting the number of stores I already have and after the test block is done I expect the count to increase by one so for that I am doing this by following this link of creating promises How to create and manipulate promises in Protractor?

describe('myApp', function() {
  var items,startCount;

  var testPromise = function(){
    items = element.all(by.repeater('store in storelist'));
    var deferred = protractor.promise.defer();
    items.count().then(function(orgCount){
       startCount = orgCount;
       console.log('Start Count: '+ startCount); //prints correct value e.g, 6
    }, function(error){
       console.log(error);
    });
    return deferred.promise;
  };

it('should accept valid data for adding new store', function() {
   var cNum = null;
   testPromise().then(function(result){
      cNum = result;
      console.log('cNUm value: '+ cNum); //this value doesn't get printed in console
   });
   /* Code for adding test fields in store page */

   expect(items.count()).toBe(cNum+1);
 });
});

I expect the store count to be same at the end of the test. count() is resolving a promise and correct value of store count gets printed in testPromise() but in it block when I call testPromise().then method it never goes in that 'then' block

and the end result says

Message:
 Expected 6 to be 1.
 Stacktrace:
  Error: Failed expectation

I also researched a bit more in webdriver.promise.Promise() from this link http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_promise_Promise.html and tried using that for creating a promise and resolving its value but not sure what the problem is. Either I get error message saying 'expected 6 to be NaN' or 'expected 6 to be 1' Am I not resolving a promise or writing 'then' block correctly? Would appreciate some insights/help on this issue.

解决方案

Creation

For creating a promise in protractor, you have to write:

var deferred = protractor.promise.defer();
var promise = deferred.promise;


Callbacks

The callbacks are invoked asynchronously. You can register one (or more) "on success" callbacks:

promise.then(function() {
   ...
});

you can also register one (or more) "on error" callback:

promise.then(null, function() {
   ...
});

These registrations could be chained:

promise.then(function() {
   ...
}).then(function() {
   ...
}).then(null, function() {
   ...
}).then(function() {

}, function() {
   ...
}).then(onSuccess, onFailure);


Resolution

Success

The "on success" callbacks are invoked when the promise is resolved successfully:

deferred.fulfill(value);

Failure

The "on failure" callbacks are invoked when the promise is resolved successfully:

deferred.reject(new Error('a problem occurs'));


In your code

you missed the resolution step. You have to fulfill the promise.

A more complete reference is available in the Webdriver.js documentation

这篇关于创建和解决量角器承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆