|
| 1 | +/** |
| 2 | + * This example shows how to use observe({ variables }) to plan a sensitive |
| 3 | + * login flow, validate the returned placeholder actions, and then execute them |
| 4 | + * with act(). |
| 5 | + * |
| 6 | + * observe() returns %variableName% placeholders in action arguments. That lets |
| 7 | + * you review the planned actions before any real secret values are used. |
| 8 | + */ |
| 9 | +import { Action, Stagehand } from "../lib/v3/index.js"; |
| 10 | +import chalk from "chalk"; |
| 11 | + |
| 12 | +const variables = { |
| 13 | + username: "test@browserbase.com", |
| 14 | + password: "stagehand=goated", |
| 15 | +}; |
| 16 | + |
| 17 | +const loginInstruction = [ |
| 18 | + "Fill the login form using the available variables.", |
| 19 | + "Use %username% for the email field.", |
| 20 | + "Use %password% for the password field.", |
| 21 | + "Include the field name in each action description.", |
| 22 | +].join(" "); |
| 23 | + |
| 24 | +function findValidatedAction( |
| 25 | + observed: Action[], |
| 26 | + placeholder: string, |
| 27 | + keywords: string[], |
| 28 | +): Action { |
| 29 | + const matches = observed.filter((action) => { |
| 30 | + const description = action.description.toLowerCase(); |
| 31 | + return ( |
| 32 | + action.arguments?.includes(placeholder) === true && |
| 33 | + keywords.some((keyword) => description.includes(keyword)) |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + if (matches.length !== 1) { |
| 38 | + throw new Error( |
| 39 | + `Expected exactly one safe action for ${placeholder}, found ${matches.length}`, |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + return matches[0]; |
| 44 | +} |
| 45 | + |
| 46 | +async function observeVariablesLogin() { |
| 47 | + const stagehand = new Stagehand({ |
| 48 | + env: "BROWSERBASE", |
| 49 | + verbose: 1, |
| 50 | + }); |
| 51 | + |
| 52 | + await stagehand.init(); |
| 53 | + |
| 54 | + try { |
| 55 | + const page = stagehand.context.pages()[0]; |
| 56 | + |
| 57 | + await page.goto("https://v0-modern-login-flow.vercel.app/", { |
| 58 | + waitUntil: "networkidle", |
| 59 | + timeoutMs: 30000, |
| 60 | + }); |
| 61 | + |
| 62 | + const observed = await stagehand.observe(loginInstruction, { |
| 63 | + variables, |
| 64 | + }); |
| 65 | + |
| 66 | + console.log( |
| 67 | + `${chalk.green("Observe:")} Placeholder actions found:\n${observed |
| 68 | + .map( |
| 69 | + (action) => |
| 70 | + `${chalk.yellow(action.description)} -> ${chalk.blue(action.arguments?.join(", ") || "no arguments")}`, |
| 71 | + ) |
| 72 | + .join("\n")}`, |
| 73 | + ); |
| 74 | + |
| 75 | + const emailAction = findValidatedAction(observed, "%username%", ["email"]); |
| 76 | + const passwordAction = findValidatedAction(observed, "%password%", [ |
| 77 | + "password", |
| 78 | + ]); |
| 79 | + |
| 80 | + console.log( |
| 81 | + `\n${chalk.green("Validated:")} Safe actions to execute:\n${[ |
| 82 | + emailAction, |
| 83 | + passwordAction, |
| 84 | + ] |
| 85 | + .map( |
| 86 | + (action) => |
| 87 | + `${chalk.yellow(action.description)} -> ${chalk.blue(action.arguments?.[0] || "no value")}`, |
| 88 | + ) |
| 89 | + .join("\n")}`, |
| 90 | + ); |
| 91 | + |
| 92 | + await stagehand.act(emailAction, { variables }); |
| 93 | + await stagehand.act(passwordAction, { variables }); |
| 94 | + |
| 95 | + const [submitButton] = await stagehand.observe("find the sign in button"); |
| 96 | + |
| 97 | + if (!submitButton) { |
| 98 | + throw new Error("Could not find the sign in button"); |
| 99 | + } |
| 100 | + |
| 101 | + await stagehand.act(submitButton); |
| 102 | + console.log( |
| 103 | + chalk.green( |
| 104 | + "\nSubmitted login form. Waiting 10 seconds before closing...", |
| 105 | + ), |
| 106 | + ); |
| 107 | + await page.waitForTimeout(10000); |
| 108 | + } finally { |
| 109 | + await stagehand.close(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +(async () => { |
| 114 | + await observeVariablesLogin(); |
| 115 | +})(); |
0 commit comments