-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
156 lines (141 loc) · 4.28 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* 🤘 Welcome to Stagehand!
*
* TO RUN THIS PROJECT:
* ```
* npm install
* npm run start
* ```
*
* In this quickstart, we'll be automating a parking ticket payment process using Playwright and Stagehand's AI features.
*
* 1. Navigate to the SF Municipal Transportation Agency payment portal
* 2. Enter license plate information
* 3. Select tickets to pay
* 4. Fill out payment information using Stagehand's AI-powered form filling
*/
import StagehandConfig from "./stagehand.config.ts";
import { Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod";
import chalk from "chalk";
import boxen from "boxen";
import dotenv from "dotenv";
dotenv.config();
function announce(message: string, title?: string) {
console.log(
boxen(message, {
padding: 1,
margin: 3,
title: title || "Stagehand",
})
);
}
async function main() {
console.log(
[
`🤘 ${chalk.yellow("Welcome to Stagehand!")}`,
"",
"Stagehand is a tool that allows you to automate browser interactions.",
"In this quickstart, we'll be automating a parking ticket payment on the San Francisco Municipal Transportation Agency website.",
"",
`1. Navigate to ${chalk.blue("https://wmq.etimspayments.com/pbw/include/sanfrancisco/input.jsp")}`,
`2. Use ${chalk.green("act")} to enter the license plate information`,
`3. Select all tickets and submit the form`,
`4. Fill out the payment information using ${chalk.green("act")} with variables`,
"",
`${chalk.bold(chalk.green("PRESS ENTER TO CONTINUE..."))}`,
].join("\n")
);
await new Promise((resolve) => {
process.stdin.once("data", () => {
resolve(undefined);
});
});
const stagehand = new Stagehand({
...StagehandConfig,
});
await stagehand.init();
const page = stagehand.page;
if (StagehandConfig.env === "BROWSERBASE") {
console.log(
boxen(
`View this session live in your browser: \n${chalk.blue(
`https://browserbase.com/sessions/${stagehand.browserbaseSessionID}`
)}`,
{
title: "Browserbase",
padding: 1,
}
)
);
}
await stagehand.page.goto(
"https://wmq.etimspayments.com/pbw/include/sanfrancisco/input.jsp",
);
await stagehand.act({
action:
"Enter the plate number 8XWY857 and then search for citations.",
});
await stagehand.act({
action:
"Select the 'I would like to pay for all tickets' radio button. Then click the 'Submit' button.",
});
await stagehand.act({
action: "Enter the following information:\n" +
"Name on the card: %nameOnCard%\n" +
"Address: %address%\n" +
"City: %city%\n" +
"State: %state%\n" +
"Zip code: %zip%" +
"Card number: %cardNumber%\n" +
"Expiration date month: %expirationDateMonth%\n" +
"Expiration date year: %expirationDateYear%\n" +
"CVV: %cvv%\n",
variables: {
cardNumber: "4111111111111111",
expirationDateMonth: "05 - May",
expirationDateYear: "2025",
cvv: "123",
nameOnCard: "John Doe",
address: "123 Main St",
city: "San Francisco",
state: "CA",
zip: "94101"
},
});
await stagehand.act({
action: "Click the 'Submit' button.",
});
await stagehand.close();
if (StagehandConfig.env === "BROWSERBASE") {
console.log(
"Session completed. Waiting for 10 seconds to see the logs and recording..."
);
// Wait for 10 seconds to see the logs
await new Promise((resolve) => setTimeout(resolve, 10000));
console.log(
boxen(
`View this session recording in your browser: \n${chalk.blue(
`https://browserbase.com/sessions/${stagehand.browserbaseSessionID}`
)}`,
{
title: "Browserbase",
padding: 1,
margin: 3,
}
)
);
} else {
console.log(
"We hope you enjoyed using Stagehand locally! On Browserbase, you can bypass captchas, replay sessions, and access unparalleled debugging tools!\n10 free sessions: https://www.browserbase.com/sign-up\n\n"
);
}
console.log(
`🤘 Thanks for using Stagehand! Create an issue if you have any feedback: ${chalk.blue(
"https://github.com/browserbase/stagehand/issues/new"
)}\n`
);
}
(async () => {
await main().catch(console.error);
})();