Skip to content

Commit

Permalink
Merge pull request #3068 from sebv/implicit-timeout
Browse files Browse the repository at this point in the history
resetting commandTimeout at each step of the implicitTimeout cycle
  • Loading branch information
jlipps committed Jul 7, 2014
2 parents 7dda297 + e55b32f commit 83e64a1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/appium.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ Appium.prototype.configure = function (args, desiredCaps, cb) {
}
this.device = this.getNewDevice(deviceType);
this.device.configure(args, desiredCaps, cb);
// TODO: better collaboration between the Appium and Device objects
this.device.onResetTimeout = function () { this.resetTimeout(); }.bind(this);
};

Appium.prototype.invoke = function (cb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/devices/android/android-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ androidController.findUIElementOrElements = function (strategy, selector, many,
}.bind(this));
}
}.bind(this);
this.waitForCondition(this.implicitWaitMs, doFind, cb);
this.implicitWaitForCondition(doFind, cb);
};

androidController.handleFindCb = function (err, res, many, findCb) {
Expand Down
3 changes: 3 additions & 0 deletions lib/devices/android/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ Android.prototype.waitForActivityToStop = function (cb) {
this.adb.waitForNotActivity(this.args.appWaitPackage, this.args.appWaitActivity, cb);
};


Android.prototype.resetTimeout = deviceCommon.resetTimeout;
Android.prototype.waitForCondition = deviceCommon.waitForCondition;
Android.prototype.implicitWaitForCondition = deviceCommon.implicitWaitForCondition;

_.extend(Android.prototype, androidController);
_.extend(Android.prototype, androidContextController);
Expand Down
14 changes: 14 additions & 0 deletions lib/devices/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ exports.proxyWithMinTime = function (command, ms, cb) {
}]);
};

exports.resetTimeout = function () {
if (this.onResetTimeout) this.onResetTimeout();
};

exports.waitForCondition = function (waitMs, condFn, cb, intervalMs) {
if (typeof intervalMs === "undefined") {
intervalMs = 500;
Expand All @@ -73,6 +77,16 @@ exports.waitForCondition = function (waitMs, condFn, cb, intervalMs) {
spin();
};

exports.implicitWaitForCondition = function (condFn, cb) {
var _condFn = condFn;
condFn = function () {
var args = Array.prototype.slice.call(arguments, 0);
this.resetTimeout();
_condFn.apply(this, args);
}.bind(this);
this.waitForCondition(this.implicitWaitMs, condFn, cb);
};

exports.doRequest = function (url, method, body, contentType, cb) {
if (typeof cb === "undefined" && typeof contentType === "function") {
cb = contentType;
Expand Down
4 changes: 2 additions & 2 deletions lib/devices/ios/ios-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ iOSController.findUIElementOrElements = function (strategy, selector, ctx, many,
}.bind(this);

if (_.contains(this.supportedStrategies, strategy)) {
this.waitForCondition(this.implicitWaitMs, doFind, cb);
this.implicitWaitForCondition(doFind, cb);
} else {
cb(null, {
status: status.codes.UnknownError.code
Expand Down Expand Up @@ -295,7 +295,7 @@ iOSController.findWebElementOrElements = function (strategy, selector, ctx, many
this.handleFindCb(err, res, many, findCb);
}.bind(this));
}.bind(this);
this.waitForCondition(this.implicitWaitMs, doFind, cb);
this.implicitWaitForCondition(doFind, cb);
};

iOSController.findElementOrElements = function (strategy, selector, ctx, many, cb) {
Expand Down
2 changes: 2 additions & 0 deletions lib/devices/ios/ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,9 @@ IOS.prototype.shutdown = function (code, traceDir, cb) {

};

IOS.prototype.resetTimeout = deviceCommon.resetTimeout;
IOS.prototype.waitForCondition = deviceCommon.waitForCondition;
IOS.prototype.implicitWaitForCondition = deviceCommon.implicitWaitForCondition;
IOS.prototype.proxy = deviceCommon.proxy;
IOS.prototype.proxyWithMinTime = deviceCommon.proxyWithMinTime;
IOS.prototype.respond = deviceCommon.respond;
Expand Down
38 changes: 24 additions & 14 deletions test/functional/ios/testapp/timeout/implicit-wait-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,39 @@ describe('testapp - timeout', function () {
var driver;
setup(this, desired).then(function (d) { driver = d; });

var impWaitSecs = 4;
var impWaitCheck = function () {
var before = new Date().getTime() / 1000;
return driver
.elementsByClassName('UIANotGonnaBeThere').then(function (missing) {
var after = new Date().getTime() / 1000;
(after - before).should.be.below(impWaitSecs + 2);
(after - before).should.be.above(impWaitSecs);
missing.should.have.length(0);
});
var impWaitCheck = function (impWaitMs) {
return function () {
var before = new Date().getTime();
return driver
.elementsByClassName('UIANotGonnaBeThere').then(function (missing) {
var after = new Date().getTime();
(after - before).should.be.below(impWaitMs + 2000);
(after - before).should.be.above(impWaitMs);
missing.should.have.length(0);
});
};
};

it('should set the implicit wait for finding elements', function (done) {
driver
.setImplicitWaitTimeout(impWaitSecs * 1000)
.then(impWaitCheck)
.setImplicitWaitTimeout(4000)
.then(impWaitCheck(4000))
.nodeify(done);
});

it('should work with small command timeout', function (done) {
driver
.setCommandTimeout(5000)
.setImplicitWaitTimeout(10000)
.then(impWaitCheck(10000))
.nodeify(done);
});

it('should work even with a reset in the middle', function (done) {
driver
.setImplicitWaitTimeout(impWaitSecs * 1000)
.then(impWaitCheck)
.setCommandTimeout(60000)
.setImplicitWaitTimeout(4000)
.then(impWaitCheck(4000))
.resetApp()
.sleep(3000) // cooldown
.then(impWaitCheck)
Expand Down

0 comments on commit 83e64a1

Please sign in to comment.