0
0
Selenium Pythontesting~8 mins

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

Choose your learning style9 modes available
Framework Mode - Cloud testing platforms (BrowserStack, Sauce Labs)
Folder Structure
cloud-testing-project/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   └── __init__.py
├── pages/
│   ├── login_page.py
│   ├── checkout_page.py
│   └── __init__.py
├── utils/
│   ├── cloud_driver.py
│   ├── config.py
│   └── __init__.py
├── reports/
│   └── test_report.html
├── requirements.txt
└── pytest.ini
  
Test Framework Layers
  • Cloud Driver Layer: Handles connection to cloud platforms like BrowserStack or Sauce Labs using remote WebDriver.
  • Page Objects: Classes representing web pages with locators and actions.
  • Test Cases: Scripts using pytest that run tests on cloud browsers.
  • Utilities: Configuration management, helper functions, and cloud driver setup.
  • Reports: Stores test execution reports for analysis.
Configuration Patterns

Use a config.py file or environment variables to manage:

  • Cloud platform credentials (username, access key)
  • Browser and OS combinations
  • Test environment URLs

Example snippet from cloud_driver.py to create remote WebDriver session:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import os

BROWSERSTACK_USERNAME = os.getenv('BROWSERSTACK_USERNAME')
BROWSERSTACK_ACCESS_KEY = os.getenv('BROWSERSTACK_ACCESS_KEY')

caps = DesiredCapabilities.CHROME.copy()
caps['browser'] = 'Chrome'
caps['browser_version'] = 'latest'
caps['os'] = 'Windows'
caps['os_version'] = '11'
caps['name'] = 'Sample Test'

url = f"https://{BROWSERSTACK_USERNAME}:{BROWSERSTACK_ACCESS_KEY}@hub-cloud.browserstack.com/wd/hub"


def get_cloud_driver():
    driver = webdriver.Remote(
        command_executor=url,
        desired_capabilities=caps
    )
    return driver
  
Test Reporting and CI/CD Integration
  • Use pytest-html plugin to generate HTML reports stored in reports/ folder.
  • Integrate tests with CI tools like GitHub Actions or Jenkins to run tests on every code push.
  • Configure environment variables securely in CI for cloud platform credentials.
  • Use cloud platform dashboards (BrowserStack or Sauce Labs) to view live test sessions and logs.
Best Practices
  • Use environment variables: Never hardcode credentials; keep them secure.
  • Parameterize browsers and OS: Run tests on multiple configurations easily.
  • Explicit waits: Use waits to handle dynamic page elements reliably.
  • Page Object Model: Keep locators and actions separate from tests for maintainability.
  • Clean up sessions: Always quit the remote driver to avoid resource leaks on cloud platforms.
Self Check

Where in this folder structure would you add a new page object for the "User Profile" page?

Key Result
A cloud testing framework uses remote WebDriver sessions with secure config and page objects to run tests on BrowserStack or Sauce Labs.