Persistent Contexts
Browser Swarm introduces Persistent Contexts, enabling you to maintain browser state across multiple sessions. This feature is essential for scenarios requiring consistent user data, such as authentication, cookies, and local storage, thereby enhancing automation reliability and performance.
π Overview
By default, each browser session in Browser Swarm starts with a clean slateβno cookies, cache, or local storage. Persistent Contexts allow you to preserve this data between sessions, facilitating:
Seamless Authentication: Maintain login states without re-authenticating.
Performance Optimization: Leverage cached resources to reduce load times.
Stateful Interactions: Retain user preferences and session-specific data.
This capability is particularly beneficial for workflows that require continuity, such as automated testing, web scraping with login requirements, or any process where maintaining state enhances efficiency.
βοΈ Creating a Persistent Context
To utilize Persistent Contexts, you first need to create a context object that stores your session data. This context can then be associated with new browser sessions to retain the stored state.
Example
import { BrowserSwarm } from 'browser-swarm-sdk';
const bs = new BrowserSwarm({ apiKey: process.env.BROWSER_SWARM_API_KEY });
(async () => {
const context = await bs.contexts.create({
projectId: process.env.BROWSER_SWARM_PROJECT_ID,
});
console.log('Context ID:', context.id);
})();
The returned context.id
serves as a reference to the stored browser state, which can be reused in subsequent sessions.
π Launching a Session with a Persistent Context
After creating a context, you can initiate new browser sessions that utilize this stored state. This ensures continuity across sessions.
Example
const session = await bs.sessions.create({
projectId: process.env.BROWSER_SWARM_PROJECT_ID,
browserSettings: {
context: {
id: context.id,
persist: true
}
}
});
Setting persist: true
ensures that any changes made during the session are saved back to the context, allowing for updated state in future sessions.
π Security Considerations
Persistent Contexts may contain sensitive information, such as authentication tokens and personal data. Browser Swarm ensures security by:
Encryption at Rest: All context data is encrypted using AES-256-CBC.
Access Controls: Only authorized API keys can access specific contexts.
Data Isolation: Contexts are isolated per project to prevent cross-project data leakage.
Always handle context IDs securely and avoid exposing them in client-side code or logs.
π§ͺ Use Cases
Automated Testing: Maintain user sessions across multiple test cases.
Web Scraping: Access authenticated content without repeated logins.
Performance Testing: Measure load times with cached resources.
User Simulation: Emulate real user behavior over extended periods.
By leveraging Persistent Contexts, you can create more efficient and realistic automation scenarios.
π‘ Best Practices
Context Lifecycle Management: Regularly review and clean up unused contexts to maintain optimal performance.
Sensitive Data Handling: Avoid storing unnecessary sensitive information in contexts.
Version Control: Track changes to contexts to ensure consistency across environments.
Error Handling: Implement robust error handling when loading or saving contexts to prevent data corruption.
Implementing these practices will help maintain the integrity and security of your automation workflows.
By integrating Persistent Contexts into your Browser Swarm workflows, you enhance the capability to perform complex, stateful browser automation tasks efficiently and securely.
Last updated