← back to Pattern Vault

scripts/signup/step.js

47 lines

// Step driver: run one action against the persistent Browserbase session,
// then dump page evidence (inputs/buttons/links + screenshot).
//   node step.js goto <url> <tag>
//   node step.js click <text-or-selector> <tag>     (text matched on buttons/links)
//   node step.js fill <selector> <value> <tag>
//   node step.js press <key> <tag>
//   node step.js explore <tag>
//   node step.js eval "<js>" <tag>
const { connect, explore, killPopups } = require('./bb');

(async () => {
  const [cmd, a, b, c] = process.argv.slice(2);
  const { browser, page } = await connect();
  try {
    if (cmd === 'goto') {
      await page.goto(a, { waitUntil: 'domcontentloaded', timeout: 60000 });
      await page.waitForTimeout(3500);
      await explore(page, b || 'goto');
    } else if (cmd === 'click') {
      await killPopups(page);
      let target;
      if (a.startsWith('css=')) target = page.locator(a.slice(4)).first();
      else target = page.locator(`button:has-text("${a}"), [role="button"]:has-text("${a}"), a:has-text("${a}"), input[type="submit"][value*="${a}"]`).first();
      await target.click({ timeout: 15000 });
      await page.waitForTimeout(3500);
      await explore(page, b || 'click');
    } else if (cmd === 'fill') {
      await killPopups(page);
      await page.locator(a).first().fill(b, { timeout: 15000 });
      await page.waitForTimeout(400);
      await explore(page, c || 'fill');
    } else if (cmd === 'press') {
      await page.keyboard.press(a);
      await page.waitForTimeout(3500);
      await explore(page, b || 'press');
    } else if (cmd === 'eval') {
      const out = await page.evaluate(a);
      console.log(JSON.stringify(out, null, 1));
      await explore(page, b || 'eval');
    } else {
      await explore(page, a || 'explore');
    }
  } finally {
    await browser.close(); // detach only; keepAlive holds the session
  }
})().catch((e) => { console.error('STEP-ERR', e.message); process.exit(1); });