tor-browser

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

browser_jsterm_screenshot_command_selector.js (4346B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that screenshot command works properly with the --selector arg
      5 
      6 "use strict";
      7 
      8 const TEST_URI =
      9  "http://example.com/browser/devtools/client/webconsole/" +
     10  "test/browser/test_jsterm_screenshot_command.html";
     11 
     12 // on some machines, such as macOS, dpr is set to 2. This is expected behavior, however
     13 // to keep tests consistant across OSs we are setting the dpr to 1
     14 const dpr = "--dpr 1";
     15 
     16 add_task(async function () {
     17  const hud = await openNewTabAndConsole(TEST_URI);
     18 
     19  info("wait for the iframes to be loaded");
     20  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
     21    await ContentTaskUtils.waitForCondition(
     22      () => content.document.querySelectorAll(".loaded-iframe").length == 2
     23    );
     24  });
     25 
     26  info("Test :screenshot --selector iframe");
     27  const sameOriginIframeScreenshotFile = new FileUtils.File(
     28    PathUtils.join(
     29      PathUtils.tempDir,
     30      "TestScreenshotFile-same-origin-iframe.png"
     31    )
     32  );
     33  await executeAndWaitForMessageByType(
     34    hud,
     35    `:screenshot --selector #same-origin-iframe ${sameOriginIframeScreenshotFile.path} ${dpr}`,
     36    `Saved to ${sameOriginIframeScreenshotFile.path}`,
     37    ".console-api"
     38  );
     39 
     40  let fileExists = sameOriginIframeScreenshotFile.exists();
     41  if (!fileExists) {
     42    throw new Error(`${sameOriginIframeScreenshotFile.path} does not exist`);
     43  }
     44 
     45  ok(
     46    fileExists,
     47    `Screenshot was saved to ${sameOriginIframeScreenshotFile.path}`
     48  );
     49 
     50  info("Create an image using the downloaded file as source");
     51  let image = new Image();
     52  image.src = PathUtils.toFileURI(sameOriginIframeScreenshotFile.path);
     53  await once(image, "load");
     54 
     55  info("Check that the node was rendered as expected in the screenshot");
     56  checkImageColorAt({
     57    image,
     58    y: 0,
     59    expectedColor: `rgb(255, 255, 0)`,
     60    label:
     61      "The top-left corner has the expected color, matching the same-origin iframe",
     62  });
     63 
     64  // Remove the downloaded screenshot file and cleanup downloads
     65  await IOUtils.remove(sameOriginIframeScreenshotFile.path);
     66  await resetDownloads();
     67 
     68  info("Check using :screenshot --selector in a remote-iframe context");
     69  // Select the remote iframe in the context selector
     70  const evaluationContextSelectorButton = hud.ui.outputNode.querySelector(
     71    ".webconsole-evaluation-selector-button"
     72  );
     73 
     74  const remoteIframeUrl = await SpecialPowers.spawn(
     75    gBrowser.selectedBrowser,
     76    [],
     77    async () => {
     78      return content.document.querySelector("#remote-iframe").src;
     79    }
     80  );
     81  selectTargetInContextSelector(hud, remoteIframeUrl);
     82  await waitFor(() =>
     83    evaluationContextSelectorButton.innerText.includes("example.org")
     84  );
     85 
     86  const remoteIframeSpanScreenshot = new FileUtils.File(
     87    PathUtils.join(PathUtils.tempDir, "TestScreenshotFile-remote-iframe.png")
     88  );
     89  await executeAndWaitForMessageByType(
     90    hud,
     91    `:screenshot --selector span ${remoteIframeSpanScreenshot.path} ${dpr}`,
     92    `Saved to ${remoteIframeSpanScreenshot.path}`,
     93    ".console-api"
     94  );
     95 
     96  fileExists = remoteIframeSpanScreenshot.exists();
     97  if (!fileExists) {
     98    throw new Error(`${remoteIframeSpanScreenshot.path} does not exist`);
     99  }
    100 
    101  ok(fileExists, `Screenshot was saved to ${remoteIframeSpanScreenshot.path}`);
    102 
    103  info("Create an image using the downloaded file as source");
    104  image = new Image();
    105  image.src = PathUtils.toFileURI(remoteIframeSpanScreenshot.path);
    106  await once(image, "load");
    107 
    108  info("Check that the node was rendered as expected in the screenshot");
    109  checkImageColorAt({
    110    image,
    111    y: 0,
    112    expectedColor: `rgb(0, 100, 0)`,
    113    label:
    114      "The top-left corner has the expected color, matching the span inside the iframe",
    115  });
    116 
    117  info(
    118    "Check that using a selector that doesn't match any element displays a warning in console"
    119  );
    120  await executeAndWaitForMessageByType(
    121    hud,
    122    `:screenshot --selector #this-element-does-not-exist`,
    123    `The ‘#this-element-does-not-exist’ selector does not match any element on the page.`,
    124    ".warn"
    125  );
    126  ok(
    127    true,
    128    "A warning message is emitted when the passed selector doesn't match any element"
    129  );
    130 
    131  // Remove the downloaded screenshot file and cleanup downloads
    132  await IOUtils.remove(remoteIframeSpanScreenshot.path);
    133  await resetDownloads();
    134 });