browser_webconsole_cached_messages.js (5133B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // Test to see if the cached messages are displayed when the console UI is opened. 5 6 "use strict"; 7 8 // See Bug 1570524. 9 requestLongerTimeout(2); 10 11 const TEST_URI = `data:text/html,<!DOCTYPE html><meta charset=utf8><h1>Test cached messages</h1> 12 <style> 13 h1 { 14 color: cssColorBug611032; 15 } 16 </style> 17 <script> 18 function logException() { 19 return new Promise(resolve => { 20 setTimeout(() => { 21 let foo = {}; 22 resolve(); 23 foo.unknown(); 24 }, 0); 25 }) 26 } 27 </script>`; 28 29 add_task(async function () { 30 // On e10s, the exception is triggered in child process 31 // and is ignored by test harness 32 if (!Services.appinfo.browserTabsRemoteAutostart) { 33 expectUncaughtException(); 34 } 35 // Enable CSS and XHR filters for the test. 36 await pushPref("devtools.webconsole.filter.css", true); 37 await pushPref("devtools.webconsole.filter.netxhr", true); 38 39 await addTab(TEST_URI); 40 41 info("Log different type of messages to fill the cache"); 42 await logMessages(); 43 44 info("Open the console"); 45 let hud = await openConsole(); 46 47 // We only start watching network requests when opening the toolbox. 48 await testMessagesVisibility(hud, false); 49 50 info("Close the toolbox and reload the tab"); 51 await closeToolboxIfOpen(); 52 await reloadPage(); 53 54 info( 55 "Open the toolbox with the inspector selected, so we can get network messages" 56 ); 57 await openInspector(); 58 59 info("Log different type of messages to fill the cache"); 60 await logMessages(); 61 62 info("Select the console"); 63 hud = await openConsole(); 64 65 await testMessagesVisibility(hud); 66 67 info("Close the toolbox"); 68 await closeToolboxIfOpen(); 69 70 info("Open the console again"); 71 hud = await openConsole(); 72 // The network messages don't persist. 73 await testMessagesVisibility(hud, false); 74 }); 75 76 async function logMessages() { 77 await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () { 78 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout 79 const wait = () => 80 new Promise(res => content.wrappedJSObject.setTimeout(res, 100)); 81 82 content.wrappedJSObject.console.log("log Bazzle"); 83 await wait(); 84 85 await content.wrappedJSObject.logException(); 86 await wait(); 87 88 await content.wrappedJSObject.fetch( 89 "http://mochi.test:8888/browser/devtools/client/webconsole/test/browser/sjs_cors-test-server.sjs?1", 90 { mode: "cors" } 91 ); 92 await wait(); 93 94 content.wrappedJSObject.console.error("error Bazzle"); 95 await wait(); 96 97 await content.wrappedJSObject.logException(); 98 await wait(); 99 100 await content.wrappedJSObject.fetch( 101 "http://mochi.test:8888/browser/devtools/client/webconsole/test/browser/sjs_cors-test-server.sjs?2" 102 ); 103 104 content.wrappedJSObject.console.info("info Bazzle"); 105 await wait(); 106 }); 107 } 108 109 async function testMessagesVisibility(hud, checkNetworkMessage = true) { 110 // wait for the last logged message to be displayed 111 await waitFor(() => findConsoleAPIMessage(hud, "info Bazzle", ".info")); 112 113 const messages = Array.from(hud.ui.outputNode.querySelectorAll(".message")); 114 const EXPECTED_MESSAGES = [ 115 { 116 text: "log Bazzle", 117 category: "log", 118 }, 119 { 120 text: "foo.unknown is not a function", 121 category: "error", 122 }, 123 { 124 text: "sjs_cors-test-server.sjs?1", 125 category: "network", 126 }, 127 { 128 text: "error Bazzle", 129 category: "error", 130 }, 131 { 132 text: "foo.unknown is not a function", 133 category: "error", 134 }, 135 { 136 text: "sjs_cors-test-server.sjs?2", 137 category: "network", 138 }, 139 { 140 text: "info Bazzle", 141 category: "info", 142 }, 143 ].filter(({ category }) => checkNetworkMessage || category != "network"); 144 145 // Clone the original array so we can use it later 146 const expectedMessages = [...EXPECTED_MESSAGES]; 147 for (const message of messages) { 148 const [expectedMessage] = expectedMessages; 149 if ( 150 message.classList.contains(expectedMessage.category) && 151 message.textContent.includes(expectedMessage.text) 152 ) { 153 ok( 154 true, 155 `The ${expectedMessage.category} message "${expectedMessage.text}" is visible at the expected place` 156 ); 157 expectedMessages.shift(); 158 if (expectedMessages.length === 0) { 159 ok( 160 true, 161 "All the expected messages were found at the expected position" 162 ); 163 break; 164 } 165 } 166 } 167 168 if (expectedMessages.length) { 169 ok( 170 false, 171 `Some messages are not visible or not in the expected order. Expected to find: \n\n${EXPECTED_MESSAGES.map( 172 ({ text }) => text 173 ).join("\n")}\n\nGot: \n\n${messages 174 .map(message => `${message.querySelector(".message-body").textContent}`) 175 .join("\n")}` 176 ); 177 } 178 179 // We can't assert the CSS warning position, so we only check that it's visible. 180 await waitFor( 181 () => findWarningMessage(hud, "cssColorBug611032", ".css"), 182 "Couldn't find the CSS warning message" 183 ); 184 ok(true, "css warning message is visible"); 185 }