小编典典

在量角器中创建和解决承诺

selenium

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

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()正在解决一个承诺,并且商店计数的正确值会打印在testPromise()中,但是当我调用testPromise()时会在该块中打印。那么方法就永远不会出现在``then’‘块中

最终结果说

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

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


阅读 255

收藏
2020-06-26

共1个答案

小编典典

这是一个用于Protractor中的用户定义函数的工作示例,该函数创建并实现(或拒绝)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()方法,该方法本身返回一个Promise。成功调用时getText(),将文本解析为整数并兑现承诺。如果getText()拒绝,我们会将原因传递给我们自己的拒绝电话。

要使用此功能,请传递一个元素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);
2020-06-26