Skip to content

Commit

Permalink
Restore tainted objects in web driver on Safari
Browse files Browse the repository at this point in the history
Fixes #53. Array.prototype methods are stored in an object and then
restored after the test runs, which is wrapped with a try/catch. Some
index properties are also deleted, and any errors resulting from the
actual tests are rethrown.
  • Loading branch information
andyearnshaw committed Jan 30, 2014
1 parent 511784c commit 9d90883
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions tests/saucelabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ function gitDetailsFromFilesystem(state, done) {
config = LIBS.fs.readFileSync(LIBS.path.resolve(gitDir, 'config')).toString();
config.split('\n[').forEach(function(conf) {
var matches = conf.match(/^remote "([^"]+)"\](.|\n)*url\s*=\s*\S+github\.com[:\/]([^\/]*)\/(.+)\.git/m);
console.log(matches);
if (matches && (matches[1] === state.git.remote)) {
state.git.user = matches[3];
state.git.repo = matches[4];
Expand Down Expand Up @@ -305,7 +306,7 @@ function runTestsInBrowser(state, browserConfig, done) {
state.tests.forEach(function(test) {
tasks.push(function(taskDone) {
var url = state.git.rawURL + test;
console.log('--TESTING--', test, browserString);
console.log('--TESTING--', test, typeof browserString);

function saveResult(out, err) {
var cookedErr = err;
Expand Down Expand Up @@ -339,7 +340,40 @@ function runTestsInBrowser(state, browserConfig, done) {

browser.get(url, function() {
/*jshint evil:true*/
browser.eval('runner()', function (err, out) {
var code = 'runner()';

// Safari chokes on tests that taint Array/Object prototypes
if (browserConfig.browserName === 'safari') {
code = function () {
var err, rtn,
backup = {},
fns = ['indexOf', 'join', 'push', 'slice', 'sort'];

fns.forEach(function (fn) {
backup[fn] = Array.prototype[fn];
});

try { rtn = runner(); }
catch (e) { err = e; }

delete Array.prototype[0];
delete Object.prototype[0];
delete Object.prototype[1];

fns.forEach(function (fn) {
Array.prototype[fn] = backup[fn];
});

if (err)
throw err;

return rtn;
}.toString();

code = '(' + code + ')()';
}

browser.eval(code, function (err, out) {
saveResult(err ? null : out, err);
});
});
Expand Down

0 comments on commit 9d90883

Please sign in to comment.