Simulate the passage of time in protractor?

后端 未结 2 1448
独厮守ぢ
独厮守ぢ 2021-01-06 09:33

I have a few spots where things happen in the UI on a delay using $timeout or $interval. Here\'s a simplified example:

Controller code:



        
2条回答
  •  感动是毒
    2021-01-06 09:50

    You could do this potentially using async.whilst. The idea is keep on looking for the element until the timeout is reached. If the element is found BEFORE timeout reaches or if element is NOT found within the timeout, test fails otherwise it passes. I haven't tested this but you get the idea. For example,

    var driver = browser.driver,
     wd = browser.wd,
     async = require('async'),
     start = Date.now(),
     found = false,
     diff;
     async.whilst(
          function() {
              var diff = Date.now() - start;
              return diff <= 10000 && !found;
          },
          function(callback) {
             driver.findElement(wd.By.id('myElement')).then(function() {
                  found = true;
                  callback();
               },function(err) {
                found = false;
                callback();
             });
          },
          function (err) {
              var isTesrPassed = !err && found && diff>=10000;
              assertTrue(isTestPassed, 'element visibility test failed');
          }
     );
    

提交回复
热议问题