Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

7.5 KB
Raw Blame
/* eslint-disable header/header */
/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type {
  Fixtures,
  FrameLocator,
  Locator,
  Page,
  Browser,
  BrowserContext
} from "@playwright/test";
import { step } from "./base-test";
import path from "path";
import { CommonFixtures, TestChildProcess } from "./common-fixtures";

type BaseTestFixtures = CommonFixtures & {
  context: BrowserContext;
};

type BaseWorkerFixtures = {
  headless: boolean;
  browser: Browser;
  browserName: "chromium" | "firefox" | "webkit";
  playwright: typeof import("@playwright/test");
};

export type TraceViewerFixtures = {
  showTraceViewer: (
    trace: string | undefined,
    options?: { host?: string; port?: number; stdin?: boolean }
  ) => Promise<TraceViewerPage>;
  runAndTrace: (
    body: () => Promise<void>,
    optsOverrides?: Parameters<BrowserContext["tracing"]["start"]>[0]
  ) => Promise<TraceViewerPage>;
};

class TraceViewerPage {
  actionTitles: Locator;
  actionsTree: Locator;
  callLines: Locator;
  consoleLines: Locator;
  logLines: Locator;
  errorMessages: Locator;
  consoleLineMessages: Locator;
  consoleStacks: Locator;
  networkRequests: Locator;
  metadataTab: Locator;
  snapshotContainer: Locator;
  sourceCodeTab: Locator;
  networkTab: Locator;

  settingsDialog: Locator;
  themeSetting: Locator;
  displayCanvasContentSetting: Locator;

  constructor(public page: Page, public process: TestChildProcess) {
    this.actionTitles = page.locator(".action-title");
    this.actionsTree = page.getByTestId("actions-tree");
    this.callLines = page.locator(".call-tab .call-line");
    this.logLines = page
      .getByRole("list", { name: "Log entries" })
      .getByRole("listitem");
    this.consoleLines = page
      .getByRole("tabpanel", { name: "Console" })
      .getByRole("listitem");
    this.consoleLineMessages = page.locator(".console-line-message");
    this.errorMessages = page.locator(".error-message");
    this.consoleStacks = page.locator(".console-stack");
    this.networkRequests = page
      .getByRole("list", { name: "Network requests" })
      .getByRole("listitem");
    this.snapshotContainer = page.locator(
      ".snapshot-container iframe.snapshot-visible[name=snapshot]"
    );
    this.metadataTab = page.getByRole("tabpanel", { name: "Metadata" });
    this.sourceCodeTab = page.getByRole("tabpanel", { name: "Source" });
    this.networkTab = page.getByRole("tabpanel", { name: "Network" });

    this.settingsDialog = page.getByTestId("settings-toolbar-dialog");
    this.themeSetting = this.settingsDialog.getByRole("combobox", {
      name: "Theme"
    });
    this.displayCanvasContentSetting = page
      .locator(".setting")
      .getByText("Display canvas content");
  }

  @step
  async showAllActions() {
    await this.page.getByRole("button", { name: "Filter actions" }).click();
    await this.page.locator(".setting").getByText("Network routes").click();
    await this.page.locator(".setting").getByText("Getters").click();
    await this.page.locator(".setting").getByText("Configuration").click();
    await this.page.getByRole("button", { name: "Filter actions" }).click();
  }

  stackFrames(options: { selected?: boolean } = {}) {
    const entry = this.page
      .getByRole("list", { name: "Stack trace" })
      .getByRole("listitem");
    if (options.selected) return entry.locator(":scope.selected");
    return entry;
  }

  actionIconsText(action: string) {
    const entry = this.actionsTree.getByRole("treeitem", { name: action });
    return entry.locator(".action-icon-value").filter({ visible: true });
  }

  actionIcons(action: string) {
    return this.actionsTree
      .getByRole("treeitem", { name: action })
      .locator(".action-icons")
      .filter({ visible: true });
  }

  @step
  async expandAction(title: string) {
    await this.actionsTree
      .getByRole("treeitem", { name: title })
      .locator(".codicon-chevron-right")
      .click();
  }

  @step
  async selectAction(title: string, ordinal: number = 0) {
    await this.actionsTree.getByTitle(title).nth(ordinal).click();
  }

  @step
  async hoverAction(title: string, ordinal: number = 0) {
    await this.actionsTree
      .getByRole("treeitem", { name: title })
      .nth(ordinal)
      .hover();
  }

  @step
  async selectSnapshot(name: string) {
    await this.page.getByRole("tab", { name }).click();
  }

  async showErrorsTab() {
    await this.page.getByRole("tab", { name: "Errors" }).click();
  }

  async showConsoleTab() {
    await this.page.getByRole("tab", { name: "Console" }).click();
  }

  async showSourceTab() {
    await this.page.getByRole("tab", { name: "Source" }).click();
  }

  async showNetworkTab() {
    await this.page.getByRole("tab", { name: "Network" }).click();
  }

  async showMetadataTab() {
    await this.page.getByRole("tab", { name: "Metadata" }).click();
  }

  async showSettings() {
    await this.page.getByRole("button", { name: "Settings" }).click();
  }

  @step
  async snapshotFrame(
    actionName: string,
    ordinal: number = 0,
    hasSubframe: boolean = false
  ): Promise<FrameLocator> {
    await this.selectAction(actionName, ordinal);
    while (this.page.frames().length < (hasSubframe ? 4 : 3))
      await this.page.waitForEvent("frameattached");
    return this.page.frameLocator("iframe.snapshot-visible[name=snapshot]");
  }
}

export const traceViewerFixtures: Fixtures<
  TraceViewerFixtures,
  {},
  BaseTestFixtures,
  BaseWorkerFixtures
> = {
  showTraceViewer: async ({ playwright, childProcess, browserName }, use) => {
    const browsers: Browser[] = [];
    await use(async (trace: string | undefined, { host, port, stdin } = {}) => {
      const command = [
        "node",
        path.join(__dirname, "../../node_modules/playwright-core/cli.js"),
        "show-trace",
        "--port",
        "" + (port ?? "0")
      ];
      if (host) command.push("--host", host);
      if (stdin) command.push("--stdin");
      if (trace) command.push(trace);
      const cp = childProcess({ command });
      await cp.waitForOutput("Listening on");
      const browser = await playwright.chromium.launch({
        ...(browserName === "chromium" ? {} : { channel: "chromium" }),
        executablePath: process.env.CRPATH // without this, setting FFPATH makes us launch Firefox with Chromium args
      });
      browsers.push(browser);
      const page = await browser.newPage();
      const url = cp.output.match(/Listening on (http:\/\/[^\s]+)/)![1];
      await page.goto(url);
      return new TraceViewerPage(page, cp);
    });
    for (const browser of browsers) await browser.close();
  },

  runAndTrace: async ({ context, showTraceViewer }, use, testInfo) => {
    await use(async (body: () => Promise<void>, optsOverrides = {}) => {
      const traceFile = testInfo.outputPath("trace.zip");
      await context.tracing.start({
        snapshots: true,
        screenshots: true,
        sources: true,
        ...optsOverrides
      });
      await body();
      await context.tracing.stop({ path: traceFile });
      return showTraceViewer(traceFile);
    });
  }
};