Overview
Browserbase provides a consistent, isolated environment for running automated tests. This ensures:
Predictable test execution with fewer flaky tests
Built-in recording and debugging features
Ability to scale testing across concurrent sessions
Adjust browser settings to match your testing needs (viewport, geolocation, etc.)
Login Test Example
Let’s create a simple test to validate a login flow using Browserbase with different frameworks.
Follow Along: Web Scraping Example Step-by-step code for web scraping
Code Example
import { Stagehand } from "@browserbasehq/stagehand" ;
import { z } from "zod" ;
import dotenv from "dotenv" ;
async function main () {
dotenv . config ();
// initialize stagehand
const stagehand = new Stagehand ({
env: "BROWSERBASE" ,
});
await stagehand . init ();
const page = stagehand . page ;
async function automatedLoginTest ( inputs : any ) {
// Navigate to the form
await page . goto ( "https://www.saucedemo.com/" );
await page . act ( `Input Username: ${ inputs . username } ` );
await page . act ( `Input Password: ${ inputs . password } ` );
await page . act ( "Click Login Button" );
// take a screenshot of the page
await page . screenshot ({ path: "login_screenshot.png" });
// log the url
return page . url ();
}
const response = await automatedLoginTest ({
username: "standard_user" ,
password: "secret_sauce" ,
});
console . log ( response );
// close the stagehand session
await stagehand . close ();
}
main (). catch ( console . error );
Configuration Options
Set up a Consistent Environment
Test in a predictable environment with the same Chrome version every time.
// Create a standardized session
const session = await bb . sessions . create ({
projectId: process . env . BROWSERBASE_PROJECT_ID ,
// Optional: Configure viewport size (desktop, mobile)
browserSettings: {
viewport: {
width: 1280 ,
height: 720
}
}
});
Test from Different Locations
Test how your application behaves from different geographic locations:
// Create a session with proxy support
const session = await bb . sessions . create ({
projectId: process . env . BROWSERBASE_PROJECT_ID ,
proxies: [
{
type: "browserbase" ,
geolocation: {
city: "New York" ,
state: "NY" ,
country: "US"
}
}
]
});
Complete Configuration Options
Set the viewport, operating system, and full list of configuration options to customize your test environment.
Best Practices
Record Sessions
Every test session is automatically recorded and available in the Browserbase dashboard. With Session Replay , you can watch the exact test execution to debug failures efficiently.
console . log ( `View session replay at https://browserbase.com/sessions/ ${ session . id } ` );
No extra setup required —session recording happens automatically, allowing
you to diagnose flaky tests and failures with ease.
Add metadata for easier organization:
const session = await bb . sessions . create ({
projectId: process . env . BROWSERBASE_PROJECT_ID ,
userMetadata: {
testName: "login_flow" ,
suite: "authentication"
}
});
Capture Screenshots
Capture screenshots at critical points for easier debugging.
// Take a screenshot
await page . screenshot ({ path: "login_screenshot.png" });
Next Steps
Now that you understand the basics of automated testing with Browserbase, here are some features to explore next: