Skip to main content
Back to Blog

Modernizing Your Test Automation: Upgrading from Selenium 3 to 4

Best PracticesJuly 12, 20226 min readQA Camp Team
ModernizingTest Auto-mationSELENIUM 3 TO 4 UPGRADEW3C - WEBDRIVER - DEVTOOLS - SHADOW DOM3LEGACYUPGRADE4MODERN{}{}Shadow DOMDevToolsW3CMIGRATION PATHReviewdeprecated APIUpdatedriversTeston stagingPromoteto production

Selenium has become the go-to open source framework for automating web browser testing. As web applications have evolved, so has Selenium, with the initial alpha release (4.0.0.alpha1) in May of 2019 marking a major leap forward. This post examines some of the key differences between Selenium 3 and Selenium 4.

Legacy Code Removal#

One of the biggest changes in Selenium 4 was a major code cleanup, removing a significant amount of deprecated and unused code from previous versions. This code cruft accumulated over the years as new features were added and old ones removed. By stripping out all this legacy code, Selenium 4 provides a leaner and faster framework. Tests will execute quicker without all that extra unused code taking up resources.

# Selenium 3
from selenium import webdriver

# Selenium 4
from selenium import webdriver
from selenium.webdriver.common.by import By

Modern Browser Compatibility#

With the rapid release cycles of modern browsers like Chrome, Firefox and Edge, it's important for Selenium to keep pace. Selenium 4 was built from the ground up to work with the latest generation of browsers. For example, it has better support for Chromium-based Edge, which changed significantly from the legacy Edge browser. It also works better with the latest Firefox releases. Selenium 3 is still compatible with most modern browsers, but may encounter issues with some newer browser versions.

# Selenium 3
driver = webdriver.Chrome()

# Selenium 4
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(options=options)

Handling of Promises#

Another key change is how Selenium 4 handles JavaScript Promises. With many web apps now built using frameworks like React and Angular that utilize Promises for asynchronous actions, properly handling them is vital for testing. Selenium 4 adapts the underlying WebDriver architecture to synchronize with Promises automatically. This enables it to smoothly test apps built on these modern frameworks. In contrast, Selenium 3 requires additional libraries like webdriver-manager to handle Promises.

New W3C Compliant Drivers#

Selenium 4 aligns the underlying WebDriver implementation with the W3C specification. This ensures closer compliance with standards set by browser vendors. All major browsers have moved to support the W3C WebDriver spec. By matching this spec, Selenium 4 can leverage the native browser capabilities more effectively. Selenium 3 uses the older JSON Wire Protocol for browser control. While still functional, the W3C approach is more robust.

Headless Testing#

Headless testing, where browsers run without a visible UI, has become popular for server-based test automation. Selenium 4 improves support for headless testing. For example, it fixes an issue with taking screenshots in headless mode in Firefox. Overall, running tests headlessly is smoother and has fewer glitches. Selenium 3 can also run browsers headlessly but requires more configuration and doesn't have full support across all browsers.

Mobile Testing#

While Selenium is predominantly used for web testing, it does provide APIs for testing mobile apps as well. Selenium 4 builds upon the mobile testing capabilities of previous versions. For example, it supports directly inspecting elements within hybrid mobile apps without needing to use developer tools. For native apps, Selenium 4 enables inspector tools like Appium to directly connect to a WebDriver session. This improves testability of elements in mobile apps.

Deferred Requests#

To better handle asynchronous actions, Selenium 4 introduces deferred requests. This allows actions to be queued up and executed only when previous actions have completed. For example if you need to wait for an element to load before clicking it, deferred requests will automatically handle this. In Selenium 3, you would need to manually write waits and sleeps to handle asynchronicity. Deferred requests save you from much of this boilerplate code.

# Selenium 3
button = driver.find_element(By.ID, "submit")
time.sleep(5)  # wait for page to load
button.click()

# Selenium 4
button = driver.find_element(By.ID, "submit")
driver.execute_async_script("""
let button = arguments[0];
button.click();
""", button)

Shadow DOM Support#

With the increasing use of shadow DOMs and web components in JavaScript frameworks, Selenium 4 puts emphasis on improving support for testing these. It adds locator strategies like CSS selector and XPath to allow targeting shadow DOM elements. Selenium 3 did not have native support for shadow DOMs, requiring workarounds and extensions to test them.

Improved Security#

Security is always a priority in test automation frameworks. Selenium 4 strengthens security on multiple fronts:

  • It sandboxes browser instances created during testing for better isolation
  • It enables communication over secure HTTPS connections to prevent snooping or tampering with commands
  • Unnecessary privileges have been stripped from processes spawned by Selenium

Together these enhancements reduce risk and improve reliability.

Debugging Capabilities#

Selenium 4 introduces a DevTools component that allows inspecting the browser state using the Chrome DevTools Protocol. This aids in understanding and debugging tests, providing insights similar to manually using DevTools. Selenium 3 does not have native support for DevTools-based debugging, requiring external libraries. The built-in debugging in Selenium 4 makes it easier to diagnose issues.

Backwards Compatibility#

For the most part, Selenium 4 maintains backwards compatibility with Selenium 3. It retains support for core APIs and concepts like WebDrivers, locators, PageObjects and more. This allows those already using Selenium 3 to upgrade without needing to drastically modify their existing automated tests. However, some deprecated methods and classes have been removed in Selenium 4, so minor code changes may be required.

Ongoing Support#

Selenium 3 is still supported for the time being. However, Selenium 4 is positioned as the future direction of the framework. New features, browser support and fixes will be focused on Selenium 4 going forward. So while not absolutely necessary yet, upgrading provides access to all the latest enhancements.

Making the Switch#

For teams looking to upgrade, here are some practical tips:

  • Review your tests and framework for use of any deprecated functionality that's been removed in Selenium 4 - these will need to be updated
  • Take advantage of new capabilities like deferred requests and DevTools to improve reliability and debuggability
  • Run comparative testing on both versions to uncover any differences in behavior across browsers
  • Update Selenium first in your staging/QA environment, then gradually promote to production after verification
  • Provide training to your test automation engineers on changes from Selenium 3 to 4
  • Update any browser drivers and plugins used by tests to latest versions compatible with Selenium 4

Migrating from Selenium 3 to 4 requires some effort, but brings many future-proofing benefits. The architectural improvements and support for modern web technologies make it a worthwhile upgrade for most test automation frameworks. Carefully planned, the migration can improve the maintainability and reliability of browser test suites.

Related Articles

Web ApplicationsTestingFeaturesPART 1UI · FORMS · NAVIGATION · EDGE CASEShttps://app.example.comForm ValidationNavigation FlowError HandlingSTARTOK?PASSFAIL
Best Practices

Web Applications Testing Features — Part 1

Explore the key features and considerations when testing web applications.

February 12, 20266 min read
Read More
SecurityTestingPhases, Types, and Best Practices<PLANNING TO GO-LIVE>4 PHASES - END-TO-END SECURITY TESTINGSECURITY TEST FLOWSECURITY TEST FLOW01PLANNING02LEARNING03ATTACKING04GO-LIVE
Best Practices

Security Testing: Phases, Types, and Best Practices

A structured guide to planning, executing, and reporting security assessments — from scoping to remediation.

February 3, 20268 min read
Read More
UsabilityTestingCustomers Come First<USER EXPERIENCE MATTERS>REAL USERS - REAL FEEDBACK - REAL RESULTSHEROSIGN UPbad"Couldn't find thebutton anywhere…"good"Super intuitive, foundeverything instantly!"FEEDBACKUSER
Best Practices

Usability Testing: Customers Come First

How usability testing helps create products that resonate.

January 20, 20266 min read
Read More

Need Expert QA for Your Project?

Let our team help you deliver software your users will love.

Get a Free Consultation