1 /* exported attachURL, promiseDone,
4 runNextTest, _documentWalker */
7 const { require
} = ChromeUtils
.importESModule(
8 "resource://devtools/shared/loader/Loader.sys.mjs"
12 } = require("resource://devtools/shared/commands/commands-factory.js");
15 } = require("resource://devtools/server/devtools-server.js");
16 const { BrowserTestUtils
} = ChromeUtils
.importESModule(
17 "resource://testing-common/BrowserTestUtils.sys.mjs"
20 DocumentWalker
: _documentWalker
,
21 } = require("resource://devtools/server/actors/inspector/document-walker.js");
23 // Always log packets when running tests.
24 Services
.prefs
.setBoolPref("devtools.debugger.log", true);
25 SimpleTest
.registerCleanupFunction(function () {
26 Services
.prefs
.clearUserPref("devtools.debugger.log");
29 if (!DevToolsServer
.initialized
) {
30 DevToolsServer
.init();
31 DevToolsServer
.registerAllActors();
32 SimpleTest
.registerCleanupFunction(function () {
33 DevToolsServer
.destroy();
37 var gAttachCleanups
= [];
39 SimpleTest
.registerCleanupFunction(function () {
40 for (const cleanup
of gAttachCleanups
) {
46 * Open a tab, load the url, wait for it to signal its readiness,
47 * connect to this tab via DevTools protocol and return.
49 * Returns an object with a few helpful attributes:
50 * - commands {Object}: The commands object defined by modules from devtools/shared/commands
51 * - target {TargetFront}: The current top-level target front.
52 * - doc {HtmlDocument}: the tab's document that got opened
54 async
function attachURL(url
) {
55 // Get the current browser window
57 Services
.wm
.getMostRecentWindow("navigator:browser").gBrowser
;
59 // open the url in a new tab, save a reference to the new inner window global object
60 // and wait for it to load. The tests rely on this window object to send a "ready"
61 // event to its opener (the test page). This window reference is used within
62 // the test tab, to reference the webpage being tested against, which is in another
64 const windowOpened
= BrowserTestUtils
.waitForNewTab(gBrowser
, url
);
65 const win
= window
.open(url
, "_blank");
68 const commands
= await CommandsFactory
.forTab(gBrowser
.selectedTab
);
69 await commands
.targetCommand
.startListening();
71 const cleanup
= async
function () {
72 await commands
.destroy();
78 gAttachCleanups
.push(cleanup
);
81 target
: commands
.targetCommand
.targetFront
,
86 function promiseOnce(target
, event
) {
87 return new Promise(resolve
=> {
88 target
.on(event
, (...args
) => {
89 if (args
.length
=== 1) {
98 function promiseDone(currentPromise
) {
99 currentPromise
.catch(err
=> {
100 ok(false, "Promise failed: " + err
);
109 function addTest(test
) {
113 function addAsyncTest(generator
) {
114 _tests
.push(() => generator().catch(ok
.bind(null, false)));
117 function runNextTest() {
118 if (!_tests
.length
) {
122 const fn
= _tests
.shift();
128 (fn
.name
? "'" + fn
.name
+ "' " : "") +
129 "threw an exception: " +