Bug 1553103 - Document the node test script using chrome-remote-inteface. r=jdescottes

Also fix the script against firefox. It only used to run against chromium.

Differential Revision: https://phabricator.services.mozilla.com/D31991

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alexandre Poirot 2019-06-18 22:05:56 +00:00
parent 77363f0e08
commit cbc84bd6f9

View File

@ -1,5 +1,19 @@
"use strict";
/**
* nodejs script to test basic CDP behaviors against Firefox and Chromium.
*
* Install chrome-remote-interface, the npm package for a CDP client in node:
* $ npm install chrome-remote-interface
*
* Run Firefox or Chromium with server turned on:
* $ ./mach run --setpref "remote.enabled=true" --remote-debugging-port 9222
* $ firefox --remote-debugging-port 9222
* $ chromium-browser --remote-debugging-port=9222
*
* Run this script:
* $ node demo.js
*/
const CDP = require("chrome-remote-interface");
async function demo() {
@ -7,13 +21,28 @@ async function demo() {
try {
client = await CDP();
const {Log, Network, Page, Runtime} = client;
let { result } = await Runtime.evaluate({expression: "this.obj = {foo:true}; this.obj"});
console.log("1", result);
({ result } = await Runtime.evaluate({expression: "this.obj"}));
console.log("2", result);
({ result } = await Runtime.evaluate({expression: "this.obj.foo"}));
console.log("3", result);
// Bug 1553756, Firefox requires `contextId` argument to be passed to
// Runtime.evaluate, so fetch the current context id it first.
Runtime.enable();
const { context } = await Runtime.executionContextCreated();
const contextId = context.id;
let { result } = await Runtime.evaluate({
expression: "this.obj = {foo:true}; this.obj",
contextId,
});
console.log("1", result);
({ result } = await Runtime.evaluate({
expression: "this.obj",
contextId,
}));
console.log("2", result);
({ result } = await Runtime.evaluate({
expression: "this.obj.foo",
contextId,
}));
console.log("3", result);
// receive console.log messages and print them
Log.enable();
@ -26,8 +55,9 @@ async function demo() {
// turn on navigation related events, such as DOMContentLoaded et al.
await Page.enable();
const onLoad = Page.loadEventFired();
await Page.navigate({url: "data:text/html,test-page<script>console.log('foo');</script><script>'</script>"});
await Page.loadEventFired();
await onLoad;
} catch (e) {
console.error(e);
} finally {