forked from mozilla/webextension-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Run a minimal set of tests from a test extension running on Ch…
…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
Showing
11 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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..."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
]); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require("babel-core/register")({ | ||
presets: ["es2017"], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |