browser_webconsole_webextension_content_script.js (3051B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Tests that an uncaught promise rejection from a content script 5 // is reported to the tabs' webconsole. 6 7 "use strict"; 8 9 const TEST_URI = 10 "https://example.com/browser/devtools/client/webconsole/" + 11 "test/browser/test-blank.html"; 12 13 add_task(async function () { 14 const extension = ExtensionTestUtils.loadExtension({ 15 manifest: { 16 content_scripts: [ 17 { 18 matches: [TEST_URI], 19 js: ["content-script.js"], 20 }, 21 ], 22 }, 23 24 files: { 25 "content-script.js": function () { 26 /* global browser */ 27 console.log("def"); 28 29 // Create an iframe with a privileged document of the extension 30 const iframe = document.createElement("iframe"); 31 iframe.src = browser.runtime.getURL(`iframe.html`); 32 document.body.appendChild(iframe); 33 34 Promise.reject("abc"); 35 }, 36 37 "iframe.html": `<div>Extension iframe</div> <script src="iframe.js"></script>`, 38 "iframe.js": `console.log("iframe log"); throw new Error("iframe exception")`, 39 }, 40 }); 41 42 await extension.startup(); 43 44 const hud = await openNewTabAndConsole(TEST_URI); 45 46 // For now, console messages and errors are shown without having to enable the content script targets 47 await checkUniqueMessageExists(hud, "uncaught exception: abc", ".error"); 48 await checkUniqueMessageExists(hud, "def", ".console-api"); 49 50 await checkUniqueMessageExists(hud, "iframe log", ".console-api"); 51 await checkUniqueMessageExists( 52 hud, 53 "Uncaught Error: iframe exception", 54 ".error" 55 ); 56 57 // Enable the content script preference in order to see content scripts messages, 58 // sources and target. 59 const onTargetProcessed = waitForTargetProcessed( 60 hud.commands, 61 target => target.targetType == "content_script" 62 ); 63 await pushPref("devtools.debugger.show-content-scripts", true); 64 await onTargetProcessed; 65 66 // Wait for more to let a chance to process unexpected duplicated messages 67 await wait(500); 68 69 await checkUniqueMessageExists(hud, "uncaught exception: abc", ".error"); 70 await checkUniqueMessageExists(hud, "def", ".console-api"); 71 72 await hud.toolbox.selectTool("jsdebugger"); 73 74 const evaluationContextSelectorButton = hud.ui.outputNode.querySelector( 75 ".webconsole-evaluation-selector-button" 76 ); 77 ok( 78 evaluationContextSelectorButton, 79 "The evaluation context selector is visible" 80 ); 81 82 info("Assert the content of the evaluation context menu"); 83 // Note that the context menu is in the top level chrome document (toolbox.xhtml) 84 // instead of webconsole.xhtml. 85 const labels = hud.toolbox.doc.querySelectorAll( 86 "#webconsole-console-evaluation-context-selector-menu-list li .label" 87 ); 88 is(labels[0].textContent, "Top"); 89 ok(!labels[0].closest(".menu-item").classList.contains("indented")); 90 is(labels[1].textContent, "Generated extension"); 91 ok(labels[1].closest(".menu-item").classList.contains("indented")); 92 93 await extension.unload(); 94 });