Bug 1553103 - Document the node test script using chrome-remote-inteface. r=jdescottes
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 18 Jun 2019 22:05:56 +0000
changeset 479196 a026827c657665ade9e7ffc6e03591093e49bbe4
parent 479195 11f0e1ee8f0c9ec82c81ed842fc68402429d4441
child 479197 b3c2e1416e88d0a364e789a8a83b222c969d62c4
push id36173
push userrgurzau@mozilla.com
push dateWed, 19 Jun 2019 15:50:11 +0000
treeherdermozilla-central@10e3ed789e2b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdescottes
bugs1553103
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
remote/test/demo.js
--- a/remote/test/demo.js
+++ b/remote/test/demo.js
@@ -1,38 +1,68 @@
 "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() {
   let client;
   try {
     client = await CDP();
     const {Log, Network, Page, Runtime} = client;
-    let { result } = await Runtime.evaluate({expression: "this.obj = {foo:true}; this.obj"});
+
+    // 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"}));
+    ({ result } = await Runtime.evaluate({
+      expression: "this.obj",
+      contextId,
+    }));
     console.log("2", result);
-    ({ result } = await Runtime.evaluate({expression: "this.obj.foo"}));
+    ({ result } = await Runtime.evaluate({
+      expression: "this.obj.foo",
+      contextId,
+    }));
     console.log("3", result);
 
-
     // receive console.log messages and print them
     Log.enable();
     Log.entryAdded(({entry}) => {
       const {timestamp, level, text, args} = entry;
       const msg = text || args.join(" ");
       console.log(`${new Date(timestamp)}\t${level.toUpperCase()}\t${msg}`);
     });
 
     // 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 {
     if (client) {
       await client.close();
     }
   }
 }