Simulate the passage of time in protractor?

心不动则不痛 提交于 2019-11-30 23:03:21

You can decorate $timeout and $interval to override the delay supplied to them:

lower-wait-time.js

exports.module = function() {
    angular.module('lowerWaitTimeDecorator', [])
    .config(function($provide) {
        $provide.decorator('$timeout', function($delegate) {
            return function() {
                // The second argument is the delay in ms
                arguments[1] = arguments[1] / 10;
                return $delegate.apply(this, arguments);
            };
        });
    })
};

Usage

beforeAll(function() {
    var lowerWaitTime = require('lower-wait-time');
    browser.addMockModule('lowerWaitTimeDecorator', lowerWaitTime.module);
});

afterAll(function() {
    browser.removeMockModule('lowerWaitTimeDecorator');
});

it('My-sped-up-test', function() {

});

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');
      }
 );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!