summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/chrome/webconsole-helpers.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /devtools/server/tests/chrome/webconsole-helpers.js
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/server/tests/chrome/webconsole-helpers.js')
-rw-r--r--devtools/server/tests/chrome/webconsole-helpers.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/devtools/server/tests/chrome/webconsole-helpers.js b/devtools/server/tests/chrome/webconsole-helpers.js
new file mode 100644
index 0000000000..4236cba16b
--- /dev/null
+++ b/devtools/server/tests/chrome/webconsole-helpers.js
@@ -0,0 +1,58 @@
+/* exported attachURL, evaluateJS */
+"use strict";
+
+const { require } = ChromeUtils.import("resource://devtools/shared/Loader.jsm");
+const { DevToolsServer } = require("devtools/server/devtools-server");
+const { TargetFactory } = require("devtools/client/framework/target");
+
+const Services = require("Services");
+
+// Always log packets when running tests.
+Services.prefs.setBoolPref("devtools.debugger.log", true);
+SimpleTest.registerCleanupFunction(function() {
+ Services.prefs.clearUserPref("devtools.debugger.log");
+});
+
+if (!DevToolsServer.initialized) {
+ DevToolsServer.init();
+ DevToolsServer.registerAllActors();
+ SimpleTest.registerCleanupFunction(function() {
+ DevToolsServer.destroy();
+ });
+}
+
+/**
+ * Open a tab, load the url, find the tab with the devtools server,
+ * and attach the console to it.
+ *
+ * @param {string} url : url to navigate to
+ * @return {Promise} Promise resolving when the console is attached.
+ * The Promise resolves with an object containing :
+ * - tab: the attached tab
+ * - targetFront: the target front
+ * - webConsoleFront: the console front
+ * - cleanup: a generator function which can be called to close
+ * the opened tab and disconnect its devtools client.
+ */
+async function attachURL(url) {
+ const tab = await addTab(url);
+ const target = await TargetFactory.forTab(tab);
+ await target.attach();
+ const webConsoleFront = await target.getFront("console");
+ return {
+ webConsoleFront,
+ };
+}
+
+/**
+ * Naive implementaion of addTab working from a mochitest-chrome test.
+ */
+async function addTab(url) {
+ const { gBrowser } = Services.wm.getMostRecentWindow("navigator:browser");
+ const {
+ BrowserTestUtils,
+ } = require("resource://testing-common/BrowserTestUtils.jsm");
+ const tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, url));
+ await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+ return tab;
+}