Created
November 14, 2012 14:57
-
-
Save sebv/4072575 to your computer and use it in GitHub Desktop.
example producing dirty selenium errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "example", | |
"version": "0.0.1", | |
"dependencies": { | |
"async": "~0.1.22", | |
"request": "~2.12.0", | |
"underscore": "~1.4.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'), | |
_ = require('underscore'), | |
async = require('async'); | |
// | |
// method used in wd to clean Selenium responses | |
// | |
var strip = function strip(str) { | |
if(typeof(str) !== 'string') { return str; } | |
var x = []; | |
_(str.length).times(function(i) { | |
if (str.charCodeAt(i)) { | |
x.push(str.charAt(i)); | |
} | |
}); | |
return x.join(''); | |
}; | |
var urlBase = 'http://localhost:4444/wd/hub'; | |
var getOpts = { | |
method: 'GET', | |
Connection: 'keep-alive', | |
headers: { | |
Accept: 'application/json', | |
} | |
}; | |
var postOpts = _(getOpts).clone(); | |
postOpts.method = 'POST'; | |
postOpts.headers['Content-Type'] = 'application/json; charset=UTF-8'; | |
var sessionId; | |
async.series([ | |
function(done) { | |
// | |
// init | |
// | |
var httpOpts = _(postOpts).clone(); | |
httpOpts.url = urlBase + '/session'; | |
data = { | |
desiredCapabilities:{ | |
browserName: 'firefox' | |
, version: '' | |
, javascriptEnabled: true | |
, platform: 'ANY' | |
} | |
}; | |
httpOpts.body = JSON.stringify(data); | |
request( httpOpts, function(err, res, body) { | |
var locationArr = res.headers.location.split('/'); | |
sessionId = locationArr[locationArr.length - 1]; | |
console.log("sessionId:", sessionId); | |
done(err); | |
}); | |
}, | |
function(done) { | |
// | |
// get | |
// | |
var httpOpts = _(postOpts).clone(); | |
httpOpts.url = urlBase + '/session/' + sessionId + '/url'; | |
data = { | |
url: 'http://www.google.com' | |
}; | |
httpOpts.body = JSON.stringify(data); | |
request( httpOpts, function(err, res, body) { | |
done(err); | |
}); | |
}, | |
function(done) { | |
// | |
// trying to retrieve inexistant element | |
// | |
var httpOpts = _(postOpts).clone(); | |
httpOpts.url = urlBase + '/session/' + sessionId + '/element'; | |
data = { | |
using: 'id', | |
value: 'it_doesnt_exists' | |
}; | |
httpOpts.body = JSON.stringify(data); | |
request( httpOpts, function(err, res, body) { | |
//console.log(body); | |
console.log(body.substring(0,500) + '...'); | |
try{ | |
JSON.parse(body); // fails with: [SyntaxError: Unexpected token ] | |
} catch(err2) { | |
console.log( "Without stripping:", err2); | |
} | |
try{ | |
JSON.parse(strip(body)); // works | |
} catch(err3) { | |
console.log("After stripping:", err3); | |
} | |
done(err); | |
}); | |
} | |
], function(err) { | |
if(err) { console.log(err); } | |
// | |
// quit | |
// | |
console.log('quitting'); | |
var httpOpts = {}; | |
httpOpts.method = 'DELETE'; | |
httpOpts.url = urlBase + '/session/' + sessionId; | |
request(httpOpts, function(err) {}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment