Browser Swarm Docs
  • πŸš€Welcome
    • What is Browser Swarm?
  • Understanding Headless Browsers
  • Quickstart Guide
  • Framework Compatibility
  • 🌟 Core Concepts
    • Starting a Browser Task
  • Interacting with Browser Tasks
  • Task Lifecycle Management
  • βš™οΈAdvanced Features
    • Stealth Automation
  • Proxy Integration
  • Real-Time Monitoring
  • Responsive Viewports
  • Session Debugging & Replay
  • File Handling (Downloads & Uploads)
  • Capturing Screenshots & PDFs
  • Persistent Contexts
  • Extension Support
  • Session Tagging & Metadata
  • 🎯Practical Examples
    • Automating Form Interactions
  • Efficient Data Scraping
  • Automated Web Testing
  • Cost Optimization Strategies
  • Handling Extended Tasks
  • Selecting Execution Regions
  • Monitoring Resource Usage
  • Leveraging Task Metadata
  • Pricing and Subscription
  • Account and Team Management
  • Managing Limits and Concurrency
  • Authentication Automation
  • Security Best Practices
  • πŸ”ŒEcosystem Integrations
    • Integration Overview
  • πŸ’»Developer Resources
    • APIs and SDKs Overview
  • Node.js Integration
  • Python Integration
  • Browser Task API
  • Project Management API
  • Context Management API
  • Browser Extensions API
  • πŸ™‹β€β™‚οΈSupport & Resources
    • Getting Help
  • Dashboard Overview
  • FAQs & Troubleshooting
Powered by GitBook
On this page
  • πŸ” Overview
  • βš™οΈ Creating a Persistent Context
  • πŸš€ Launching a Session with a Persistent Context
  • πŸ” Security Considerations
  • πŸ§ͺ Use Cases
  • πŸ’‘ Best Practices

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);
})();
from browser_swarm import BrowserSwarm
import os

bs = BrowserSwarm(api_key=os.environ["BROWSER_SWARM_API_KEY"])

context = bs.contexts.create(
    project_id=os.environ["BROWSER_SWARM_PROJECT_ID"]
)

print(f"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
    }
  }
});
session = bs.sessions.create(
    project_id=os.environ["BROWSER_SWARM_PROJECT_ID"],
    browser_settings={
        "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.

PreviousCapturing Screenshots & PDFsNextExtension Support

Last updated 1 month ago