Skip to content

Commit

Permalink
Fix webdriver for firefox
Browse files Browse the repository at this point in the history
This works around an issue in the mozilla geckodriver:
mozilla/geckodriver#1798
  • Loading branch information
mantoni committed Sep 6, 2021
1 parent ef2bc49 commit 85e1eec
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
8 changes: 8 additions & 0 deletions mochify/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ function MochifyReporter(runner) {
mocha.reporter(MochifyReporter);
mocha.ui(/* MOCHIFY_UI */);
mocha.timeout(/* MOCHIFY_TIMEOUT */);

// Workaround for https://github.com/mozilla/geckodriver/issues/1798
if (typeof globalThis !== 'undefined' && globalThis !== window) {
// Register globals on window. Mocha uses globalThis, if defined.
window.mocha = mocha;
mocha.suite.emit('pre-require', window, null, mocha);
}

mocha.mochify_pollEvents = pollEvents;

var chunks = [];
Expand Down
6 changes: 4 additions & 2 deletions mochify/lib/inject-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ async function injectScript(driver, script) {
chunk = script;
script = null;
}
await driver.evaluate(`mocha.mochify_receive(${JSON.stringify(chunk)})`);
await driver.evaluate(
`window.mocha.mochify_receive(${JSON.stringify(chunk)})`
);
} while (script);
await driver.evaluate(`mocha.mochify_run()`);
await driver.evaluate(`window.mocha.mochify_run()`);
}
13 changes: 8 additions & 5 deletions mochify/lib/inject-script.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('mochify/lib/inject-script', () => {

assert.calledOnceWith(
driver.evaluate,
'mocha.mochify_receive("console.log(\\"Hi!\\")")'
'window.mocha.mochify_receive("console.log(\\"Hi!\\")")'
);
});

Expand All @@ -24,7 +24,7 @@ describe('mochify/lib/inject-script', () => {

await assert.resolves(promise);
assert.calledTwice(driver.evaluate);
assert.calledWith(driver.evaluate, 'mocha.mochify_run()');
assert.calledWith(driver.evaluate, 'window.mocha.mochify_run()');
});

it('splits script into chunks and invokes mochify_receive twice', async () => {
Expand All @@ -41,8 +41,11 @@ describe('mochify/lib/inject-script', () => {
assert.calledWith(script.substring, 0, MAX_SCRIPT_CHUNK);
assert.calledWith(script.substring, MAX_SCRIPT_CHUNK);
assert.calledThrice(driver.evaluate);
assert.calledWith(driver.evaluate, 'mocha.mochify_receive("first")');
assert.calledWith(driver.evaluate, 'mocha.mochify_receive("second")');
assert.calledWith(driver.evaluate, 'mocha.mochify_run()');
assert.calledWith(driver.evaluate, 'window.mocha.mochify_receive("first")');
assert.calledWith(
driver.evaluate,
'window.mocha.mochify_receive("second")'
);
assert.calledWith(driver.evaluate, 'window.mocha.mochify_run()');
});
});
4 changes: 3 additions & 1 deletion mochify/lib/poll-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ exports.pollEvents = pollEvents;
function pollEvents(driver, emit) {
return new Promise((resolve) => {
async function doPoll() {
const events = await driver.evaluateReturn('mocha.mochify_pollEvents()');
const events = await driver.evaluateReturn(
'window.mocha.mochify_pollEvents()'
);
if (!events) {
setImmediate(doPoll);
return;
Expand Down

0 comments on commit 85e1eec

Please sign in to comment.