Skip to content

Conversation

manuelsblanco
Copy link
Contributor

@manuelsblanco manuelsblanco commented Oct 13, 2025

User description

💥 What does this PR do?

This PR adds a new capability to RemoteWebDriver: allowing the browser version (browserVersion) to be retrieved programmatically from the driver’s capabilities. A getBrowserVersion() method is added to extract that information safely. An integration test (ShowBrowserVersionIntegrationTest) is included to demonstrate its usage in a real browser session, printing the version to the console and asserting that it is neither null nor empty.

🔧 Implementation Notes
• The getBrowserVersion() method in RemoteWebDriver reads the browserVersion key from the capabilities object, converts it to string if present, or returns an empty string otherwise, ensuring compatibility with drivers that don’t provide it.
• No additional dependencies are introduced; this is compatible with Java 11 and Selenium 4.
• The integration test serves as a usage example, rather than being the focus; the central value is the new API.

💡 Additional Considerations
• Some drivers may not report browserVersion. In those cases, getBrowserVersion() will reliably return an empty string, avoiding errors or breaking changes.
• It would be beneficial to document, in Selenium’s capability documentation, that browserVersion is now available via this method.
• In the future, additional integration tests for other browsers (Firefox, Edge, etc.) could be added to increase confidence across platforms.

🔄 Types of changes
• ✅ New feature (non-breaking change which adds functionality and tests!)
• ✅ Enhancement / cleanup (documentation and method visibility improvements)


PR Type

Enhancement


Description

  • Add getBrowserVersion() method to RemoteWebDriver for programmatic browser version retrieval

  • Include unit test validating version extraction from capabilities

  • Add integration test demonstrating real browser session version display

  • Enhance test fixture with validSessionResponder for mock session creation


Diagram Walkthrough

flowchart LR
  A["RemoteWebDriver"] -- "adds method" --> B["getBrowserVersion()"]
  B -- "reads from" --> C["Capabilities"]
  D["Unit Test"] -- "validates" --> B
  E["Integration Test"] -- "demonstrates" --> B
  F["WebDriverFixture"] -- "supports" --> D
Loading

File Walkthrough

Relevant files
Enhancement
RemoteWebDriver.java
Add getBrowserVersion method to RemoteWebDriver                   

java/src/org/openqa/selenium/remote/RemoteWebDriver.java

  • Add getBrowserVersion() public method to extract browser version from
    capabilities
  • Return empty string if browserVersion capability is not available
  • Include Javadoc documenting Java 11 and Selenium 4 compatibility
+11/-0   
Tests
RemoteWebDriverUnitTest.java
Add unit test for getBrowserVersion method                             

java/test/org/openqa/selenium/remote/RemoteWebDriverUnitTest.java

  • Add canGetBrowserVersionFromCapabilities() unit test
  • Verify correct extraction of browser version from capabilities
  • Use mock capabilities with version "123.45.67" for testing
+18/-0   
ShowBrowserVersionIntegrationTest.java
Add integration test for browser version retrieval             

java/test/org/openqa/selenium/remote/ShowBrowserVersionIntegrationTest.java

  • Create new integration test class for browser version display
  • Launch real ChromeDriver session and retrieve browser version
  • Assert version is not null or empty and print to console
+24/-0   
WebDriverFixture.java
Add validSessionResponder to test fixture                               

java/test/org/openqa/selenium/remote/WebDriverFixture.java

  • Add validSessionResponder static function for mock session responses
  • Return mock session ID and browser version in response value
  • Support unit testing of browser version retrieval
+13/-0   

Introduce ShowBrowserVersionIntegrationTest to launch a real browser session
Retrieve browser version from capabilities and print it to the console
Assert that the browser version is not empty or null
Implements feature to programmatically obtain browser version
@selenium-ci selenium-ci added the C-java Java Bindings label Oct 13, 2025
Copy link
Contributor

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
Unsafe integration test

Description: The integration test instantiates a real ChromeDriver without guarding for environment
availability or sandboxing, which can trigger unintended local browser launches in CI;
consider marking it as integration-only or requiring explicit enablement.
ShowBrowserVersionIntegrationTest.java [12-21]

