1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +01:00

Merge pull request #152 from robertknight/wombatjs-tests

Wombatjs tests
This commit is contained in:
Ilya Kreymer 2015-11-26 00:25:49 -08:00
commit 26684b0973
7 changed files with 290 additions and 2 deletions

3
.editorconfig Normal file
View File

@ -0,0 +1,3 @@
[*.js]
indent_style=space
indent_size=4

3
.gitignore vendored
View File

@ -41,3 +41,6 @@ nosetests.xml
.pydevproject
.vagrant
# Node
node_modules/

View File

@ -6,7 +6,11 @@ python:
os:
- linux
# - osx
cache:
directories:
- $HOME/.cache/pip
- node_modules
sudo: false
@ -16,9 +20,11 @@ install:
- pip install git+https://github.com/esnme/ultrajson.git
- python setup.py -q install
- pip install coverage pytest-cov coveralls --use-mirrors
- npm install
script:
python setup.py test
- python setup.py test
- cd karma-tests && make test
after_success:
coveralls

4
karma-tests/Makefile Normal file
View File

@ -0,0 +1,4 @@
NODE_BIN_DIR=../node_modules/.bin
test:
$(NODE_BIN_DIR)/karma start --single-run

90
karma-tests/karma.conf.js Normal file
View File

@ -0,0 +1,90 @@
if (!process.env['SAUCE_USERNAME'] || !process.env['SAUCE_ACCESS_KEY']) {
console.error('Sauce Labs account details not set, skipping Karma tests');
process.exit(0);
}
var sauceLabsConfig = {
testName: 'PyWB Client Tests',
};
// see https://github.com/karma-runner/karma-sauce-launcher/issues/73
if (process.env.TRAVIS_JOB_NUMBER) {
sauceLabsConfig.startConnect = false;
sauceLabsConfig.tunnelIdentifier = process.env.TRAVIS_JOB_NUMBER;
}
var WOMBAT_JS_PATH = 'pywb/static/wombat.js';
var customLaunchers = {
sl_chrome: {
base: 'SauceLabs',
browserName: 'chrome',
},
sl_firefox: {
base: 'SauceLabs',
browserName: 'firefox',
},
/* Safari and Edge are currently broken in
pywb.
See: https://github.com/ikreymer/pywb/issues/148 (Edge)
https://github.com/ikreymer/pywb/issues/147 (Safari)
sl_safari: {
base: 'SauceLabs',
browserName: 'safari',
platform: 'OS X 10.11',
version: '9.0',
},
sl_edge: {
base: 'SauceLabs',
browserName: 'MicrosoftEdge',
},
*/
};
module.exports = function(config) {
config.set({
basePath: '../',
frameworks: ['mocha', 'chai'],
files: [
{
pattern: WOMBAT_JS_PATH,
watched: true,
included: false,
served: true,
},
'karma-tests/*.spec.js',
],
preprocessors: {},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
sauceLabs: sauceLabsConfig,
// use an extended timeout for capturing Sauce Labs
// browsers in case the service is busy
captureTimeout: 3 * 60000,
customLaunchers: customLaunchers,
browsers: Object.keys(customLaunchers),
singleRun: false,
concurrency: Infinity
})
};

148
karma-tests/wombat.spec.js Normal file
View File

@ -0,0 +1,148 @@
var WOMBAT_SRC = '../pywb/static/wombat.js';
var DEFAULT_TIMEOUT = 20000;
// creates a new document in an <iframe> and runs
// a WombatJS test case in it.
//
// A new <iframe> is used for each test so that each
// case is run with fresh Document and Window objects,
// since Wombat monkey-patches many Document and Window
// functions
//
function runWombatTest(testCase, done) {
// create an <iframe>
var testFrame = document.createElement('iframe');
testFrame.src = '/dummy.html';
document.body.appendChild(testFrame);
testFrame.contentWindow.addEventListener('load', function () {
var testDocument = testFrame.contentDocument;
function runFunctionInIFrame(func) {
testFrame.contentWindow.eval('(' + func.toString() + ')()');
}
// expose an error reporting function to the <iframe>
window.reportError = function(ex) {
done(new Error(ex));
};
// expose chai assertions to the <iframe>
window.assert = assert;
runFunctionInIFrame(function () {
// re-assign the iframe's console object to the parent window's
// console so that messages are intercepted by Karma
// and output to wherever it is configured to send
// console logs (typically stdout)
console = window.parent.console;
window.onerror = function (message, url, line, col, error) {
if (error) {
console.log(error.stack);
}
reportError(new Error(message));
};
// expose chai's assertion testing API to the test script
assert = window.parent.assert;
reportError = window.parent.reportError;
});
try {
runFunctionInIFrame(testCase.initScript);
} catch (e) {
throw new Error('Configuring Wombat failed: ' + e.toString());
}
try {
testFrame.contentWindow.eval(testCase.wombatScript);
runFunctionInIFrame(function () {
new window._WBWombat(wbinfo);
});
} catch (e) {
throw new Error('Initializing WombatJS failed: ' + e.toString());
}
if (testCase.html) {
testDocument.body.innerHTML = testCase.html;
}
if (testCase.testScript) {
try {
runFunctionInIFrame(testCase.testScript);
} catch (e) {
throw new Error('Test script failed: ' + e.toString());
}
}
testFrame.remove();
done();
});
}
describe('WombatJS', function () {
this.timeout(DEFAULT_TIMEOUT);
var wombatScript;
before(function (done) {
// load the source of the WombatJS content
// rewriting script
var req = new XMLHttpRequest();
req.open('GET', '/base/pywb/static/wombat.js');
req.onload = function () {
wombatScript = req.responseText;
done();
};
req.send();
});
it('should load', function (done) {
runWombatTest({
initScript: function () {
wbinfo = {
wombat_opts: {},
};
},
wombatScript: wombatScript,
}, done);
});
it('should rewrite document.baseURI', function (done) {
runWombatTest({
initScript: function () {
wbinfo = {
wombat_opts: {},
prefix: window.location.origin,
wombat_ts: '',
};
},
wombatScript: wombatScript,
testScript: function () {
var baseURI = document.baseURI;
if (typeof baseURI !== 'string') {
throw new Error('baseURI is not a string');
}
assert.equal(baseURI, 'http:///dummy.html');
},
}, done);
});
it('should rewrite links in dynamically injected <a> tags', function (done) {
runWombatTest({
initScript: function () {
wbinfo = {
wombat_opts: {},
prefix: window.location.origin,
wombat_ts: '',
};
},
wombatScript: wombatScript,
html: '<a href="foobar.html" id="link">A link</a>',
testScript: function () {
var link = document.getElementById('link');
assert.equal(link.href, 'http:///foobar.html');
},
}, done);
});
});

34
package.json Normal file
View File

@ -0,0 +1,34 @@
{
"name": "pywb",
"version": "1.0.0",
"description": "Web archival replay tools",
"main": "index.js",
"directories": {
"doc": "doc",
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ikreymer/pywb.git"
},
"author": "",
"license": "GPL-3.0",
"bugs": {
"url": "https://github.com/ikreymer/pywb/issues"
},
"homepage": "https://github.com/ikreymer/pywb#readme",
"devDependencies": {
"chai": "^3.4.1",
"karma": "^0.13.15",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^0.2.1",
"karma-firefox-launcher": "^0.1.7",
"karma-html2js-preprocessor": "^0.1.0",
"karma-mocha": "^0.2.1",
"karma-sauce-launcher": "^0.3.0",
"mocha": "^2.3.4"
}
}