Skip to content
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

[py] Fix driver class name in test fixtures #15550

Merged
merged 4 commits into from
Apr 2, 2025

Conversation

cgoldberg
Copy link
Contributor

@cgoldberg cgoldberg commented Apr 2, 2025

User description

Motivation and Context

This PR fixes some issues in the Python test configuration (PyTest fixtures).

Before this fix, running tests that use the clean_driver or clean_service fixture would fail when using certain --driver options.

If --driver option was not included, the tests would fail with:
TypeError: 'NoneType' object is not subscriptable
Now they correctly fail with:
Exception: This test requires a --driver to be specified

If --driver=webkitgtk or --driver=wpewebkit was used, tests would fail with:
AttributeError: module 'selenium.webdriver' has no attribute 'Webkitgtk'. Did you mean: 'webkitgtk'?
or
AttributeError: module 'selenium.webdriver' has no attribute 'Wpewebkit'. Did you mean: 'wpewebkit'?
Now the tests can be executed using these options.

This PR also fixes an issue where WebKitGTK and WPEWebKit tests were not skipped on non-Linux platforms.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Bug fix


Description

  • Fixed incorrect driver class name generation in test fixtures.

  • Added get_driver_class function for consistent driver class handling.

  • Improved error handling for missing or invalid --driver options.

  • Enhanced compatibility with webkitgtk and wpewebkit drivers.


Changes walkthrough 📝

Relevant files
Bug fix
conftest.py
Refactored driver class handling and improved error messages

py/conftest.py

  • Added get_driver_class function to generate driver class names.
  • Updated clean_service and clean_driver fixtures to use
    get_driver_class.
  • Improved error messages for missing or invalid --driver options.
  • Enhanced support for webkitgtk and wpewebkit driver options.
  • +17/-6   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    qodo-merge-pro bot commented Apr 2, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Missing Variable

    The driver_class variable is missing in the clean_service fixture. It's defined but not assigned before being used in the get_service call.

    yield get_service(driver_class, request.config.option.executable)

    Copy link
    Contributor

    qodo-merge-pro bot commented Apr 2, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Add input validation

    The function should handle the case when driver_option is None or not a string,
    which could happen if the driver option is not provided correctly. This would
    prevent the TypeError that's being caught in the fixtures.

    py/conftest.py [345-353]

     def get_driver_class(driver_option):
         """Generate the driver class name from the lowercase driver option"""
    +    if driver_option is None:
    +        raise TypeError("Driver option cannot be None")
    +    if not isinstance(driver_option, str):
    +        raise TypeError(f"Driver option must be a string, got {type(driver_option).__name__}")
    +    
         if driver_option == "webkitgtk":
             driver_class = "WebKitGTK"
         elif driver_option == "wpewebkit":
             driver_class = "WPEWebKit"
         else:
             driver_class = driver_option.capitalize()
         return driver_class
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion adds important input validation to prevent unexpected errors when invalid driver options are provided. This improves error handling by providing more specific error messages rather than letting the error propagate to the try/except blocks in the fixtures.

    Medium
    • Update

    @cgoldberg
    Copy link
    Contributor Author

    The driver_class variable is missing in the clean_service fixture. It's defined but not assigned before being used in the get_service call.

    I don't know what this AI is smoking, but driver_class is very clearly assigned before being used.

    Copy link

    @CloneOfAlex CloneOfAlex left a comment

    Choose a reason for hiding this comment

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

    nice

    @cgoldberg cgoldberg merged commit 3d13da1 into SeleniumHQ:trunk Apr 2, 2025
    3 of 4 checks passed
    @cgoldberg cgoldberg deleted the py-test-fixtures-class-name branch April 2, 2025 20:02
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants