Use WebSocket for message proxy

This commit is contained in:
hensm
2019-01-12 06:17:28 +00:00
parent 8f6770e8a0
commit 626b8ca75e
8 changed files with 179 additions and 200 deletions

View File

@@ -25,7 +25,6 @@
"@babel/plugin-transform-runtime": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/register": "^7.0.0",
"glob": "^7.1.3",
"minimist": "^1.2.0",
"mustache": "^3.0.1",
"pkg": "^4.3.5"

15
package-lock.json generated
View File

@@ -17,6 +17,12 @@
"color-convert": "^1.9.0"
}
},
"async-limiter": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz",
"integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==",
"dev": true
},
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@@ -465,6 +471,15 @@
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true
},
"ws": {
"version": "6.1.2",
"resolved": "https://registry.npmjs.org/ws/-/ws-6.1.2.tgz",
"integrity": "sha512-rfUqzvz0WxmSXtJpPMX2EeASXabOrSMk1ruMOV3JBTBjo4ac2lDjGGsbQSyxj8Odhw5fBib8ZKEjDNvgouNKYw==",
"dev": true,
"requires": {
"async-limiter": "~1.0.0"
}
},
"xml2js": {
"version": "0.4.19",
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.19.tgz",

View File

@@ -15,8 +15,10 @@
},
"devDependencies": {
"fs-extra": "^7.0.1",
"glob": "^7.1.3",
"jasmine-console-reporter": "^3.1.0",
"selenium-webdriver": "^4.0.0-alpha.1",
"semver": "^5.6.0"
"semver": "^5.6.0",
"ws": "^6.1.2"
}
}

View File

@@ -6,27 +6,13 @@
<script src="lib/jasmine-3.3.0/jasmine.js"></script>
<script src="lib/jasmine-3.3.0/jasmine-html.js"></script>
<script src="lib/jasmine-3.3.0/boot.js"></script>
<!-- Reporter proxy -->
<script src="reporterProxy.js"></script>
<!-- Custom setup -->
<script src="messageProxy.js"></script>
<script src="customBoot.js"></script>
<!-- Cast API -->
<script src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"></script>
<!-- Spec files -->
<script src="spec/shim/chrome.spec.js"></script>
<script src="spec/shim/cast/ApiConfig.spec.js"></script>
<script src="spec/shim/cast/DialRequest.spec.js"></script>
<script src="spec/shim/cast/Error.spec.js"></script>
<script src="spec/shim/cast/Image.spec.js"></script>
<script src="spec/shim/cast/Receiver.spec.js"></script>
<script src="spec/shim/cast/ReceiverDisplayStatus.spec.js"></script>
<script src="spec/shim/cast/SenderApplication.spec.js"></script>
<script src="spec/shim/cast/Session.spec.js"></script>
<script src="spec/shim/cast/SessionRequest.spec.js"></script>
<script src="spec/shim/cast/Timeout.spec.js"></script>
<script src="spec/shim/cast/Volume.spec.js"></script>
</head>
<body>
</body>

73
test/customBoot.js Normal file
View File

@@ -0,0 +1,73 @@
"use strict";
window.jasmine = jasmineRequire.core(jasmineRequire);
jasmineRequire.html(jasmine);
const env = jasmine.getEnv();
// Copy to window
Object.assign(window, jasmineRequire.interface(jasmine, env));
// Create query string
const queryString = new jasmine.QueryString({
getWindowLocation () {
return window.location;
}
});
// If spec is present in the query string
const filterSpecs = !!queryString.getParam("spec");
// Create HTML reporter
const htmlReporter = new jasmine.HtmlReporter({
env
, filterSpecs
, timer: new jasmine.Timer()
, getContainer () {
return document.body;
}
// Bound functions
, navigateWithNewParam: queryString.navigateWithNewParam.bind(queryString)
, addToExistingQueryString: queryString.fullStringWithNewParam.bind(queryString)
, createElement: document.createElement.bind(document)
, createTextNode: document.createTextNode.bind(document)
});
// Create spec filter
const specFilter = new jasmine.HtmlSpecFilter({
filterString () {
return queryString.getParam("spec");
}
});
// Add reporters
env.addReporter(jsApiReporter);
env.addReporter(htmlReporter);
// Configure Env
env.configure({
failFast : queryString.getParam("failFast")
, hideDisabled : queryString.getParam("hideDisabled")
, oneFailurePerSpec : queryString.getParam("oneFailurePerSpec")
, random : queryString.getParam("random")
, seed : queryString.getParam("seed")
, specFilter (spec) {
return specFilter.matches(spec.getFullName());
}
});
window.addEventListener("load", () => {
htmlReporter.initialize();
// Use messageProxy socket to request spec injection
window.sendMessage({
subject: "injectSpecs"
});
});

