Creating a Live Playwright REPL for Browser Automation over SSH or API
HexShift

HexShift @hexshift

About: Help with web development through niche, technical tutorials. From advanced JavaScript patterns and TypeScript best practices to mastering frameworks. Support link at base of each article. Thanks.

Joined:
Apr 5, 2025

Creating a Live Playwright REPL for Browser Automation over SSH or API

Publish Date: May 1
0 0

Playwright is already one of the best tools for browser automation — but what if you could connect to a browser session remotely and script it live?

This article shows you how to build a Node.js REPL interface connected to a running Playwright browser, allowing live control via terminal, SSH, or even WebSockets.

Use cases include:

  • Debugging headless sessions on remote servers
  • Teaching or pairing in live browser contexts
  • Automated QA with human override
  • Controlled automation bots (via API or CLI)

Step 1: Set Up a Headless Browser with Playwright

Start by launching a Playwright browser that stays open:


npm install playwright

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({ headless: false });
  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto('https://example.com');

  // Keep alive
  global.browser = browser;
  global.page = page;
})();

Step 2: Start a Node.js REPL That Has Access to page

Let’s expose the Playwright page to a live REPL:


const repl = require('repl');

const r = repl.start({
  prompt: 'playwright> ',
  useGlobal: true
});

r.context.page = global.page;

Now anything you type into this REPL — like await page.screenshot() — runs directly in the open browser session.


Step 3: Allow Remote Access Over SSH

By running this script on a remote server, you can SSH in and attach to the live REPL:


ssh user@host
node remote-playwright.js

Once connected, you can do:


playwright> await page.click('text=Login')
playwright> await page.fill('#email', 'me@example.com')

It’s like a programmable Chrome DevTools, from the CLI.


Step 4: Optional – Expose REPL Over WebSocket or API

For advanced cases, expose this REPL interface over WebSocket using libraries like ws or socket.io:


const WebSocket = require('ws');
const { Writable } = require('stream');

const wss = new WebSocket.Server({ port: 8080 });

wss.on('connection', (ws) => {
  const stream = new Writable({
    write(chunk, _, cb) {
      ws.send(chunk.toString());
      cb();
    }
  });

  const r = repl.start({
    input: ws,
    output: stream,
    prompt: 'webrepl> ',
    terminal: false,
    useGlobal: true
  });

  r.context.page = global.page;
});

Now you can connect live to the REPL from a web client or external system.


Step 5: Bonus – Add Sandbox or Timeout Protections

To make your live REPL safer, add timeouts or sandbox restrictions using tools like vm2 or use setTimeout to auto-disconnect idle users.


const { NodeVM } = require('vm2');

const vm = new NodeVM({
  console: 'inherit',
  sandbox: { page: global.page },
  timeout: 3000
});

vm.run('await page.screenshot()');

Pros:

  • 🔧 Live debugging and control of Playwright sessions
  • 📡 SSH or WebSocket remote control of browsers
  • 🧪 Great for QA, automation, CI pipelines
  • 👥 Useful for shared dev environments and bots

⚠️ Cons:

  • 🔐 Security risks if exposed without sandbox/auth
  • 🐛 Harder to manage in clustered or multi-user setups
  • 🧱 Not supported natively — you build the infra
  • ⌛ Long-running sessions need manual cleanup

Summary

This approach gives you a live, programmable REPL interface into a running Playwright browser, perfect for debugging, teaching, QA, or live automation. By combining the flexibility of Node.js REPLs with the power of Playwright, you unlock an advanced dev tool few people know about — all without writing test suites or restarting sessions.


If this was helpful, you can support me here: Buy Me a Coffee

Comments 0 total

    Add comment