-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext-basic.ts
More file actions
50 lines (42 loc) · 1.42 KB
/
Copy pathcontext-basic.ts
File metadata and controls
50 lines (42 loc) · 1.42 KB
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
import { config } from 'dotenv';
import { chromium, type Browser } from 'playwright';
import { Lexmount } from 'lexmount';
config({ override: true });
async function main(): Promise<void> {
const client = new Lexmount();
let browser: Browser | undefined;
let createdContextId: string | undefined;
try {
const persistentContext = await client.contexts.create({
description: 'Quickstart login context',
metadata: {},
});
createdContextId = persistentContext.id;
console.log(`Context created: ${persistentContext.displayName} (${createdContextId})`);
const session = await client.sessions.create({
context: { id: createdContextId, mode: 'readWrite' },
});
try {
browser = await chromium.connectOverCDP(session.connectUrl);
const context = browser.contexts()[0];
const page = context?.pages()[0] ?? (await context?.newPage());
if (!page) {
throw new Error('No page available after connecting to the remote browser.');
}
await page.goto('https://www.baidu.com/');
console.log(`Session created with context: ${createdContextId}`);
} finally {
await browser?.close();
await session.close();
}
} finally {
if (createdContextId) {
await client.contexts.delete(createdContextId).catch(() => undefined);
}
client.close();
}
}
main().catch((error: unknown) => {
console.error(error);
process.exit(1);
});