View File

@@ -1,10 +1,11 @@
"use strict";
const path = require("path");
const glob = require("glob");
const fs = require("fs");
const EventEmitter = require("events");
const glob = require("glob");
const WebSocket = require("ws");
const JasmineConsoleReporter = require("jasmine-console-reporter");
const webdriver = require("selenium-webdriver");
@@ -86,41 +87,72 @@ function waitUntilDefined (
* from Jasmine in the browser context to node context to use
* reporters here.
*
* reporter.js is loaded on the test page and adds reporter
* messages to the DOM which we listen for here.
* Creates a WebSocket server. messageProxy.js creates a client
* socket to this server and facilitates a communication
* channel.
*/
class ReporterProxy extends EventEmitter {
constructor (driver) {
class MessageProxy extends EventEmitter {
constructor () {
super();
this._driver = driver;
this._wait();
}
async _wait () {
const elementFilter = By.id("__msg");
// Wait for message element
await this._driver.wait(until.elementLocated(elementFilter))
// Get message content
const messageElement = this._driver.findElement(elementFilter);
const messageContent = JSON.parse(await messageElement.getText());
// Remove message element
await this._driver.executeScript(() => {
document.getElementById("__msg").remove();
const wss = new WebSocket.Server({
port: 8080
});
// Send event
this.emit(messageContent.subject, messageContent.data);
// Wait for next event
if (messageContent.subject !== "jasmineDone") {
this._wait();
}
wss.on("connection", socket => {
socket.on("message", message => {
const messageContent = JSON.parse(message);
this.emit(messageContent.subject, messageContent.data);
});
})
}
}
/**
* Ensures cast API is loaded before finding and injecting spec
* file scripts into the test page.
*/
async function injectSpecs (driver) {
const [ loaded, errorInfo ] = await driver.executeAsyncScript(() => {
const callback = arguments[arguments.length - 1];
// If already loaded, return immediately
if (chrome.cast !== undefined) {
callback([ true ]);
}
// Set Cast API callback
window.__onGCastApiAvailable = function (...args) {
callback(args);
};
});
// Return error if API unavailable
if (!loaded) {
console.error("Failed to load cast API", errorInfo);
return;
}
// Get spec files
const specFiles = glob.sync("spec/**/*.spec.js", {
cwd: __dirname
});
// Inject spec files
for (const specFile of specFiles) {
await driver.executeScript(`
const scriptElement = document.createElement("script");
scriptElement.src = "${specFile}";
document.head.appendChild(scriptElement);
`);
}
// Trigger Jasmine spec execution
await driver.executeScript(() => {
jasmine.getEnv().execute();
});
}
(async () => {
const driver = new webdriver.Builder()
@@ -140,24 +172,28 @@ class ReporterProxy extends EventEmitter {
}
// Create console reporter and reporter proxy.
// Create console reporter and message proxy.
const reporter = new JasmineConsoleReporter();
const reporterProxy = new ReporterProxy(driver);
const messageProxy = new MessageProxy();
// Inject specs when ready
messageProxy.on("injectSpecs", () => {
injectSpecs(driver);
});
/**
* Forward events from Jasmine standlone via reporter proxy to
* Forward events from Jasmine standlone via message proxy to
* console reporter.
*/
reporterProxy.on("jasmineDone" , result => reporter.jasmineDone(result));
reporterProxy.on("jasmineStarted" , result => reporter.jasmineStarted(result));
reporterProxy.on("specDone" , result => reporter.specDone(result));
reporterProxy.on("specStarted" , result => reporter.specStarted(result));
reporterProxy.on("suiteDone" , result => reporter.suiteDone(result));
reporterProxy.on("suiteStarted" , result => reporter.suiteStarted(result));
messageProxy.on("jasmineDone" , result => reporter.jasmineDone(result));
messageProxy.on("jasmineStarted" , result => reporter.jasmineStarted(result));
messageProxy.on("specDone" , result => reporter.specDone(result));
messageProxy.on("specStarted" , result => reporter.specStarted(result));
messageProxy.on("suiteDone" , result => reporter.suiteDone(result));
messageProxy.on("suiteStarted" , result => reporter.suiteStarted(result));
// Load Jasmine test page
await driver.get(TEST_PAGE_URL);
driver.get(TEST_PAGE_URL);
})();
// Keep process alive

View File

@@ -1,136 +0,0 @@
/**
Starting with version 2.0, this file "boots" Jasmine, performing all of the necessary initialization before executing the loaded environment and all of a project's specs. This file should be loaded after `jasmine.js` and `jasmine_html.js`, but before any project source files or spec files are loaded. Thus this file can also be used to customize Jasmine for a project.
If a project is using Jasmine via the standalone distribution, this file can be customized directly. If a project is using Jasmine via the [Ruby gem][jasmine-gem], this file can be copied into the support directory via `jasmine copy_boot_js`. Other environments (e.g., Python) will have different mechanisms.
The location of `boot.js` can be specified and/or overridden in `jasmine.yml`.
[jasmine-gem]: http://github.com/pivotal/jasmine-gem
*/
(function() {
/**
* ## Require &amp; Instantiate
*
* Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
*/
window.jasmine = jasmineRequire.core(jasmineRequire);
/**
* Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
*/
jasmineRequire.html(jasmine);
/**
* Create the Jasmine environment. This is used to run all specs in a project.
*/
var env = jasmine.getEnv();
/**
* ## The Global Interface
*
* Build up the functions that will be exposed as the Jasmine public interface. A project can customize, rename or alias any of these functions as desired, provided the implementation remains unchanged.
*/
var jasmineInterface = jasmineRequire.interface(jasmine, env);
/**
* Add all of the Jasmine global/public interface to the global scope, so a project can use the public interface directly. For example, calling `describe` in specs instead of `jasmine.getEnv().describe`.
*/
extend(window, jasmineInterface);
/**
* ## Runner Parameters
*
* More browser specific code - wrap the query string in an object and to allow for getting/setting parameters from the runner user interface.
*/
var queryString = new jasmine.QueryString({
getWindowLocation: function() { return window.location; }
});
var filterSpecs = !!queryString.getParam("spec");
var config = {
failFast: queryString.getParam("failFast"),
oneFailurePerSpec: queryString.getParam("oneFailurePerSpec"),
hideDisabled: queryString.getParam("hideDisabled")
};
var random = queryString.getParam("random");
if (random !== undefined && random !== "") {
config.random = random;
}
var seed = queryString.getParam("seed");
if (seed) {
config.seed = seed;
}
/**
* ## Reporters
* The `HtmlReporter` builds all of the HTML UI for the runner page. This reporter paints the dots, stars, and x's for specs, as well as all spec names and all failures (if any).
*/
var htmlReporter = new jasmine.HtmlReporter({
env: env,
navigateWithNewParam: function(key, value) { return queryString.navigateWithNewParam(key, value); },
addToExistingQueryString: function(key, value) { return queryString.fullStringWithNewParam(key, value); },
getContainer: function() { return document.body; },
createElement: function() { return document.createElement.apply(document, arguments); },
createTextNode: function() { return document.createTextNode.apply(document, arguments); },
timer: new jasmine.Timer(),
filterSpecs: filterSpecs
});
/**
* The `jsApiReporter` also receives spec results, and is used by any environment that needs to extract the results from JavaScript.
*/
env.addReporter(jasmineInterface.jsApiReporter);
env.addReporter(htmlReporter);
/**
* Filter which specs will be run by matching the start of the full name against the `spec` query param.
*/
var specFilter = new jasmine.HtmlSpecFilter({
filterString: function() { return queryString.getParam("spec"); }
});
config.specFilter = function(spec) {
return specFilter.matches(spec.getFullName());
};
env.configure(config);
/**
* Setting up timing functions to be able to be overridden. Certain browsers (Safari, IE 8, phantomjs) require this hack.
*/
window.setTimeout = window.setTimeout;
window.setInterval = window.setInterval;
window.clearTimeout = window.clearTimeout;
window.clearInterval = window.clearInterval;
/**
* ## Execution
*
* Replace the browser window's `onload`, ensure it's called, and then run all of the loaded specs. This includes initializing the `HtmlReporter` instance and then executing the loaded Jasmine environment. All of this will happen after all of the specs are loaded.
*/
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
htmlReporter.initialize();
env.execute();
};
/**
* Helper function for readability above.
*/
function extend(destination, source) {
for (var property in source) destination[property] = source[property];
return destination;
}
}());

View File

@@ -1,8 +1,10 @@
function sendMessage (message) {
const msgElement = document.createElement("div");
msgElement.setAttribute("id", "__msg");
msgElement.textContent = JSON.stringify(message);
document.body.appendChild(msgElement);
"use strict";
// Create socket connection
const socket = new WebSocket("ws://localhost:8080");
window.sendMessage = (message) => {
socket.send(JSON.stringify(message));
}
const reporterMethods = [
@@ -26,4 +28,6 @@ for (const method of reporterMethods) {
}
}
jasmine.getEnv().addReporter(customReporter);
socket.addEventListener("open", ev => {
jasmine.getEnv().addReporter(customReporter);
});