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

问题描述

我正在编写一个测试用例,用于使用量角器在 Angular 应用程序的页面中添加商店信息,最初我正在计算我已经拥有的商店数量,在测试块完成后,我希望计数增加一为此,我通过遵循创建承诺的链接如何创建并在量角器中操纵承诺?

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);
 });
});

我希望在测试结束时商店数量相同.count() 正在解决一个承诺,并且 store count 的正确值被打印在 testPromise() 中,但是当我调用 testPromise().then 方法时,它永远不会进入那个then"块

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

最终结果是

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

我还在此链接中对 webdriver.promise.Promise() 进行了更多研究 http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_promise_Promise.html 并尝试使用它来创建承诺并解决其价值,但不确定问题是什么.要么我收到错误消息,说预计 6 为 NaN"或预计 6 为 1" 我是不是没有正确解决承诺或编写then"块?希望能就此问题提供一些见解/帮助.

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.

推荐答案

以下是在 Protractor 中使用的用户定义函数的工作示例,该函数创建和履行(或拒绝)承诺:

Here's a working example of a user-defined function for use in Protractor which creates and fulfills (or rejects) a promise:

// Get the text of an element and convert to an integer.
// Returns a promise.
function getTextAsInteger(element, radix) {
  // Specify a default radix for the call to parseInt.
  radix = radix || 10;
  // Create a promise to be fulfilled or rejected later.
  var deferred = protractor.promise.defer();
  // Get the text from the element. The call to getText
  // returns a promise.
  element.getText().then(
    function success(text) {
      var num = parseInt(text, radix);
      if (!isNaN(num)) {
        // Successfully converted text to integer.
        deferred.fulfill(num);
      } else {
        // Error converting text to integer.
        deferred.reject('could not parse "$1" into an integer'
          .replace('$1', text));
      }
    },
    function error(reason) {
      // Reject our promise and pass the reason from the getText rejection.
      deferred.reject(reason);
    });

  // Return the promise. This will be resolved or rejected
  // when the above call to getText is resolved.
  return deferred.promise;
}

该函数将 element 作为参数并调用其 getText() 方法,该方法本身返回一个承诺.成功调用 getText() 后,将文本解析为整数并履行承诺.如果 getText() 拒绝,我们将原因传递给我们自己的拒绝调用.

The function takes element as an argument and calls its getText() method which itself returns a promise. On a successful call to getText(), parse the text as an integer and fulfill the promise. If getText() rejects, we pass the reason to our own reject call.

要使用这个函数,传入一个元素承诺:

To use this function, pass in an element promise:

var numField = element(by.id('num-field'));
getTextAsInteger(numField).then(
    function success(num) {
        console.log(num);
    },
    function error(reason) {
        console.error(reason);
    });

或:

var numField = element(by.id('num-field'));
expect(getTextAsInteger(numField)).toEqual(jasmine.any(Number));
expect(getTextAsInteger(numField)).toBeGreaterThan(0);

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

08-23 06:54