Skip to content

[rust] Electron support in Selenium-Manager (#13954) #15752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

bonigarcia
Copy link
Member

@bonigarcia bonigarcia commented May 19, 2025

User description

🔗 Related Issues

This PR implements the Rust side of #13954.

💥 What does this PR do?

This PR makes Selenium Manager to support Electron. As requested in #13954, Selenium Manager downloads the driver for Electron (i.e., chromedriver) from https://github.com/electron/electron/releases. If no driver version is specified, Selenium Manager downloads the latest.

🔧 Implementation Notes

Output examples:

> ./selenium-manager --browser electron --debug
[2025-05-19T14:09:14.206Z DEBUG] Required driver: chromedriver 36.2.1
[2025-05-19T14:09:14.208Z DEBUG] Acquiring lock: C:\Users\boni\.cache\selenium\chromedriver\win32-x64\36.2.1\sm.lock
[2025-05-19T14:09:14.208Z DEBUG] Downloading chromedriver 36.2.1 from https://github.com/electron/electron/releases/download/v36.2.1/chromedriver-v36.2.1-win32-x64.zip
[2025-05-19T14:09:17.382Z INFO ] Driver path: C:\Users\boni\.cache\selenium\chromedriver\win32-x64\36.2.1\chromedriver.exe
> ./selenium-manager --browser electron --debug --driver-version 36.2.0
[2025-05-19T14:09:59.986Z DEBUG] Acquiring lock: C:\Users\boni\.cache\selenium\chromedriver\win32-x64\36.2.0\sm.lock
[2025-05-19T14:09:59.986Z DEBUG] Downloading chromedriver 36.2.0 from https://github.com/electron/electron/releases/download/v36.2.0/chromedriver-v36.2.0-win32-x64.zip
[2025-05-19T14:10:03.125Z INFO ] Driver path: C:\Users\boni\.cache\selenium\chromedriver\win32-x64\36.2.0\chromedriver.exe

💡 Additional Considerations

The bindings will use this feature.

🔄 Types of changes

  • New feature (non-breaking change which adds functionality and tests!)

PR Type

Enhancement, Tests


Description

  • Add Electron browser support to Selenium Manager (Rust)

    • Implement ElectronManager for driver download and management
    • Integrate Electron into browser selection logic
  • Update CLI and documentation to mention Electron support

  • Add tests for Electron driver download and version selection


Changes walkthrough 📝

Relevant files
Enhancement
electron.rs
Add ElectronManager for Electron driver support                   

rust/src/electron.rs

  • Introduces new ElectronManager struct for handling Electron driver
    logic
  • Implements SeleniumManager trait for Electron
  • Handles driver version resolution and platform-specific download URLs
  • Integrates with caching and metadata systems
  • +275/-0 
    lib.rs
    Integrate ElectronManager into SeleniumManager logic         

    rust/src/lib.rs

  • Registers ElectronManager in browser selection logic
  • Adds is_electron() helper and integrates into download/setup flows
  • Exports ELECTRON_NAME constant and LATEST_RELEASE
  • Skips browser download and PATH checks for Electron
  • +12/-2   
    Documentation
    main.rs
    Document Electron as supported browser in CLI                       

    rust/src/main.rs

    • Updates CLI help text to include Electron as a supported browser
    +1/-1     
    Tests
    electron_tests.rs
    Add tests for Electron driver support                                       

    rust/tests/electron_tests.rs

  • Adds new tests for Electron driver download and version selection
  • Uses rstest for parameterized version test
  • +39/-0   
    Miscellaneous
    firefox.rs
    Use shared LATEST_RELEASE constant for Firefox                     

    rust/src/firefox.rs

  • Removes local LATEST_RELEASE constant in favor of shared one
  • Minor import adjustment for LATEST_RELEASE
  • +2/-2     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @bonigarcia bonigarcia added the C-rust Rust code is mostly Selenium Manager label May 19, 2025
    @bonigarcia bonigarcia moved this to In Progress in Selenium Manager May 19, 2025
    @selenium-ci selenium-ci added the B-manager Selenium Manager label May 19, 2025
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Platform Detection

    The platform label logic for macOS uses "mas-arm64" and "mas-x64", but these might not match Electron's actual release naming conventions. Verify these labels against Electron's GitHub releases.

    fn get_platform_label(&self) -> &str {
        let os = self.get_os();
        let arch = self.get_arch();
        if WINDOWS.is(os) {
            if X32.is(arch) {
                "win32-ia32"
            } else if ARM64.is(arch) {
                "win32-arm64-x64"
            } else {
                "win32-x64"
            }
        } else if MACOS.is(os) {
            if ARM64.is(arch) {
                "mas-arm64"
            } else {
                "mas-x64"
            }
        } else if ARM64.is(arch) {
            "linux-arm64"
        } else {
            "linux-x64"
        }
    Error Handling

    The request_driver_version method doesn't handle the case where the redirect from latest_url fails or returns an invalid version string. Consider adding more robust error handling.

    fn request_driver_version(&mut self) -> Result<String, Error> {
        let major_browser_version_binding = self.get_major_browser_version();
        let major_browser_version = major_browser_version_binding.as_str();
        let cache_path = self.get_cache_path()?;
        let mut metadata = get_metadata(self.get_logger(), &cache_path);
    
        match get_driver_version_from_metadata(
            &metadata.drivers,
            self.driver_name,
            major_browser_version,
        ) {
            Some(driver_version) => {
                self.log.trace(format!(
                    "Driver TTL is valid. Getting {} version from metadata",
                    &self.driver_name
                ));
                Ok(driver_version)
            }
            _ => {
                self.assert_online_or_err(OFFLINE_REQUEST_ERR_MSG)?;
    
                let latest_url = format!(
                    "{}{}",
                    self.get_driver_mirror_url_or_default(DRIVER_URL),
                    LATEST_RELEASE
                );
                let driver_version =
                    read_redirect_from_link(self.get_http_client(), latest_url, self.get_logger())?;
                let driver_ttl = self.get_ttl();
                if driver_ttl > 0 {
                    metadata.drivers.push(create_driver_metadata(
                        major_browser_version,
                        self.driver_name,
                        &driver_version,
                        driver_ttl,
                    ));
                    write_metadata(&metadata, self.get_logger(), cache_path);
                }
                Ok(driver_version)
            }
        }
    }

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix platform label format

    The platform labels for Electron drivers don't match the expected format. For
    macOS, Electron uses "darwin" in platform labels, not "mas". This will cause
    driver downloads to fail on macOS systems.

    rust/src/electron.rs [204-225]

     if WINDOWS.is(os) {
         if X32.is(arch) {
             "win32-ia32"
         } else if ARM64.is(arch) {
             "win32-arm64-x64"
         } else {
             "win32-x64"
         }
     } else if MACOS.is(os) {
         if ARM64.is(arch) {
    -        "mas-arm64"
    +        "darwin-arm64"
         } else {
    -        "mas-x64"
    +        "darwin-x64"
         }
     } else if ARM64.is(arch) {
         "linux-arm64"
     } else {
         "linux-x64"
     }

    [To ensure code accuracy, apply this suggestion manually]

    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly identifies that Electron uses "darwin" (not "mas") in platform labels for macOS, which is crucial for successful driver downloads on macOS systems. This is a significant bug fix that directly impacts functionality.

    Medium
    Fix download URL format

    The URL format for Electron's chromedriver is incorrect. Electron releases don't
    include a separate chromedriver download with this naming pattern. Instead, the
    chromedriver is bundled within the Electron release.

    rust/src/electron.rs [150-163]

     fn get_driver_url(&mut self) -> Result<String, Error> {
         if self.driver_url.is_some() {
             return Ok(self.driver_url.as_ref().unwrap().to_string());
         }
     
         Ok(format!(
    -        "{}download/v{}/{}-v{}-{}.zip",
    +        "{}download/v{}/chromedriver-v{}-{}.zip",
             self.get_driver_mirror_url_or_default(DRIVER_URL),
             self.get_driver_version(),
    -        CHROMEDRIVER_NAME,
             self.get_driver_version(),
             self.get_platform_label()
         ))
     }
    • Apply / Chat
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly notes that the Electron chromedriver is not distributed with the same naming pattern as upstream chromedriver releases, and proposes a more accurate URL format. This improves compatibility, though the impact is slightly less critical than the platform label fix.

    Medium
    • More

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    B-manager Selenium Manager C-rust Rust code is mostly Selenium Manager Review effort 3/5
    Projects
    Status: In Progress
    Development

    Successfully merging this pull request may close these issues.

    2 participants