Run Cross Browser Tests with Selenium: Step-by-Step
In this article
Cross-browser testing with Selenium lets you verify that your web application works correctly across different browsers (Chrome, Firefox, Safari, Edge) and operating systems. This guide walks you through setting up and running Selenium WebDriver tests in multiple browsers, using Python as the example language, though the concepts apply to Java, C#, or JavaScript.
What You'll Need
- Python 3.7+ installed (or your preferred programming language)
- pip (Python package manager)
- A code editor or IDE
- Web browsers installed: Chrome, Firefox, Safari (macOS only), Edge
- WebDriver binaries for each browser (e.g., chromedriver, geckodriver)
- Basic knowledge of Selenium WebDriver and writing test scripts
Step-by-Step
- Install Selenium
Open a terminal and run:pip install selenium. This installs the Selenium bindings for Python. - Download WebDriver binaries
Each browser needs its own driver. For Chrome, download ChromeDriver from the official site; for Firefox, download GeckoDriver; for Edge, Microsoft Edge Driver. Place them in a known directory or add to PATH. - Write a basic test script
Create a Python file, e.g.,test_example.py. Start with a simple test that opens a website and checks the title: - Parameterize the browser
Instead of hardcoding a browser, use a variable or command-line argument. For example, usesys.argvto accept a browser name, or use environment variables. - Set up WebDriver for each browser
In your script, create a function that returns the appropriate WebDriver instance based on the browser name. For Chrome:webdriver.Chrome(); Firefox:webdriver.Firefox(); Edge:webdriver.Edge(); Safari:webdriver.Safari()(macOS only). - Run the test in one browser
Execute the script with the browser name as an argument, e.g.,python test_example.py chrome. Verify it works. - Run in multiple browsers sequentially
Create a loop that iterates over a list of browsers and runs the test for each. For example: - Handle browser-specific options
Some browsers need specific options (e.g., headless mode, disabling GPU). UseOptionsclasses. For Chrome:Options.add_argument("--headless")for headless testing. - Use a test framework
Integrate with pytest or unittest to manage tests, fixtures, and reporting. This allows you to run the same test across browsers with minimal code duplication. - Run tests in parallel (optional)
To speed up, use pytest-xdist or Selenium Grid to run tests in multiple browsers simultaneously. This requires more setup but is efficient for large suites. - Capture results and logs
After each test, take a screenshot on failure and log the browser and environment. This helps debug cross-browser issues.
Common Mistakes
- Not matching WebDriver version with browser version. Always check compatibility.
- Hardcoding browser-specific code without abstraction, making tests hard to maintain.
- Forgetting to close the WebDriver instance, causing memory leaks.
- Using the same test logic for all browsers without considering browser-specific behavior (e.g., CSS differences).
- Not using waits (implicit or explicit) leading to flaky tests.
Tips
- Use explicit waits (
WebDriverWait) instead oftime.sleep()to make tests more reliable. - Keep WebDriver binaries in a central location and update them regularly.
- Use a configuration file (e.g., JSON or YAML) to define browser settings, so you can change them without editing code.
- Run tests in headless mode during CI to save resources, but also run in headed mode locally to see what's happening.
- Take screenshots on failure and attach them to your test report for easier debugging.
- Use Selenium Grid or cloud services (like Sauce Labs or BrowserStack) if you need to test on many browser/OS combos.
Troubleshooting
- WebDriver not found: Ensure the driver executable is in PATH or specify its path when creating the driver instance.
- SessionNotCreatedException: Usually means the WebDriver version doesn't match the browser version. Update both.
- Element not found in one browser but not another: Check for CSS or XPath differences; use more robust locators like IDs or data attributes.
- Tests hang or time out: Increase timeouts or check for infinite waits. Use explicit waits with a timeout.
- Browser opens but no action happens: Check if the browser is in headless mode and if the page is loading correctly. Add a wait for page load.
Frequently Asked Questions
Can I run Selenium tests on Safari?
Yes, Safari is supported on macOS only. You need to enable the 'Allow Remote Automation' option in Safari's Develop menu and use webdriver.Safari().
Do I need to download WebDriver for each browser?
Yes, each browser has its own driver. Chrome uses ChromeDriver, Firefox uses GeckoDriver, Edge uses EdgeDriver, and Safari has built-in driver support.
Can I run tests in parallel across browsers?
Yes, you can use Selenium Grid to distribute tests across multiple machines or browsers, or use pytest-xdist for parallel execution on the same machine.
What is the difference between implicit and explicit waits?
Implicit wait tells the WebDriver to poll the DOM for a certain amount of time when trying to find an element. Explicit wait waits for a specific condition (like element visibility) with a timeout. Explicit waits are more flexible and recommended.
How do I handle browser-specific code in my tests?
Use conditional logic based on the browser name, or use capabilities and options to adjust behavior. For example, you might need different CSS selectors for some browsers.
Is it necessary to test on all browsers?
It depends on your target audience. At minimum, test on the most popular browsers (Chrome, Firefox, Safari, Edge) and versions that your users are likely to use. Use analytics to decide.