0
0
Selenium Pythontesting~15 mins

Cloud testing platforms (BrowserStack, Sauce Labs) in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality on BrowserStack cloud platform
Preconditions (3)
Step 1: Open remote browser session on BrowserStack with Windows 10 and Chrome latest
Step 2: Navigate to 'https://example.testapp.com/login'
Step 3: Enter 'testuser@example.com' in the email input field with id 'email'
Step 4: Enter 'TestPass123' in the password input field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait until the dashboard page loads and URL contains '/dashboard'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with URL containing '/dashboard'
Automation Requirements - Selenium WebDriver with Python
Assertions Needed:
Verify current URL contains '/dashboard' after login
Verify login button is clickable before clicking
Verify email and password fields are present before input
Best Practices:
Use explicit waits to wait for elements and page load
Use remote WebDriver with desired capabilities for BrowserStack
Use try-except blocks to handle exceptions gracefully
Close the remote browser session after test
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# BrowserStack credentials and URL
BROWSERSTACK_USERNAME = 'your_username'
BROWSERSTACK_ACCESS_KEY = 'your_access_key'
remote_url = f'https://{BROWSERSTACK_USERNAME}:{BROWSERSTACK_ACCESS_KEY}@hub-cloud.browserstack.com/wd/hub'

# Desired capabilities for Windows 10 and Chrome latest
desired_cap = {
    'os': 'Windows',
    'os_version': '10',
    'browserName': 'Chrome',
    'browser_version': 'latest',
    'name': 'Login Test on BrowserStack'
}

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

    wait = WebDriverWait(driver, 15)

    # Navigate to login page
    driver.get('https://example.testapp.com/login')

    # Wait for email field and enter email
    email_field = wait.until(EC.presence_of_element_located((By.ID, 'email')))
    email_field.send_keys('testuser@example.com')

    # Wait for password field and enter password
    password_field = wait.until(EC.presence_of_element_located((By.ID, 'password')))
    password_field.send_keys('TestPass123')

    # Wait for login button to be clickable and click
    login_button = wait.until(EC.element_to_be_clickable((By.ID, 'loginBtn')))
    login_button.click()

    # Wait until URL contains '/dashboard'
    wait.until(EC.url_contains('/dashboard'))

    # Assertion: Verify URL contains '/dashboard'
    assert '/dashboard' in driver.current_url, 'Login failed or dashboard not loaded'

finally:
    driver.quit()

This script uses Selenium WebDriver with Python to run a test on BrowserStack cloud platform.

First, it sets up the remote WebDriver with BrowserStack credentials and desired capabilities for Windows 10 and Chrome latest.

It opens the login page URL, waits explicitly for the email and password fields to be present, then inputs the test credentials.

It waits for the login button to be clickable before clicking it.

After clicking, it waits until the URL contains '/dashboard' to confirm successful login.

Finally, it asserts that the URL contains '/dashboard' to verify the test passed.

The driver is quit in a finally block to ensure the browser session closes even if errors occur.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
{'mistake': 'Hardcoding local WebDriver instead of using remote WebDriver for BrowserStack', 'why_bad': "Tests won't run on BrowserStack cloud without remote WebDriver setup.", 'correct_approach': 'Use webdriver.Remote with BrowserStack hub URL and desired capabilities.'}
Not verifying elements are present or clickable before interacting
Not closing the browser session after test
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials to verify login success or failure.

Show Hint