-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathbrowserbox.js
More file actions
136 lines (116 loc) · 4.1 KB
/
Copy pathbrowserbox.js
File metadata and controls
136 lines (116 loc) · 4.1 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
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
/**
* BrowserBox Example - Browser automation with Playwright Server
*
* Demonstrates:
* - Starting browsers with Playwright Server (chromium, firefox, webkit)
* - Connection methods: playwrightEndpoint(), connect()
* - Multi-browser support
*/
import { BrowserBox } from '@boxlite-ai/boxlite';
import { chromium, firefox, webkit } from 'playwright-core';
const browserTypes = { chromium, firefox, webkit };
async function main() {
console.log('=== BrowserBox Example (Playwright Server) ===\n');
// Demo 1: All connection methods
console.log('1. Connection Methods Demo...\n');
await connectionMethodsDemo();
// Demo 2: All browsers work
console.log('\n2. Multi-Browser Demo (all browsers)...\n');
await multiBrowserDemo();
}
/**
* Demonstrates both connection methods:
* - playwrightEndpoint() + playwright.connect() - recommended for explicit control
* - connect() - convenience one-liner
*/
async function connectionMethodsDemo() {
// Method 1: playwrightEndpoint() + explicit Playwright connect (RECOMMENDED)
console.log(' Method 1: playwrightEndpoint() + playwright.connect()');
console.log(' (Recommended - gives you explicit control)\n');
{
const box = new BrowserBox({ browser: 'chromium' });
try {
// Get WebSocket endpoint
const wsEndpoint = await box.playwrightEndpoint();
console.log(` wsEndpoint: ${wsEndpoint}`);
// Connect using Playwright directly
const browser = await chromium.connect(wsEndpoint);
const page = await browser.newPage();
await page.goto('https://example.com');
console.log(` Page title: ${await page.title()}`);
await browser.close();
} finally {
await box.stop();
}
}
// Method 2: connect() - convenience one-liner
console.log('\n Method 2: connect() convenience method');
console.log(' (One-liner - auto-selects browser type)\n');
{
const box = new BrowserBox({ browser: 'chromium' });
try {
// One-liner: starts box and returns connected browser
const browser = await box.connect();
const page = await browser.newPage();
await page.goto('https://example.com');
console.log(` Page title: ${await page.title()}`);
await browser.close();
} finally {
await box.stop();
}
}
console.log('\n Both connection methods work!\n');
}
/**
* Multi-browser demo - tests all browsers with results tracking
*/
async function multiBrowserDemo() {
const browsers = ['chromium', 'firefox', 'webkit'];
const results = [];
for (const browserName of browsers) {
console.log(` Testing ${browserName}...`);
const box = new BrowserBox({
browser: browserName,
memoryMib: 2048,
cpus: 2,
// Use different ports for parallel testing
port: 3000 + browsers.indexOf(browserName)
});
try {
const wsEndpoint = await box.playwrightEndpoint();
console.log(` Endpoint: ${wsEndpoint}`);
// Connect using the matching browser type from Playwright
const playwright = browserTypes[browserName];
const browser = await playwright.connect(wsEndpoint);
const page = await browser.newPage();
await page.goto('https://example.com');
const title = await page.title();
console.log(` Title: ${title}`);
await browser.close();
results.push({ browser: browserName, status: 'PASSED', title });
console.log(` ${browserName}: PASSED\n`);
} catch (error) {
results.push({ browser: browserName, status: 'FAILED', error: error.message });
console.log(` ${browserName}: FAILED - ${error.message}\n`);
} finally {
await box.stop();
}
}
// Summary
console.log(' === SUMMARY ===');
for (const r of results) {
console.log(` ${r.browser}: ${r.status}${r.title ? ` (${r.title})` : ''}`);
}
const failed = results.filter(r => r.status === 'FAILED');
if (failed.length > 0) {
console.log(`\n ${failed.length} browser(s) failed`);
process.exit(1);
} else {
console.log('\n All browsers PASSED!\n');
}
}
// Run the example
main().catch(error => {
console.error('Error:', error);
process.exit(1);
});