Referred Code
// Start a real Chrome session
WebDriver driver = new ChromeDriver();
try {
    Capabilities caps = ((ChromeDriver) driver).getCapabilities();
    String version = caps.getBrowserVersion();
    System.out.println("Browser version: " + version);
    assertThat(version).isNotEmpty();
    assertThat(version).isNotNull();
} finally {
    driver.quit();
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
No custom compliance provided

Follow the guide to enable custom compliance check.

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

Copy link
Contributor

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
High-level
Redundant method duplicates existing functionality

The new getBrowserVersion() method in RemoteWebDriver is redundant because the
Capabilities interface, accessible via driver.getCapabilities(), already
provides a getBrowserVersion() method.

Examples:

java/src/org/openqa/selenium/remote/RemoteWebDriver.java [877-880]
  public String getBrowserVersion() {
    Object version = getCapabilities().getCapability("browserVersion");
    return version != null ? version.toString() : "";
  }
java/test/org/openqa/selenium/remote/ShowBrowserVersionIntegrationTest.java [15-16]
            Capabilities caps = ((ChromeDriver) driver).getCapabilities();
            String version = caps.getBrowserVersion();

Solution Walkthrough:

Before:

// In RemoteWebDriver.java
public class RemoteWebDriver implements HasCapabilities {
  // ...
  @Override
  public Capabilities getCapabilities() {
    // ... returns capabilities
  }

  // No getBrowserVersion() method exists directly on RemoteWebDriver.
  // It must be called via getCapabilities():
  // driver.getCapabilities().getBrowserVersion()
}

After:

// In RemoteWebDriver.java
public class RemoteWebDriver implements HasCapabilities {
  // ...
  @Override
  public Capabilities getCapabilities() {
    // ...
  }

  /**
   * Returns the browser version from the capabilities.
   */
  public String getBrowserVersion() {
    Object version = getCapabilities().getCapability("browserVersion");
    return version != null ? version.toString() : "";
  }
}
Suggestion importance[1-10]: 10

__

Why: The suggestion correctly identifies that the new getBrowserVersion method is redundant, as equivalent functionality already exists via driver.getCapabilities().getBrowserVersion(), making the core change of this PR unnecessary.

High
General
Align mock response with W3C standard

Update the mock session response in validSessionResponder to be W3C compliant by
nesting the capabilities map under a capabilities key. Also, translate Spanish
comments to English for consistency.

java/test/org/openqa/selenium/remote/WebDriverFixture.java [193-204]

 public static final Function<Command, Response> validSessionResponder = cmd -> {
   if (DriverCommand.NEW_SESSION.equals(cmd.getName())) {
     Response response = new Response();
     response.setState("success");
     response.setSessionId("mock-session-id");
-    // Capabilities con browserVersion
-    response.setValue(Map.of("browserVersion", "123.45.67"));
+    // W3C-compliant session response with capabilities
+    response.setValue(Map.of("capabilities", Map.of("browserVersion", "123.45.67")));
     return response;
   }
-  // Para otros comandos, devolver null o como se requiera
+  // For other commands, return null or as required
   return null;
 };
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies that the mock session response does not follow the W3C WebDriver standard, and proposes a fix that aligns it with the correct structure, improving test accuracy.

Medium
  • More

@cgoldberg cgoldberg changed the title Add method to retrieve and display browser version [java] Add method to retrieve and display browser version Oct 13, 2025

/**
* Returns the browser version from the capabilities.
* Compatible with Java 11 and Selenium 4.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All methods in Selenium 4 are compatible with Selenium 4 (and Java 11).

@cgoldberg
Copy link
Member

You can already do: driver.getCapabilities().getBrowserVersion()

@diemol
Copy link
Member

diemol commented Oct 14, 2025

I agree with @cgoldberg, this is possible already. Why do we need a convenience method?

@manuelsblanco
Copy link
Contributor Author

I agree with @cgoldberg, this is possible already. Why do we need a convenience method?

my mistake I didn’t saw the original method, we can close this PR, Sorry

@manuelsblanco manuelsblanco deleted the getBrowser branch October 14, 2025 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants