Skip to content

Commit

Permalink
test: Run a minimal set of tests from a test extension running on Ch…
Browse files Browse the repository at this point in the history
…rome (mozilla#66)

* test: Run a minimal set of integration/smoke tests on Chrome
* chore: minor tweaks to dependencies version in the package.json
  • Loading branch information
rpl authored Oct 23, 2017
1 parent 8914541 commit 49ce6ef
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"root": true,

"parser": "babel-eslint",

"parserOptions": {
"ecmaVersion": 6,
},
Expand Down Expand Up @@ -429,7 +432,7 @@
"spaced-comment": [2, "always"],

// Require "use strict" to be defined globally in the script.
"strict": [2, "global"],
"strict": [0, "global"],

// Allow vars to be declared anywhere in the scope.
"vars-on-top": 0,
Expand Down
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ script:
- echo "RE-RUN tests on the webpack and browserify bundled files" &&
npm install -g browserify webpack &&
./test/run-module-bundlers-smoketests.sh
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- echo "RUN integration tests on chrome" &&
./test/run-chrome-smoketests.sh

after_script: npm run publish-coverage

Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
},
"homepage": "https://github.com/mozilla/webextension-polyfill",
"devDependencies": {
"async-wait-until": "^1.1.5",
"babel-eslint": "^8.0.1",
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
"babel-preset-babili": "0.0.10",
"babel-preset-babili": "^0.0.10",
"babel-preset-es2017": "^6.24.1",
"chai": "^3.5.0",
"eslint": "3.9.1",
"eslint": "^3.9.1",
"finalhandler": "^1.1.0",
"grunt": "^1.0.1",
"grunt-babel": "^6.0.0",
"grunt-contrib-concat": "^1.0.1",
Expand All @@ -31,6 +35,8 @@
"jsdom": "^9.6.0",
"mocha": "^3.1.0",
"nyc": "^8.3.1",
"puppeteer": "^0.10.2",
"serve-static": "^1.13.1",
"sinon": "^1.17.6"
},
"nyc": {
Expand All @@ -47,6 +53,7 @@
"publish-coverage": "grunt coveralls",
"test": "mocha",
"test-coverage": "COVERAGE=y nyc mocha",
"test-minified": "TEST_MINIFIED_POLYFILL=1 mocha"
"test-minified": "TEST_MINIFIED_POLYFILL=1 mocha",
"test-integration": "mocha -r test/mocha-babel test/integration/test-*"
}
}
10 changes: 10 additions & 0 deletions test/fixtures/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE>
<html>
<head>
<title>Browser Polyfill Test Page</title>
<meta charset="utf-8">
</head>
<body>
<h1>Browser Polyfill Test Page</h1>
</body>
</html>
11 changes: 11 additions & 0 deletions test/fixtures/runtime-messaging-extension/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {name} = browser.runtime.getManifest();

console.log(name, "background page loaded");

browser.runtime.onMessage.addListener((msg, sender) => {
console.log(name, "background received msg", {msg, sender});

return Promise.resolve("background page reply");
});

console.log(name, "background page ready to receive a content script message...");
9 changes: 9 additions & 0 deletions test/fixtures/runtime-messaging-extension/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {name} = browser.runtime.getManifest();

console.log(name, "content script loaded");

browser.runtime.sendMessage("content script message").then(reply => {
console.log(name, "content script received reply", {reply});
});

console.log(name, "content script message sent");
24 changes: 24 additions & 0 deletions test/fixtures/runtime-messaging-extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"manifest_version": 2,
"name": "test-extension-runtime-messaging",
"version": "0.1",
"description": "test-extension-runtime-messaging",
"content_scripts": [
{
"matches": [
"http://localhost/*"
],
"js": [
"browser-polyfill.js",
"content.js"
]
}
],
"permissions": [],
"background": {
"scripts": [
"browser-polyfill.js",
"background.js"
]
}
}
21 changes: 21 additions & 0 deletions test/integration/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const finalhandler = require("finalhandler");
const http = require("http");
const serveStatic = require("serve-static");

exports.createHTTPServer = async (path) => {
var serve = serveStatic(path);

var server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res));
});

return new Promise((resolve, reject) => {
server.listen((err) => {
if (err) {
reject(err);
} else {
resolve(server);
}
});
});
};
92 changes: 92 additions & 0 deletions test/integration/test-runtime-messaging-on-chrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use strict";

const path = require("path");

const waitUntil = require("async-wait-until");
const {deepEqual} = require("chai").assert;
const puppeteer = require("puppeteer");

const {createHTTPServer} = require("./setup");

const fixtureExtensionDirName = "runtime-messaging-extension";

const extensionName = require(`../fixtures/${fixtureExtensionDirName}/manifest.json`).name;

describe("browser.runtime.onMessage/sendMessage", function() {
this.timeout(10000);

it("works as expected on Chrome", async () => {
const server = await createHTTPServer(path.join(__dirname, "..", "fixtures"));

const url = `http://localhost:${server.address().port}`;

const browser = await puppeteer.launch({
// Chrome Extensions are not currently supported in headless mode.
headless: false,

// Custom chrome arguments.
args: [
`--load-extension=${process.env.TEST_EXTENSIONS_PATH}/${fixtureExtensionDirName}`,
],
});

const page = await browser.newPage();

const pageConsoleMessages = [];
const pageErrors = [];

page.on("console", (...args) => {
pageConsoleMessages.push(args);
});

page.on("error", (error) => {
pageErrors.push(error);
});

await page.goto(url);

const expectedConsoleMessages = [
[extensionName, "content script loaded"],
[extensionName, "content script message sent"],
[extensionName, "content script received reply", {"reply": "background page reply"}],
];

const lastExpectedMessage = expectedConsoleMessages.slice(-1).pop();

let unexpectedException;

try {
// Wait until the last expected message has been received.
await waitUntil(() => {
return pageConsoleMessages.filter((msg) => {
return msg[0] === lastExpectedMessage[0] && msg[1] === lastExpectedMessage[1];
}).length > 0;
}, 5000);
} catch (error) {
// Collect any unexpected exception (e.g. a timeout error raised by waitUntil),
// it will be part of the deepEqual assertion of the results.
unexpectedException = error;
}

let actualResults = {
consoleMessages: pageConsoleMessages,
unexpectedException,
};

let expectedResults = {
consoleMessages: expectedConsoleMessages,
unexpectedException: undefined,
};

try {
deepEqual(actualResults, expectedResults, "Got the expected results");
} finally {
// ensure that we close the browser and the test HTTP server before exiting
// the test, even when the assertions fails.
await Promise.all([
browser.close(),
new Promise(resolve => server.close(resolve)),
]);
}
});
});
3 changes: 3 additions & 0 deletions test/mocha-babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("babel-core/register")({
presets: ["es2017"],
});
21 changes: 21 additions & 0 deletions test/run-chrome-smoketests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
echo "\nTest webextension-polyfill from an extension running on chrome"
echo "==============================================="

export TEST_EXTENSIONS_PATH=/tmp/browser-polyfill-chrome-smoketests

MARKER_FILE=$TEST_EXTENSIONS_PATH/.created-for-run-chrome-smoketests

# Check if the marker file exists and then remove the directory.
if [ -f $MARKER_FILE ]; then
rm -fr $TEST_EXTENSIONS_PATH
fi

## Exits immediately if the directory already exists (which can only happen in a local
## development environment, while this test will usually run on travis).
mkdir $TEST_EXTENSIONS_PATH || exit 1
touch $MARKER_FILE

cp -rf test/fixtures/runtime-messaging-extension $TEST_EXTENSIONS_PATH
cp -rf dist/browser-polyfill.js* $TEST_EXTENSIONS_PATH/runtime-messaging-extension/

npm run test-integration

0 comments on commit 49ce6ef

Please sign in to comment.