webconsole-helpers.js (1674B)
1 /* exported addTabAndCreateCommands */ 2 "use strict"; 3 4 const { require } = ChromeUtils.importESModule( 5 "resource://devtools/shared/loader/Loader.sys.mjs" 6 ); 7 const { 8 DevToolsServer, 9 } = require("resource://devtools/server/devtools-server.js"); 10 const { 11 CommandsFactory, 12 } = require("resource://devtools/shared/commands/commands-factory.js"); 13 14 // Always log packets when running tests. 15 Services.prefs.setBoolPref("devtools.debugger.log", true); 16 SimpleTest.registerCleanupFunction(function () { 17 Services.prefs.clearUserPref("devtools.debugger.log"); 18 }); 19 20 if (!DevToolsServer.initialized) { 21 DevToolsServer.init(); 22 DevToolsServer.registerAllActors(); 23 SimpleTest.registerCleanupFunction(function () { 24 DevToolsServer.destroy(); 25 }); 26 } 27 28 /** 29 * Open a tab, load the url, find the tab with the devtools server, 30 * and attach the console to it. 31 * 32 * @param {string} url : url to navigate to 33 * @return {Promise} Promise resolving when commands are initialized 34 * The Promise resolves with the commands. 35 */ 36 async function addTabAndCreateCommands(url) { 37 const tab = await addTab(url); 38 const commands = await CommandsFactory.forTab(tab); 39 await commands.targetCommand.startListening(); 40 return commands; 41 } 42 43 /** 44 * Naive implementaion of addTab working from a mochitest-chrome test. 45 */ 46 async function addTab(url) { 47 const { gBrowser } = Services.wm.getMostRecentBrowserWindow(); 48 const { BrowserTestUtils } = ChromeUtils.importESModule( 49 "resource://testing-common/BrowserTestUtils.sys.mjs" 50 ); 51 const tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, url)); 52 await BrowserTestUtils.browserLoaded(tab.linkedBrowser); 53 return tab; 54 }