browser_net_clear.js (4485B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 Services.scriptloader.loadSubScript( 7 "chrome://mochitests/content/browser/devtools/client/webconsole/test/browser/shared-head.js", 8 this 9 ); 10 11 /** 12 * Tests if the clear button empties the request menu. 13 */ 14 15 add_task(async function () { 16 Services.prefs.setBoolPref("devtools.webconsole.filter.net", true); 17 18 const { monitor, toolbox } = await initNetMonitor(HTTPS_SIMPLE_URL, { 19 requestCount: 1, 20 }); 21 info("Starting test... "); 22 23 const { document, store, windowRequire } = monitor.panelWin; 24 const Actions = windowRequire("devtools/client/netmonitor/src/actions/index"); 25 const clearButton = document.querySelector(".requests-list-clear-button"); 26 27 store.dispatch(Actions.batchEnable(false)); 28 29 // Make sure we start in a sane state 30 assertNoRequestState(); 31 32 // Load one request and assert it shows up in the list 33 let wait = waitForNetworkEvents(monitor, 1); 34 await reloadBrowser(); 35 await wait; 36 37 assertSingleRequestState(); 38 assertNetworkEventResourceState(1); 39 40 info("Swith to the webconsole and wait for network logs"); 41 const onWebConsole = monitor.toolbox.once("webconsole-selected"); 42 const { hud } = await monitor.toolbox.selectTool("webconsole"); 43 await onWebConsole; 44 45 info("Wait for request"); 46 await waitFor(() => findMessageByType(hud, HTTPS_SIMPLE_URL, ".network")); 47 48 info("Switch back the the netmonitor"); 49 await monitor.toolbox.selectTool("netmonitor"); 50 51 // Click clear and make sure the requests are gone 52 let waitRequestListCleared = waitForEmptyRequestList(document); 53 EventUtils.sendMouseEvent({ type: "click" }, clearButton); 54 await waitRequestListCleared; 55 56 assertNoRequestState(); 57 assertNetworkEventResourceState(0); 58 59 info( 60 "Swith back to the webconsole to assert that the cleared request gets disabled" 61 ); 62 await monitor.toolbox.selectTool("webconsole"); 63 64 info("Wait for network request to show and that its disabled"); 65 66 await waitFor(() => 67 findMessageByType(hud, HTTPS_SIMPLE_URL, ".network.disabled") 68 ); 69 70 // Switch back to the netmonitor. 71 await monitor.toolbox.selectTool("netmonitor"); 72 73 // Load a second request and make sure they still show up 74 wait = waitForNetworkEvents(monitor, 1); 75 await reloadBrowser(); 76 await wait; 77 78 assertSingleRequestState(); 79 assertNetworkEventResourceState(1); 80 81 // Make sure we can now open the network details panel 82 store.dispatch(Actions.toggleNetworkDetails()); 83 const detailsPanelToggleButton = document.querySelector(".sidebar-toggle"); 84 // Wait for the details panel to finish fetching the headers information 85 await waitForRequestData(store, ["requestHeaders", "responseHeaders"]); 86 87 ok( 88 detailsPanelToggleButton && 89 !detailsPanelToggleButton.classList.contains("pane-collapsed"), 90 "The details pane should be visible." 91 ); 92 93 // Click clear and make sure the details pane closes 94 waitRequestListCleared = waitForEmptyRequestList(document); 95 EventUtils.sendMouseEvent({ type: "click" }, clearButton); 96 await waitRequestListCleared; 97 98 assertNoRequestState(); 99 assertNetworkEventResourceState(0); 100 101 ok( 102 !document.querySelector(".network-details-bar"), 103 "The details pane should not be visible clicking 'clear'." 104 ); 105 106 return teardown(monitor); 107 108 /** 109 * Asserts the state of the network monitor when one request has loaded 110 */ 111 function assertSingleRequestState() { 112 is( 113 store.getState().requests.requests.length, 114 1, 115 "The request menu should have one item at this point." 116 ); 117 } 118 119 /** 120 * Asserts the state of the network monitor when no requests have loaded 121 */ 122 function assertNoRequestState() { 123 is( 124 store.getState().requests.requests.length, 125 0, 126 "The request menu should be empty at this point." 127 ); 128 } 129 130 function assertNetworkEventResourceState(expectedNoOfNetworkEventResources) { 131 const actualNoOfNetworkEventResources = 132 toolbox.commands.resourceCommand.getAllResources( 133 toolbox.commands.resourceCommand.TYPES.NETWORK_EVENT 134 ).length; 135 136 is( 137 actualNoOfNetworkEventResources, 138 expectedNoOfNetworkEventResources, 139 `The expected number of network resources is correctly ${actualNoOfNetworkEventResources}` 140 ); 141 } 142 143 function waitForEmptyRequestList(doc) { 144 info("Wait for request list to clear"); 145 return waitFor(() => !!doc.querySelector(".request-list-empty-notice")); 146 } 147 });