Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e41d40d

Browse files
committed
selenium uploads
1 parent 4b5d790 commit e41d40d

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

examples/selenium-upload.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import http, { type ClientRequest } from 'node:http';
2+
import { join } from 'node:path';
3+
import { Builder, By, until } from 'selenium-webdriver';
4+
import Browserbase from 'browserbase/index';
5+
import fs from 'node:fs';
6+
// Configuration
7+
const BROWSERBASE_PROJECT_ID = process.env['BROWSERBASE_PROJECT_ID']!;
8+
const BROWSERBASE_API_KEY = process.env['BROWSERBASE_API_KEY']!;
9+
const UPLOAD_TEST_URL = 'https://browser-tests-alpha.vercel.app/api/upload-test';
10+
const UPLOAD_TIMEOUT = 10_000; // 10 seconds
11+
12+
const bb = new Browserbase({
13+
apiKey: BROWSERBASE_API_KEY,
14+
});
15+
16+
async function testFileUpload() {
17+
try {
18+
// Create a new session
19+
console.log('Creating a new session...');
20+
const session = await bb.sessions.create({
21+
projectId: BROWSERBASE_PROJECT_ID,
22+
});
23+
console.log(`Session created with ID: ${session.id}`);
24+
25+
// Upload file to session
26+
const localFilePath = join(__dirname, 'packages/logo.png');
27+
const remoteFilePath = '/tmp/.uploads/logo.png';
28+
29+
console.log('Uploading file to session...');
30+
await bb.sessions.uploads.create(session.id, {
31+
file: fs.createReadStream(localFilePath),
32+
});
33+
34+
// Configure HTTP agent for Selenium
35+
const customHttpAgent = new http.Agent({});
36+
// @ts-expect-error -- accessing private method
37+
customHttpAgent.addRequest = (req: ClientRequest, options: unknown) => {
38+
req.setHeader('session-id', session.id);
39+
req.setHeader('x-bb-api-key', BROWSERBASE_API_KEY);
40+
// @ts-expect-error -- accessing private method
41+
(http.Agent.prototype.addRequest as (...args: any[]) => unknown).call(customHttpAgent, req, options);
42+
};
43+
44+
// Configure Selenium WebDriver
45+
console.log('Connecting to browser...');
46+
const driver = new Builder()
47+
.forBrowser('chrome')
48+
.usingHttpAgent(customHttpAgent)
49+
.usingServer(session.seleniumRemoteUrl)
50+
.build();
51+
52+
// Navigate to upload test page
53+
console.log('Navigating to upload test page...');
54+
await driver.get(UPLOAD_TEST_URL);
55+
56+
// Perform file upload
57+
console.log('Uploading file...');
58+
const fileInput = await driver.wait(until.elementLocated(By.css('#fileUpload')), UPLOAD_TIMEOUT);
59+
await fileInput.sendKeys(remoteFilePath);
60+
61+
// Verify upload
62+
const fileNameSpan = await driver.findElement(By.css('#fileName'));
63+
const fileName = await fileNameSpan.getText();
64+
65+
const fileSizeSpan = await driver.findElement(By.css('#fileSize'));
66+
const fileSize = Number(await fileSizeSpan.getText());
67+
68+
console.log('Upload results:');
69+
console.log('- File name:', fileName);
70+
console.log('- File size:', fileSize, 'bytes');
71+
72+
if (fileName === 'logo.png' && fileSize > 0) {
73+
console.log('File upload successful!');
74+
} else {
75+
throw new Error('File upload verification failed');
76+
}
77+
78+
// Optional: Take screenshot for debugging
79+
// const screenshot = await driver.takeScreenshot();
80+
// await writeFile('selenium-upload.png', screenshot, 'base64');
81+
82+
// Cleanup
83+
await driver.quit();
84+
85+
console.log('Upload test completed successfully!');
86+
console.log(`View session replay at https://browserbase.com/sessions/${session.id}`);
87+
} catch (error) {
88+
console.error('An error occurred:', error);
89+
process.exit(1);
90+
}
91+
}
92+
93+
// Run the script
94+
testFileUpload();

0 commit comments

Comments
 (0)