BrowserStack Python SDK (Legacy)

1.45.0 · deprecated · verified Fri Apr 17

This is a legacy Python SDK for integrating Selenium-webdriver tests with BrowserStack. It is officially deprecated and no longer actively maintained. BrowserStack now recommends using standard Selenium with BrowserStack capabilities directly. The last release of this SDK was 1.45.0 in April 2022.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how the deprecated `browserstack-sdk` was typically used for setting up BrowserStack Local testing with Selenium. It is provided for historical context and to highlight functionality unique to the SDK. For new projects, it is highly recommended to follow the official BrowserStack documentation for setting up Selenium tests without this SDK.

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from browserstack.local import Local # Specific to the DEPRECATED SDK

# --- WARNING: This SDK is deprecated. Use standard Selenium setup for new projects. ---

# Configuration using environment variables
BROWSERSTACK_USERNAME = os.environ.get('BROWSERSTACK_USERNAME', 'YOUR_USERNAME')
BROWSERSTACK_ACCESS_KEY = os.environ.get('BROWSERSTACK_ACCESS_KEY', 'YOUR_ACCESS_KEY')

# Desired capabilities for the test
desired_cap = {
    'bstack:options': {
        'userName': BROWSERSTACK_USERNAME,
        'accessKey': BROWSERSTACK_ACCESS_KEY,
        'buildName': 'BrowserStack Python SDK Example (Legacy)',
        'sessionName': 'Local Test with Deprecated SDK',
        'local': 'true' # Enable local testing
    },
    'browserName': 'Chrome',
    'browserVersion': 'latest',
    'os': 'Windows',
    'osVersion': '10'
}

bs_local = None
driver = None

try:
    # Start BrowserStack Local using the deprecated SDK's Local class
    bs_local = Local()
    bs_local_args = {
        "key": BROWSERSTACK_ACCESS_KEY,
        "forcelocal": "true"
    }
    print("Starting BrowserStack Local tunnel...")
    bs_local.start(bs_local_args)
    print("BrowserStack Local tunnel started.")

    # Initialize Selenium WebDriver connected to BrowserStack hub
    driver = webdriver.Remote(
        command_executor='https://hub.browserstack.com/wd/hub',
        desired_capabilities=desired_cap
    )

    # Perform a test that requires local access
    driver.get("http://bs-local.com:45454/")
    print(f"Page title: {driver.title}")

    # Example assertion (this specific page is part of BrowserStack Local setup)
    if "BrowserStack Local" in driver.title:
        print("Test Passed: Successfully accessed local URL through BrowserStack Local.")
    else:
        print("Test Failed: Could not access local URL.")

finally:
    # Stop BrowserStack Local tunnel
    if bs_local:
        print("Stopping BrowserStack Local tunnel...")
        bs_local.stop()
        print("BrowserStack Local tunnel stopped.")

    # Quit the WebDriver session
    if driver:
        driver.quit()
        print("WebDriver session quit.")

# To run this example:
# 1. Set BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.
# 2. pip install browserstack-sdk selenium
# 3. python your_script.py

view raw JSON →