Skip to content
Browse files

Added Client#session()

  • Loading branch information...
1 parent 7a795cf commit bcd4319eafbe7b83d94ede2d83b02a9fb5a7f8a3 @tj tj committed
Showing with 27 additions and 5 deletions.
  1. +7 −4 examples/google.js
  2. +20 −1 lib/selenium/client.js
View
11 examples/google.js
@@ -5,12 +5,15 @@
var selenium = require('../index');
-var client = selenium.createClient({
+var browser = selenium.createClient({
host: 'localhost',
port: 4444,
- url: 'http://www.google.com'
+ url: 'http://www.google.com',
+ browser: '*firefox'
});
-client.command('getNewBrowserSession', ['*firefox', 'http://google.com'], function(err, res){
- console.dir(res.body)
+browser.session(function(err){
+ browser.command('open', ['/'], function(err, res){
+ console.dir(res.body)
+ });
});
View
21 lib/selenium/client.js
@@ -32,6 +32,17 @@ var Client = exports = module.exports = function Client(options) {
this.url = options.url;
};
+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);
+ self.sid = res.body;
+ fn();
+ });
+};
+
/**
* Execute the given `cmd` / `args`, then callback `fn(err, res)`.
*
@@ -42,10 +53,18 @@ var Client = exports = module.exports = function Client(options) {
*/
Client.prototype.command = function(cmd, args, fn){
+ // HTTP client
var client = http.createClient(this.port, this.host);
+
+ // Path construction
+ var path = this.commandPath(cmd, args);
+ if (this.sid) path += '&sessionId=' + this.sid;
+
+ // Request
var req = client.request('GET'
- , this.commandPath(cmd, args)
+ , path
, { Host: this.host + (this.port ? ':' + this.port : '') });
+
req.on('response', function(res){
res.body = '';
res.setEncoding('utf8');

0 comments on commit bcd4319

Please sign in to comment.
Something went wrong with that request. Please try again.