Skip to content

Instantly share code, notes, and snippets.

@sebv
Created November 16, 2012 03:39
Show Gist options
  • Save sebv/4083813 to your computer and use it in GitHub Desktop.
Save sebv/4083813 to your computer and use it in GitHub Desktop.
retrieving element hangs when containing window is closed
{
"name": "bid",
"version": "0.0.0",
"description": "ERROR: No README.md file found!",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"author": "",
"license": "BSD",
"devDependencies": {
"wd": "0.0.26",
"request": "~2.12.0",
"underscore": "~1.4.2",
"async": "~0.1.22"
}
}
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, buttonEl, fooEl;
async.series([
function(done) {
//
// Opening session
//
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) {
//
// Setting implicit wait to 5000
//
var httpOpts = _(postOpts).clone();
httpOpts.url = urlBase + '/session/' + sessionId + '/timeouts/implicit_wait';
data = {
ms: 5000
};
httpOpts.body = JSON.stringify(data);
request( httpOpts, function(err, res, body) {
done(err);
});
},
function(done) {
//
// Navigating to url
//
var httpOpts = _(postOpts).clone();
httpOpts.url = urlBase + '/session/' + sessionId + '/url';
data = {
url: 'http://people.mozilla.org/~lhilaiel/hang/'
};
httpOpts.body = JSON.stringify(data);
request( httpOpts, function(err, res, body) {
done(err);
});
},
function(done) {
//
// Getting button element
//
var httpOpts = _(postOpts).clone();
httpOpts.url = urlBase + '/session/' + sessionId + '/element';
data = {
using: 'id',
value: 'theButton'
};
httpOpts.body = JSON.stringify(data);
request( httpOpts, function(err, res, body) {
body = JSON.parse(strip(body));
buttonEl = body.value.ELEMENT;
done(err);
});
},
function(done) {
//
// Clicking button to open popup
//
var httpOpts = _(postOpts).clone();
httpOpts.url = urlBase + '/session/' + sessionId + '/element/'
+ buttonEl + '/click';
data = {};
httpOpts.body = JSON.stringify(data);
request( httpOpts, function(err, res, body) {
done(err);
});
},
function(done) {
//
// Going into popup
//
var httpOpts = _(postOpts).clone();
httpOpts.url = urlBase + '/session/' + sessionId + '/window';
data = {
name: 'mypopup'
};
httpOpts.body = JSON.stringify(data);
request( httpOpts, function(err, res, body) {
done(err);
});
},
function(done) {
//
// Selecting existing element in popup => OK
//
var httpOpts = _(postOpts).clone();
httpOpts.url = urlBase + '/session/' + sessionId + '/element';
data = {
using: 'id',
value: '#foo'
};
httpOpts.body = JSON.stringify(data);
request( httpOpts, function(err, res, body) {
body = JSON.parse(strip(body));
fooEl = body.value.ELEMENT;
done(err);
});
},
function(done) {
//
// Selecting not-existing element in popup => Not OK
//
var httpOpts = _(postOpts).clone();
httpOpts.url = urlBase + '/session/' + sessionId + '/element';
data = {
using: 'id',
value: 'foo'
};
httpOpts.body = JSON.stringify(data);
request( httpOpts, function(err, res, body) {
console.log("You'll see this whenever"); // It hangs in there forever
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