Bug 1586798 - Use WalkerFront from the currently selected element in onTagEdit()...
[gecko.git] / remote / test / demo.js
blob20306740ef6dcea20b9d7f20a340a22808e597ad
1 "use strict";
3 /**
4  * nodejs script to test basic CDP behaviors against Firefox and Chromium.
5  *
6  * Install chrome-remote-interface, the npm package for a CDP client in node:
7  * $ npm install chrome-remote-interface
8  *
9  * Run Firefox or Chromium with server turned on:
10  * $ ./mach run --setpref "remote.enabled=true" --remote-debugging-port 9222
11  * $ firefox --remote-debugging-port 9222
12  * $ chromium-browser --remote-debugging-port=9222
13  *
14  * Run this script:
15  * $ node demo.js
16  */
17 const CDP = require("chrome-remote-interface");
19 async function demo() {
20   let client;
21   try {
22     client = await CDP();
23     const {Log, Network, Page, Runtime} = client;
25     // Bug 1553756, Firefox requires `contextId` argument to be passed to
26     // Runtime.evaluate, so fetch the current context id it first.
27     Runtime.enable();
28     const { context } = await Runtime.executionContextCreated();
29     const contextId = context.id;
31     let { result } = await Runtime.evaluate({
32       expression: "this.obj = {foo:true}; this.obj",
33       contextId,
34     });
35     console.log("1", result);
36     ({ result } = await Runtime.evaluate({
37       expression: "this.obj",
38       contextId,
39     }));
40     console.log("2", result);
41     ({ result } = await Runtime.evaluate({
42       expression: "this.obj.foo",
43       contextId,
44     }));
45     console.log("3", result);
47     // receive console.log messages and print them
48     Log.enable();
49     Log.entryAdded(({entry}) => {
50       const {timestamp, level, text, args} = entry;
51       const msg = text || args.join(" ");
52       console.log(`${new Date(timestamp)}\t${level.toUpperCase()}\t${msg}`);
53     });
55     // turn on navigation related events, such as DOMContentLoaded et al.
56     await Page.enable();
58     const onLoad = Page.loadEventFired();
59     await Page.navigate({url: "data:text/html,test-page<script>console.log('foo');</script><script>'</script>"});
60     await onLoad;
61   } catch (e) {
62     console.error(e);
63   } finally {
64     if (client) {
65       await client.close();
66     }
67   }
70 demo();