【问题标题】:creating and resolving promises in protractor在量角器中创建和解决承诺
【发布时间】:2014-08-08 23:28:26
【问题描述】:

我正在编写一个测试用例,用于使用 Protractor 在 Angular 应用程序的页面中添加商店信息,最初我在计算我已经拥有的商店数量,在测试块完成后,我希望计数增加一个所以为此,我通过创建承诺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() 正在解决一个承诺,并且在 testPromise() 中打印了存储计数的正确值,但是当我调用 testPromise().then 方法时它会阻塞它永远不会进入那个“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”我没有正确解决承诺或写“那么”块吗?非常感谢您对此问题的一些见解/帮助。

【问题讨论】:

    标签: angularjs selenium protractor


    【解决方案1】:

    创作

    要在量角器中创建承诺,您必须编写:

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

    回调

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

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

    您还可以注册一个(或多个)“出错”回调:

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

    这些注册可以被链接起来:

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

    分辨率

    成功

    当成功解决承诺时调用“成功”回调:

    deferred.fulfill(value);
    

    失败

    当 promise 成功解决时调用“失败时”回调:

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

    在您的代码中

    您错过了解决步骤。你必须兑现承诺。

    Webdriver.js documentation 中提供了更完整的参考资料

    【讨论】:

    • 有人可以举一个更具体的例子吗?我已尝试遵循此答案,但仍然无法正常工作。
    • @BlakeBiker 我认为你应该问自己的问题来描述你的具体问题
    • 以上指向 webdriver 文档的链接似乎已过期。
    • @ChuckvanderLinden 修复链接
    • 我需要从量角器包中导入什么才能使用 protractor.promise.defer?我的 IDE 在该行抱怨“找不到名称‘量角器’”。
    【解决方案2】:

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

    // 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() 拒绝,我们将原因传递给我们自己的拒绝呼叫。

    要使用这个函数,传入一个元素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);
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,并通过为预期项目数创建承诺来解决它:

      it('should accept valid data for adding new store', function() {
          // Given
          let expectedCountAfterNewRow = items.count().then((cnt)=>{return cnt+1}); 
          // Note: items.count() is a promise so the expected count should a promise too
      
          // When
          /* Code for adding test fields in store page */
      
          // Then
         expect(items.count()).toEqual(expectedCountAfterNewRow);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        • 2019-07-07
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多