Skip to main content

Quality Assurance

A Beginner’s Guide to Running Selenium Tests on BrowserStack using Pytest

Female It Specialist

In today’s world, testing web applications across multiple browsers and devices is essential. One of the best tools for this is BrowserStack, a cloud-based platform that allows you to run Selenium tests on various real browsers and devices. In this blog, we will walk you through the process of integrating BrowserStack with Python Selenium and running tests using Pytest.

What is BrowserStack?

BrowserStack is a cloud-based testing platform that allows you to run automated tests on real browsers and devices, without needing to set up the infrastructure yourself. It supports various browsers like Chrome, Firefox, Safari, Edge, and even mobile devices like iPhones and Android phones.

Browserstack

Why Use BrowserStack with Selenium?

Selenium is a widely used tool for automating browsers, and with BrowserStack, you can run your Selenium tests on a wide range of browsers and operating systems. This ensures your application works seamlessly across different environments without needing to maintain your own testing infrastructure.

In this guide, we will use Pytest—a popular testing framework for Python—to run tests. This combination provides an efficient and flexible way to conduct cross-browser testing.

Prerequisites

Before you begin, ensure you have the following installed:

  1. Python (Version 3 or above)
  2. Selenium: Install it via pip
    pip install selenium
  3. Pytest: Install it via pip
    pip install pytest
  4. BrowserStack Account: Sign up at BrowserStack and get your username and access key.

Step 1: Set Up BrowserStack

Once you have an account on BrowserStack, you can find your username and access key from the BrowserStack dashboard.

You’ll use these credentials to authenticate your Selenium tests on BrowserStack.

Step 2: Configure Selenium to Use BrowserStack

To configure your Selenium WebDriver to run on BrowserStack, you need to specify BrowserStack’s remote URL, your credentials, and the desired capabilities of the browser or device you wish to test on.

Here is how to set it up:

from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# BrowserStack credentials
username = "your_browserstack_username"
access_key = "your_browserstack_access_key"

# Desired capabilities to run the test
desired_cap = {
    'browser': 'Chrome',
    'browser_version': 'latest',
    'os': 'Windows',
    'os_version': '10',
    'name': 'Python Selenium BrowserStack Test'  # Test name
}

# BrowserStack URL
url = f"https://{username}:{access_key}@hub-cloud.browserstack.com/wd/hub"

# Pytest Fixture to initialize the driver
@pytest.fixture(scope="function")
def driver():
    driver = webdriver.Remote(
        command_executor=url,
        desired_capabilities=desired_cap
    )
    yield driver
    driver.quit()

# Sample Test Case using Pytest
def test_browserstack(driver):
    driver.get("https://www.browserstack.com")
    assert "BrowserStack" in driver.title

    # Example of interaction
    search_box = driver.find_element(By.NAME, "q")
    search_box.send_keys("Selenium Python")
    search_box.send_keys(Keys.RETURN)

    # Assert something to validate test
    assert "Selenium" in driver.page_source

Key Points:

  • username and access_key: These values authenticate your test on BrowserStack.
  • desired_cap: This dictionary defines the configuration for the browser and OS you want to test on.
    • browser: The browser you want to test (Chrome, Firefox, etc.).
    • browser_version: Version of the browser (e.g., ‘latest’ or ’90’).
    • os: Operating system you want to run the tests on (Windows, macOS, Linux).
    • os_version: Version of the OS.
    • name: A custom name for the test.

Step 3: Running the Test with Pytest

To run the tests with Pytest, simply use the following command in your terminal:

pytest test_browserstack.py

This will trigger the test on BrowserStack, and you can view the results directly on the BrowserStack dashboard.

Step 4: Viewing Test Results on BrowserStack

Once your tests complete, you can visit the BrowserStack Dashboard to view detailed logs, screenshots, and videos of the tests. This can help in debugging any issues with your application.

Conclusion

Integrating BrowserStack with Python Selenium and Pytest is a powerful way to ensure your web application works across different browsers and devices. By using the cloud-based BrowserStack platform, you can avoid the hassle of setting up multiple testing environments and focus on writing effective tests. With Pytest’s simple yet effective testing capabilities, you can execute your cross-browser tests smoothly and efficiently.

If you run into any issues during the setup or execution, make sure to check the BrowserStack documentation for troubleshooting and advanced configuration options.

Happy testing!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ashika Meshram

Ashika Meshram is a Technical Consultant currently working on EHI. She is a coder girl fond of traveling and blogging.

More from this Author

Categories
Follow Us