tor-browser

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

browser_webconsole_async_stack.js (2691B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Check that async stacktraces are displayed as expected.
      5 
      6 "use strict";
      7 
      8 const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html><script>
      9 function timeout(cb, delay) {
     10  setTimeout(cb, delay);
     11 }
     12 
     13 function promiseThen(cb) {
     14  Promise.resolve().then(cb);
     15 }
     16 
     17 const onTimeout = () => {
     18  console.trace("Trace message");
     19  console.error("console error message");
     20  throw new Error("Thrown error message");
     21 };
     22 const onPromiseThen = () => timeout(onTimeout, 1);
     23 promiseThen(onPromiseThen);
     24 
     25 </script>`;
     26 
     27 add_task(async function () {
     28  await pushPref("javascript.options.asyncstack_capture_debuggee_only", false);
     29  const hud = await openNewTabAndConsole(TEST_URI);
     30 
     31  // Cached messages stacktrace are missing "promise callback" frames, so we reload
     32  // the page to get "live" messages instead. See Bug 1604428.
     33  await reloadPage();
     34 
     35  const expectedFrames = [
     36    "onTimeout",
     37    "(Async: setTimeout handler)",
     38    "timeout",
     39    "onPromiseThen",
     40    "(Async: promise callback)",
     41    "promiseThen",
     42    "<anonymous>",
     43  ].join("\n");
     44 
     45  const traceMsgNode = await waitFor(
     46    () => findConsoleAPIMessage(hud, "Trace message", ".trace"),
     47    "Wait for the trace message to be logged"
     48  );
     49  let frames = await getSimplifiedStack(traceMsgNode);
     50  is(frames, expectedFrames, "console.trace has expected frames");
     51 
     52  const consoleErrorMsgNode = await waitFor(
     53    () => findConsoleAPIMessage(hud, "console error message", ".error"),
     54    "Wait for the console error message to be logged"
     55  );
     56  consoleErrorMsgNode.querySelector(".arrow").click();
     57  frames = await getSimplifiedStack(consoleErrorMsgNode);
     58  is(frames, expectedFrames, "console.error has expected frames");
     59 
     60  const errorMsgNode = await waitFor(
     61    () =>
     62      findErrorMessage(
     63        hud,
     64        "Uncaught Error: Thrown error message",
     65        ".javascript"
     66      ),
     67    "Wait for the thrown error message to be logged"
     68  );
     69  errorMsgNode.querySelector(".arrow").click();
     70  frames = await getSimplifiedStack(errorMsgNode);
     71  is(frames, expectedFrames, "thrown error has expected frames");
     72 });
     73 
     74 async function getSimplifiedStack(messageEl) {
     75  const framesEl = await waitFor(() => {
     76    const frames = messageEl.querySelectorAll(
     77      `.message-body-wrapper > .stacktrace .frame,
     78       .message-body-wrapper > .stacktrace .location-async-cause`
     79    );
     80    return frames.length ? frames : null;
     81  }, "Couldn't find stacktrace");
     82 
     83  return [...framesEl]
     84    .map(frameEl => {
     85      const el = frameEl.querySelector(".title") || frameEl;
     86      return el.textContent.trim();
     87    })
     88    .flat()
     89    .join("\n");
     90 }