0
0
Selenium Pythontesting~7 mins

Cloud testing platforms (BrowserStack, Sauce Labs) in Selenium Python

Choose your learning style9 modes available
Introduction

Cloud testing platforms let you test your website or app on many browsers and devices without owning them. They save time and money.

You want to check if your website works on different browsers like Chrome, Firefox, Safari.
You need to test your app on mobile devices you don't have.
You want to run tests in parallel to finish faster.
You want to avoid setting up many test machines yourself.
You want to see screenshots or videos of your tests running remotely.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Example for BrowserStack
desired_cap = DesiredCapabilities.CHROME.copy()
desired_cap['browser'] = 'Chrome'
desired_cap['browser_version'] = 'latest'
desired_cap['os'] = 'Windows'
desired_cap['os_version'] = '10'
desired_cap['name'] = 'Sample Test'

username = 'YOUR_USERNAME'
access_key = 'YOUR_ACCESS_KEY'
remote_url = f'https://{username}:{access_key}@hub-cloud.browserstack.com/wd/hub'

driver = webdriver.Remote(
    command_executor=remote_url,
    desired_capabilities=desired_cap
)

# Use driver as usual
# driver.get('https://example.com')

# driver.quit()

Replace YOUR_USERNAME and YOUR_ACCESS_KEY with your actual credentials.

Desired capabilities tell the cloud platform which browser and OS to use.

Examples
This example shows how to test on Firefox browser on Mac OS.
Selenium Python
desired_cap = DesiredCapabilities.FIREFOX.copy()
desired_cap['browser'] = 'Firefox'
desired_cap['browser_version'] = 'latest'
desired_cap['os'] = 'OS X'
desired_cap['os_version'] = 'Monterey'
desired_cap['name'] = 'Firefox Test on Mac'

# This runs the test on latest Firefox on Mac OS Monterey
This example shows testing on a real mobile device using BrowserStack.
Selenium Python
desired_cap = DesiredCapabilities.CHROME.copy()
desired_cap['browser'] = 'Chrome'
desired_cap['browser_version'] = 'latest'
desired_cap['os'] = 'Android'
desired_cap['os_version'] = '12.0'
desired_cap['device'] = 'Samsung Galaxy S22'
desired_cap['real_mobile'] = 'true'
desired_cap['name'] = 'Mobile Test on Samsung'

# This runs the test on a real Samsung Galaxy S22 device
Sample Program

This script connects to BrowserStack, opens Google homepage, checks the page title contains 'Google', and then closes the browser.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time

# Setup desired capabilities for BrowserStack
desired_cap = DesiredCapabilities.CHROME.copy()
desired_cap['browser'] = 'Chrome'
desired_cap['browser_version'] = 'latest'
desired_cap['os'] = 'Windows'
desired_cap['os_version'] = '10'
desired_cap['name'] = 'Simple Google Test'

# Your BrowserStack credentials
username = 'YOUR_USERNAME'
access_key = 'YOUR_ACCESS_KEY'
remote_url = f'https://{username}:{access_key}@hub-cloud.browserstack.com/wd/hub'

# Create remote WebDriver
print('Starting remote WebDriver session...')
driver = webdriver.Remote(
    command_executor=remote_url,
    desired_capabilities=desired_cap
)

try:
    print('Opening Google homepage...')
    driver.get('https://www.google.com')
    time.sleep(3)  # Wait to see the page

    title = driver.title
    print(f'Page title is: {title}')

    assert 'Google' in title, 'Title does not contain Google'
    print('Assertion passed: Title contains Google')

finally:
    print('Quitting driver...')
    driver.quit()
OutputSuccess
Important Notes

Tests run on cloud platforms may be slower than local because of network delay.

Keep your credentials secret and do not share them publicly.

Use parallel testing features of these platforms to run many tests at once and save time.

Summary

Cloud testing platforms let you test on many browsers and devices without owning them.

You write Selenium tests as usual but connect to a remote URL with your credentials.

Set desired capabilities to choose browser, OS, and device for your test.