tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

head.js (3034B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /* eslint no-unused-vars: [2, {"vars": "local", "args": "none"}] */
      7 /* import-globals-from ../../../test/head.js */
      8 
      9 // Load the NetMonitor head.js file to share its API.
     10 var netMonitorHead =
     11  "chrome://mochitests/content/browser/devtools/client/netmonitor/test/head.js";
     12 Services.scriptloader.loadSubScript(netMonitorHead, this);
     13 
     14 // Directory with HAR related test files.
     15 const HAR_EXAMPLE_URL =
     16  "https://example.com/browser/devtools/client/netmonitor/src/har/test/";
     17 
     18 /**
     19 * Trigger a "copy all as har" from the context menu of the requests list.
     20 
     21 * @param {object} monitor
     22 *        The network monitor object
     23 */
     24 async function copyAllAsHARWithContextMenu(monitor, { asString = false } = {}) {
     25  const { HarMenuUtils } = monitor.panelWin.windowRequire(
     26    "devtools/client/netmonitor/src/har/har-menu-utils"
     27  );
     28 
     29  info("Open the context menu on the first visible request.");
     30  const firstRequest =
     31    monitor.panelWin.document.querySelectorAll(".request-list-item")[0];
     32 
     33  EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest);
     34  EventUtils.sendMouseEvent({ type: "contextmenu" }, firstRequest);
     35 
     36  info("Trigger Copy All As HAR from the context menu");
     37  const onHarCopyDone = HarMenuUtils.once("copy-all-as-har-done");
     38  await selectContextMenuItem(monitor, "request-list-context-copy-all-as-har");
     39  const jsonString = await onHarCopyDone;
     40 
     41  if (asString) {
     42    return jsonString;
     43  }
     44  return JSON.parse(jsonString);
     45 }
     46 
     47 /**
     48 * Trigger a "save as har" from the context menu of the requests list.
     49 
     50 * @param {object} monitor
     51 *        The network monitor object
     52 */
     53 async function saveAsHARWithContextMenu(monitor, { asString = false } = {}) {
     54  info("Open the context menu on the first visible request.");
     55  const firstRequest =
     56    monitor.panelWin.document.querySelectorAll(".request-list-item")[0];
     57 
     58  EventUtils.sendMouseEvent({ type: "mousedown" }, firstRequest);
     59  EventUtils.sendMouseEvent({ type: "contextmenu" }, firstRequest);
     60 
     61  info("Trigger Save As HAR from the context menu");
     62  await selectContextMenuItem(monitor, "request-list-context-save-as-har");
     63 
     64  info("Mock the file picker dialog to save the HAR file to disk");
     65  const MockFilePicker = SpecialPowers.MockFilePicker;
     66  MockFilePicker.init(window.browsingContext);
     67  const nsiFile = new FileUtils.File(
     68    PathUtils.join(PathUtils.tempDir, `save_as_har-${Date.now()}.har`)
     69  );
     70  MockFilePicker.setFiles([nsiFile]);
     71  const path = nsiFile.path;
     72 
     73  info("Wait for the downloaded file to be fully saved to disk");
     74  await BrowserTestUtils.waitForCondition(() => IOUtils.exists(path));
     75  await BrowserTestUtils.waitForCondition(async () => {
     76    const { size } = await IOUtils.stat(path);
     77    return size > 0;
     78  });
     79  const buffer = await IOUtils.read(path);
     80  const savedFileContent = new TextDecoder().decode(buffer);
     81 
     82  if (asString) {
     83    return savedFileContent;
     84  }
     85  return JSON.parse(savedFileContent);
     86 }