Permalink
Browse files

Added Client#session() chaining support. Closes #4

  • Loading branch information...
1 parent 0a1594e commit faa56fee560b96cf57cea74c47d4a3331d1b885e @tj tj committed Sep 6, 2010
Showing with 27 additions and 21 deletions.
  1. +12 −14 examples/google.js
  2. +15 −7 lib/soda/client.js
View
@@ -27,17 +27,15 @@ var browser = soda.createClient({
// });
// });
-browser.session(function(){
- browser
- .chain
- .open('/')
- .type('q', 'Hello World')
- .clickAndWait('btnG')
- .assertTitle('Hello World - Google Search')
- .testComplete()
- .done(function(err, res){
- if (err) throw err;
- console.log('done');
- });
-});
-
+browser
+ .chain
+ .session()
+ .open('/')
+ .type('q', 'Hello World')
+ .clickAndWait('btnG')
+ .assertTitle('Hello World - Google Search')
+ .testComplete()
+ .done(function(err, res){
+ if (err) throw err;
+ console.log('done');
+ });
View
@@ -37,19 +37,25 @@ var Client = exports = module.exports = function Client(options) {
/**
* Initialize a new session, then callback `fn(err, sid)`
*
- * @param {Type} name
- * @return {Type}
+ * @param {Function} fn
+ * @return {Client}
* @api public
*/
Client.prototype.session = function(fn){
var self = this;
if (!this.browser) throw new Error('browser required');
if (!this.url) throw new Error('browser url required');
- this.command('getNewBrowserSession', [this.browser, this.url], function(err, res){
- if (err) return fn(err);
- fn(null, self.sid = res.body);
- });
+ if (this.queue) {
+ return this.enqueue('getNewBrowserSession', [this.browser, this.url], function(err, res){
+ self.sid = res.body;
+ });
+ } else {
+ this.command('getNewBrowserSession', [this.browser, this.url], function(err, res){
+ if (err) return fn(err);
+ fn(null, self.sid = res.body);
+ });
+ }
};
/**
@@ -154,10 +160,11 @@ Client.prototype.done = function(fn){
* @api private
*/
-Client.prototype.enqueue = function(cmd, args){
+Client.prototype.enqueue = function(cmd, args, fn){
var self = this;
this.queue.push(function(){
self.command(cmd, args, function(err, res){
+ fn && fn(err, res);
if (err) {
self.done(err, res);
} else if (self.queue.length) {
@@ -202,6 +209,7 @@ exports.commands = [
, 'dragAndDrop'
, 'fireEvent'
, 'goBack'
+ , 'getNewBrowserSession'
, 'keyDown'
, 'keyPress'
, 'keyUp'

0 comments on commit faa56fe

Please sign in to comment.