Skip to content

Commit b3c9a47

Browse files
committed
Merge pull request #157 from robertknight/wombat-ms-edge-compat
Microsoft Edge compatibility fixes, including Safari fix from #156 and Node.prototype fix from #154
2 parents cd23903 + 977647c commit b3c9a47

File tree

5 files changed

+150
-63
lines changed

5 files changed

+150
-63
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ python:
77
os:
88
- linux
99

10+
addons:
11+
sauce_connect: true
12+
1013
cache:
1114
directories:
1215
- $HOME/.cache/pip
@@ -22,6 +25,10 @@ install:
2225
- pip install coverage pytest-cov coveralls --use-mirrors
2326
- npm install
2427

28+
before_script:
29+
- export DISPLAY=:99.0
30+
- sh -e /etc/init.d/xvfb start
31+
2532
script:
2633
- python setup.py test
2734
- cd karma-tests && make test

karma-tests/dummy.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head><meta charset="UTF-8"></head>
3+
<body>
4+
<!-- This is a dummy page used in
5+
tests of Wombat's live-rewriting
6+
functionality.
7+
!-->
8+
</body>
9+
</html>

karma-tests/karma.conf.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
if (!process.env['SAUCE_USERNAME'] || !process.env['SAUCE_ACCESS_KEY']) {
2-
console.error('Sauce Labs account details not set, skipping Karma tests');
3-
process.exit(0);
4-
}
5-
61
var sauceLabsConfig = {
72
testName: 'PyWB Client Tests',
83
};
@@ -15,7 +10,7 @@ if (process.env.TRAVIS_JOB_NUMBER) {
1510

1611
var WOMBAT_JS_PATH = 'pywb/static/wombat.js';
1712

18-
var customLaunchers = {
13+
var sauceLaunchers = {
1914
sl_chrome: {
2015
base: 'SauceLabs',
2116
browserName: 'chrome',
@@ -26,25 +21,37 @@ var customLaunchers = {
2621
browserName: 'firefox',
2722
},
2823

29-
/* Safari and Edge are currently broken in
30-
pywb.
31-
32-
See: https://github.com/ikreymer/pywb/issues/148 (Edge)
33-
https://github.com/ikreymer/pywb/issues/147 (Safari)
34-
3524
sl_safari: {
3625
base: 'SauceLabs',
3726
browserName: 'safari',
3827
platform: 'OS X 10.11',
3928
version: '9.0',
4029
},
30+
4131
sl_edge: {
4232
base: 'SauceLabs',
4333
browserName: 'MicrosoftEdge',
4434
},
45-
*/
4635
};
4736

37+
var localLaunchers = {
38+
localFirefox: {
39+
base: 'Firefox',
40+
},
41+
};
42+
43+
var customLaunchers = {};
44+
45+
if (process.env['SAUCE_USERNAME'] && process.env['SAUCE_ACCESS_KEY']) {
46+
customLaunchers = sauceLaunchers;
47+
} else {
48+
console.error('Sauce Labs account details not set, ' +
49+
'Karma tests will be run only against local browsers.' +
50+
'Set SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables to ' +
51+
'run tests against Sauce Labs browsers');
52+
customLaunchers = localLaunchers;
53+
}
54+
4855
module.exports = function(config) {
4956
config.set({
5057
basePath: '../',
@@ -58,6 +65,11 @@ module.exports = function(config) {
5865
included: false,
5966
served: true,
6067
},
68+
{
69+
pattern: 'karma-tests/dummy.html',
70+
included: false,
71+
served: true,
72+
},
6173
'karma-tests/*.spec.js',
6274
],
6375

@@ -75,9 +87,15 @@ module.exports = function(config) {
7587

7688
sauceLabs: sauceLabsConfig,
7789

78-
// use an extended timeout for capturing Sauce Labs
79-
// browsers in case the service is busy
90+
// Set extended timeouts to account for the slowness
91+
// in connecting to remote browsers (eg. when using
92+
// Sauce Labs)
93+
//
94+
// See https://oligofren.wordpress.com/2014/05/27/running-karma-tests-on-browserstack/
8095
captureTimeout: 3 * 60000,
96+
browserNoActivityTimeout: 30 * 1000,
97+
browserDisconnectTimeout: 10 * 1000,
98+
browserDisconnectTolerance: 1,
8199

82100
customLaunchers: customLaunchers,
83101

karma-tests/wombat.spec.js

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var WOMBAT_SRC = '../pywb/static/wombat.js';
21
var DEFAULT_TIMEOUT = 20000;
32

43
// creates a new document in an <iframe> and runs
@@ -12,7 +11,7 @@ var DEFAULT_TIMEOUT = 20000;
1211
function runWombatTest(testCase, done) {
1312
// create an <iframe>
1413
var testFrame = document.createElement('iframe');
15-
testFrame.src = '/dummy.html';
14+
testFrame.src = '/base/karma-tests/dummy.html';
1615
document.body.appendChild(testFrame);
1716

1817
testFrame.contentWindow.addEventListener('load', function () {
@@ -27,8 +26,20 @@ function runWombatTest(testCase, done) {
2726
done(new Error(ex));
2827
};
2928

30-
// expose chai assertions to the <iframe>
31-
window.assert = assert;
29+
// expose utility methods for assertion testing in tests.
30+
// (We used to expose chai asserts here but Karma's default
31+
// error reporter replaces URLs in exception messages with
32+
// the corresponding file paths, which is unhelpful for us
33+
// since assert.equal() will often be called with URLs in our tests)
34+
window.assert = {
35+
equal: function (a, b) {
36+
if (a !== b) {
37+
x.equal(a, b);
38+
console.error('Mismatch between', a, 'and', b);
39+
throw new Error('AssertionError');
40+
}
41+
}
42+
};
3243

3344
runFunctionInIFrame(function () {
3445
// re-assign the iframe's console object to the parent window's
@@ -44,8 +55,21 @@ function runWombatTest(testCase, done) {
4455
};
4556

4657
// expose chai's assertion testing API to the test script
47-
assert = window.parent.assert;
48-
reportError = window.parent.reportError;
58+
window.assert = window.parent.assert;
59+
window.reportError = window.parent.reportError;
60+
61+
// helpers which check whether DOM property overrides are supported
62+
// in the current browser
63+
window.domTests = {
64+
areDOMPropertiesConfigurable: function () {
65+
var descriptor = Object.getOwnPropertyDescriptor(Node.prototype, 'baseURI');
66+
if (descriptor && !descriptor.configurable) {
67+
return false;
68+
} else {
69+
return true;
70+
}
71+
}
72+
};
4973
});
5074

5175
try {
@@ -108,41 +132,66 @@ describe('WombatJS', function () {
108132
}, done);
109133
});
110134

111-
it('should rewrite document.baseURI', function (done) {
112-
runWombatTest({
113-
initScript: function () {
114-
wbinfo = {
115-
wombat_opts: {},
116-
prefix: window.location.origin,
117-
wombat_ts: '',
118-
};
119-
},
120-
wombatScript: wombatScript,
121-
testScript: function () {
122-
var baseURI = document.baseURI;
123-
if (typeof baseURI !== 'string') {
124-
throw new Error('baseURI is not a string');
125-
}
126-
assert.equal(baseURI, 'http:///dummy.html');
127-
},
128-
}, done);
135+
describe('anchor rewriting', function () {
136+
it('should rewrite links in dynamically injected <a> tags', function (done) {
137+
runWombatTest({
138+
initScript: function () {
139+
wbinfo = {
140+
wombat_opts: {},
141+
prefix: window.location.origin,
142+
wombat_ts: '',
143+
};
144+
},
145+
wombatScript: wombatScript,
146+
html: '<a href="foobar.html" id="link">A link</a>',
147+
testScript: function () {
148+
var link = document.getElementById('link');
149+
if (domTests.areDOMPropertiesConfigurable()) {
150+
assert.equal(link.href, 'http:///base/karma-tests/foobar.html');
151+
}
152+
},
153+
}, done);
154+
});
129155
});
130156

131-
it('should rewrite links in dynamically injected <a> tags', function (done) {
132-
runWombatTest({
133-
initScript: function () {
134-
wbinfo = {
135-
wombat_opts: {},
136-
prefix: window.location.origin,
137-
wombat_ts: '',
138-
};
139-
},
140-
wombatScript: wombatScript,
141-
html: '<a href="foobar.html" id="link">A link</a>',
142-
testScript: function () {
143-
var link = document.getElementById('link');
144-
assert.equal(link.href, 'http:///foobar.html');
145-
},
146-
}, done);
157+
describe('base URL overrides', function () {
158+
it('document.baseURI should return the original URL', function (done) {
159+
runWombatTest({
160+
initScript: function () {
161+
wbinfo = {
162+
wombat_opts: {},
163+
prefix: window.location.origin,
164+
wombat_ts: '',
165+
};
166+
},
167+
wombatScript: wombatScript,
168+
testScript: function () {
169+
var baseURI = document.baseURI;
170+
if (typeof baseURI !== 'string') {
171+
throw new Error('baseURI is not a string');
172+
}
173+
if (domTests.areDOMPropertiesConfigurable()) {
174+
assert.equal(baseURI, 'http:///base/karma-tests/dummy.html');
175+
}
176+
},
177+
}, done);
178+
});
179+
180+
it('should allow base.href to be assigned', function (done) {
181+
runWombatTest({
182+
initScript: function () {
183+
wbinfo = {
184+
wombat_opts: {},
185+
};
186+
},
187+
wombatScript: wombatScript,
188+
testScript: function () {
189+
'use strict';
190+
var baseElement = document.createElement('base');
191+
baseElement.href = 'http://foobar.com/base';
192+
assert.equal(baseElement.href, 'http://foobar.com/base');
193+
},
194+
}, done);
195+
});
147196
});
148197
});

pywb/static/wombat.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,16 @@ var wombat_internal = function($wbwindow) {
379379
}
380380

381381
try {
382-
Object.defineProperty(obj, prop, {
383-
configurable: false,
384-
set: set_func,
385-
get: get_func
386-
});
382+
var descriptor = {
383+
configurable: true,
384+
get: get_func,
385+
};
386+
387+
if (set_func) {
388+
descriptor.set = set_func;
389+
}
390+
391+
Object.defineProperty(obj, prop, descriptor);
387392

388393
return true;
389394
} catch (e) {
@@ -762,15 +767,14 @@ var wombat_internal = function($wbwindow) {
762767
def_prop($wbwindow.HTMLBaseElement.prototype, "href", undefined, base_href_get);
763768

764769
// Shared baseURI
765-
var orig_getter = get_orig_getter($wbwindow.Node, "baseURI");
770+
var orig_getter = get_orig_getter($wbwindow.Node.prototype, "baseURI");
766771
if (orig_getter) {
767772
var get_baseURI = function() {
768773
var res = orig_getter.call(this);
769774
return extract_orig(res);
770775
}
771776

772-
def_prop($wbwindow.HTMLElement.prototype, "baseURI", undefined, get_baseURI);
773-
def_prop($wbwindow.HTMLDocument.prototype, "baseURI", undefined, get_baseURI);
777+
def_prop($wbwindow.Node.prototype, "baseURI", undefined, get_baseURI);
774778
}
775779
}
776780

0 commit comments

Comments
 (0)