Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

6.7 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import path from "node:path";
import { expect, type Locator, type Page } from "@playwright/test";
import { type HotkeyAction, HotkeyProvider } from "../src/core/hotkeys/hotkeys";
import { clickWithRetry, waitForMarimoApp } from "./test-utils";

export async function createCellBelow(opts: {
  page: Page;
  cellSelector: string;
  content: string;
  run: boolean;
}) {
  const { page, cellSelector, content, run } = opts;

  // Hover over a cell the 'add cell' button appears
  await page.hover(cellSelector);
  await expect(
    page.getByTestId("create-cell-button").locator(":visible"),
  ).toHaveCount(2);

  // Clicking the first button creates a new cell below
  await page
    .getByTestId("create-cell-button")
    .locator(":visible")
    .last()
    .click();
  // Type into the currently focused cell
  if (content) {
    await page.locator("*:focus").type(content);
  }

  // Run the new cell
  if (run) {
    await page.locator("*:focus").hover();
    await page.getByTestId("run-button").locator(":visible").first().click();
  }
}

export async function openCellActions(page: Page, element: Locator) {
  await element.hover();
  await page
    .getByTestId("cell-actions-button")
    .locator(":visible")
    .first()
    .click();
}

export async function runCell(opts: { page: Page; cellSelector: string }) {
  const { page, cellSelector } = opts;

  // Hover over a cell
  await page.hover(cellSelector);

  // Run the new cell
  await page.getByTestId("run-button").locator(":visible").first().click();
}

const countsForName: Record<string, number> = {};
/**
 * Take a screenshot of the page.
 * @example
 * await takeScreenshot(page, _filename);
 */
export async function takeScreenshot(page: Page, filename: string) {
  const clean = path.basename(filename).replace(".spec.ts", "");

  const count = countsForName[clean] || 0;
  countsForName[clean] = count + 1;
  const fullName = `${clean}.${count}`;
  await page.screenshot({
    path: `e2e-tests/screenshots/${fullName}.png`,
    fullPage: true,
  });
}

/**
 * Press a hotkey on the page.
 *
 * It uses the hotkey provider to get the correct key for the current platform
 * and then maps it to the correct key for playwright.
 */
export async function pressShortcut(page: Page, action: HotkeyAction) {
  const isMac = await page.evaluate(() => navigator.userAgent.includes("Mac"));
  const provider = HotkeyProvider.create();
  const key = provider.getHotkey(action);
  // playwright uses "Meta" for command key on mac, "Control" for windows/linux
  // we also need to capitalize the first letter of each key
  const split = key.key.split("-");
  const capitalized = split.map((s) => s[0].toUpperCase() + s.slice(1));
  const keymap = capitalized
    .join("+")
    .replace("Cmd", isMac ? "Meta" : "Control")
    .replace("Ctrl", "Control");

  await page.keyboard.press(keymap);
}

/**
 * Download as HTML
 *
 * Download HTML of the current notebook and take a screenshot
 */
export async function exportAsHTMLAndTakeScreenshot(page: Page) {
  // Wait for networkidle so that the notebook is fully loaded
  await page.waitForLoadState("networkidle");

  await openCommandPalette({ page, command: "Download as HTML" });

  const [download] = await Promise.all([
    page.waitForEvent("download"),
    page.getByRole("button", { name: "Export HTML" }).click(),
  ]);

  // Wait for the download process to complete and save the downloaded file somewhere.
  const exportPath = `e2e-tests/exports/${download.suggestedFilename()}`;
  await download.saveAs(exportPath);

  // Open a new page and take a screenshot
  const exportPage = await page.context().newPage();
  // Dependency updates change chunk hashes before those assets reach the CDN.
  await exportPage.route(
    "https://cdn.jsdelivr.net/npm/@marimo-team/frontend@*/dist/**",
    async (route) => {
      const pathname = new URL(route.request().url()).pathname;
      const assetPath = pathname.slice(pathname.indexOf("/dist/") + 6);
      await route.fulfill({
        path: path.join(process.cwd(), "../marimo/_static", assetPath),
      });
    },
  );
  const fullPath = `${process.cwd()}/${exportPath}`;
  await exportPage.goto(`file://${fullPath}`, {
    waitUntil: "networkidle",
  });
  await expect(exportPage.getByTestId("static-notebook-banner")).toBeVisible();
  await takeScreenshot(exportPage, exportPath);

  // Toggle code
  if (await exportPage.isVisible("[data-testid=show-code]")) {
    await exportPage.getByTestId("show-code").click();
    // wait 100ms for the code to be shown
    await exportPage.waitForTimeout(100);
  }

  // Take screenshot of code
  await takeScreenshot(exportPage, `code-${exportPath}`);
}

export async function exportAsPNG(page: Page) {
  // Wait for networkidle so that the notebook is fully loaded
  await page.waitForLoadState("networkidle");

  const [download] = await Promise.all([
    page.waitForEvent("download"),
    page
      .getByTestId("notebook-menu-dropdown")
      .click()
      .then(() => {
        return openCommandPalette({ page, command: "Download as PNG" });
      }),
  ]);

  // Wait for the download process to complete and save the downloaded file somewhere.
  const path = `e2e-tests/screenshots/${download.suggestedFilename()}`;
  await download.saveAs(path);
}

/**
 * Open the command palette, type something, and hit Enter
 */
export async function openCommandPalette(opts: {
  page: Page;
  command: string;
}) {
  const { page, command } = opts;

  // Blur any active element first to avoid typing into cell editors
  await page.evaluate(() => {
    if (document.activeElement instanceof HTMLElement) {
      document.activeElement.blur();
    }
  });

  // Open command palette with Ctrl+K (or Cmd+K on Mac)
  await pressShortcut(page, "global.commandPalette");

  // Wait for the command palette input to be visible
  const commandInput = page.getByPlaceholder("Type to search...");
  await expect(commandInput).toBeVisible();

  // Focus and wait for it to be ready
  await commandInput.focus();
  await commandInput.fill(command);

  // Hit Enter to execute
  await page.keyboard.press("Enter");
}

/**
 * Waits for the page to load. If we have resumed a session, we restart the kernel.
 */
export async function maybeRestartKernel(page: Page) {
  // Wait for cells to appear
  await waitForMarimoApp(page);

  // If it says, "You have connected to an existing session", then restart
  const hasText = await page
    .getByText("You have reconnected to an existing session", { exact: false })
    .isVisible();
  if (!hasText) {
    return;
  }

  await clickWithRetry(page, "[data-testid='notebook-menu-dropdown']");
  await page.getByText("Restart kernel", { exact: true }).click();
  await page.getByLabel("Confirm Restart", { exact: true }).click();
}