Skip to content

Commit

Permalink
make implicit wait do what it should
Browse files Browse the repository at this point in the history
  • Loading branch information
jlipps committed Feb 22, 2013
1 parent 51706a5 commit b8c2812
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ exports.postDismissAlert = function(req, res) {
};

exports.implicitWait = function(req, res) {
var seconds = req.body.ms / 1000;
req.device.implicitWait(seconds, getResponseHandler(req, res));
var ms = req.body.ms;
req.device.implicitWait(ms, getResponseHandler(req, res));
};

exports.setOrientation = function(req, res) {
Expand Down
51 changes: 46 additions & 5 deletions app/ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var IOS = function(rest, app, udid, verbose, removeTraceDir, warp, reset) {
this.curWindowHandle = null;
this.windowHandleCache = [];
this.webElementIds = [];
this.implicitWaitMs = 0;
this.capabilities = {
version: '6.0'
, webStorageEnabled: false
Expand Down Expand Up @@ -261,6 +262,28 @@ IOS.prototype.stop = function(cb) {
}
};

IOS.prototype.waitForCondition = function(waitMs, condFn, cb, intervalMs) {
if (typeof intervalMs === "undefined") {
intervalMs = 500;
}
var begunAt = Date.now();
var endAt = begunAt + waitMs;
var me = this;
var spin = function() {
condFn(function(condMet) {
var args = Array.prototype.slice.call(arguments);
if (condMet) {
cb.apply(me, args.slice(1));
} else if (Date.now() < endAt) {
setTimeout(spin, intervalMs);
} else {
cb.apply(me, args.slice(1));
}
});
};
spin();
};

IOS.prototype.setCommandTimeout = function(secs, cb) {
var cmd = "waitForDataTimeout = " + parseInt(secs, 10);
this.proxy(cmd, cb);
Expand Down Expand Up @@ -335,7 +358,8 @@ IOS.prototype.push = function(elem) {
};

IOS.prototype.findUIElementOrElements = function(strategy, selector, ctx, many, cb) {
if (_.contains(["name", "tag name", "xpath"], strategy)) {
var me = this;
var doFind = function(findCb) {
var ext = many ? 's' : '';
if (typeof ctx === "undefined" || !ctx) {
ctx = '';
Expand All @@ -353,7 +377,18 @@ IOS.prototype.findUIElementOrElements = function(strategy, selector, ctx, many,
command = ["au.getElement", ext, "ByType('", selector, "'", ctx,")"].join('');
}

this.proxy(command, cb);
me.proxy(command, function(err, res) {
if (!many && res.status === 0) {
findCb(true, err, res);
} else if (many && res.value.length > 0) {
findCb(true, err, res);
} else {
findCb(false, err, res);
}
});
};
if (_.contains(["name", "tag name", "xpath"], strategy)) {
this.waitForCondition(this.implicitWaitMs, doFind, cb);
} else {
cb(null, {
status: status.codes.UnknownError.code
Expand Down Expand Up @@ -567,9 +602,15 @@ IOS.prototype.frame = function(frame, cb) {
this.proxy(command, cb);
};

IOS.prototype.implicitWait = function(seconds, cb) {
var command = ["au.timeout('", seconds, "')"].join('');
this.proxy(command, cb);
IOS.prototype.implicitWait = function(ms, cb) {
//var command = ["au.timeout('", seconds, "')"].join('');
//this.proxy(command, cb);
this.implicitWaitMs = parseInt(ms, 10);
logger.info("Set iOS implicit wait to " + ms + "ms");
cb(null, {
status: status.codes.Success.code
, value: null
});
};

IOS.prototype.elementDisplayed = function(elementId, cb) {
Expand Down
43 changes: 28 additions & 15 deletions test/functional/testapp/timeouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,31 @@ describeWd('command timeout', function(h) {
});
});

//describeWd('check implicit wait', function(h) {
//return it('should set the implicit wait for finding elements', function(done) {
//h.driver.setImplicitWaitTimeout(10 * 1000, function(err) {
//var before = new Date().getTime() / 1000;
//console.log(before);
//h.driver.elementsByTagName('notgonnabethere', function(err, missing) {
//var after = new Date().getTime() / 1000;
//console.log(after);
//assert.ok(after - before < 12);
//assert.ok(after - before > 10);
//done();
//});
//});
//});
//});
describeWd('check implicit wait', function(h) {
it('should set the implicit wait for finding elements', function(done) {
h.driver.setImplicitWaitTimeout(10 * 1000, function(err) {
should.not.exist(err);
var before = new Date().getTime() / 1000;
h.driver.elementsByTagName('notgonnabethere', function(err, missing) {
var after = new Date().getTime() / 1000;
should.ok(after - before < 12);
should.ok(after - before > 10);
missing.length.should.equal(0);
done();
});
});
});
it('should set the implicit wait for finding element', function(done) {
h.driver.setImplicitWaitTimeout(10 * 1000, function(err) {
should.not.exist(err);
var before = new Date().getTime() / 1000;
h.driver.elementByTagName('notgonnabethere', function(err) {
var after = new Date().getTime() / 1000;
should.ok(after - before < 12);
should.ok(after - before > 10);
should.exist(err);
done();
});
});
});
});

0 comments on commit b8c2812

Please sign in to comment.