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

[rb] Add FedCM support to the ruby selenium client #13796

Merged
merged 89 commits into from
Jun 24, 2024

Conversation

aguspe
Copy link
Contributor

@aguspe aguspe commented Apr 10, 2024

User description

Description

As mentioned in #12088

The goal is to add support for the Federal credential management API (FedCM) so users have access to commands that will enable them to automate it

Reference Docs:

https://fedidcg.github.io/FedCM/#automation
https://developer.mozilla.org/en-US/docs/Web/API/FedCM_API

Motivation and Context

On Chrome 108 FedCM has shipped and it's already implemented by several web solutions, by adding support to the client library we guarantee that more users can automate this functionality without work arounds

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

Enhancement, Tests


Description

  • Added support for Federal Credential Management (FedCM) API to the Ruby Selenium client.
  • Implemented HasFedCmDialog extension for handling FedCM dialogs.
  • Added Account and Dialog classes to represent and handle FedCM dialogs.
  • Updated bridge to include methods for interacting with FedCM dialogs.
  • Added integration and unit tests for FedCM dialog interactions.
  • Updated FedCM configuration and type signatures.

Changes walkthrough 📝

Relevant files
Enhancement
13 files
driver.rb
Add FedCM dialog extension to Chromium driver                       

rb/lib/selenium/webdriver/chromium/driver.rb

  • Added DriverExtensions::HasFedCmDialog to the list of extensions.
  • +1/-0     
    common.rb
    Require FedCM dialog and account in common driver               

    rb/lib/selenium/webdriver/common.rb

  • Required has_fedcm_dialog extension.
  • Required fedcm/account and fedcm/dialog.
  • +3/-0     
    has_fedcm_dialog.rb
    Implement FedCM dialog handling extension                               

    rb/lib/selenium/webdriver/common/driver_extensions/has_fedcm_dialog.rb

  • Added HasFedCmDialog module with methods to handle FedCM dialogs.
  • +55/-0   
    fedcm.rb
    Autoload FedCM account and dialog modules                               

    rb/lib/selenium/webdriver/common/fedcm.rb

    • Added autoload for Account and Dialog under FedCM module.
    +27/-0   
    account.rb
    Add FedCM account representation class                                     

    rb/lib/selenium/webdriver/common/fedcm/account.rb

    • Added Account class to represent FedCM accounts.
    +50/-0   
    dialog.rb
    Add FedCM dialog handling class                                                   

    rb/lib/selenium/webdriver/common/fedcm/dialog.rb

    • Added Dialog class to handle FedCM dialogs.
    +74/-0   
    bridge.rb
    Add FedCM dialog interaction methods to bridge                     

    rb/lib/selenium/webdriver/remote/bridge.rb

    • Added methods to handle FedCM dialog interactions.
    +40/-0   
    commands.rb
    Add FedCM dialog commands to bridge                                           

    rb/lib/selenium/webdriver/remote/bridge/commands.rb

    • Added FedCM dialog related commands.
    +13/-1   
    fedcm.html
    Update FedCM config URL to use HTTP                                           

    common/src/web/fedcm/fedcm.html

    • Updated configURL to use http instead of https.
    +1/-1     
    fedcm.json
    Add login URL to FedCM configuration                                         

    common/src/web/fedcm/fedcm.json

    • Added login_url to FedCM configuration.
    +2/-1     
    account.rbs
    Add type signatures for FedCM account class                           

    rb/sig/lib/selenium/webdriver/fedcm/account.rbs

    • Added type signatures for Account class.
    +52/-0   
    dialog.rbs
    Add type signatures for FedCM dialog class                             

    rb/sig/lib/selenium/webdriver/fedcm/dialog.rbs

    • Added type signatures for Dialog class.
    +26/-0   
    bridge.rbs
    Add type signatures for FedCM methods in bridge                   

    rb/sig/lib/selenium/webdriver/remote/bridge.rbs

    • Added type signatures for FedCM dialog methods in bridge.
    +18/-0   
    Tests
    3 files
    fedcm_spec.rb
    Add integration tests for FedCM dialog                                     

    rb/spec/integration/selenium/webdriver/fedcm_spec.rb

    • Added integration tests for FedCM dialog interactions.
    +90/-0   
    account_spec.rb
    Add unit tests for FedCM account class                                     

    rb/spec/unit/selenium/webdriver/common/fedcm/account_spec.rb

    • Added unit tests for Account class.
    +54/-0   
    dialog_spec.rb
    Add unit tests for FedCM dialog class                                       

    rb/spec/unit/selenium/webdriver/common/fedcm/dialog_spec.rb

    • Added unit tests for Dialog class.
    +93/-0   

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @aguspe aguspe marked this pull request as ready for review June 9, 2024 14:04
    @p0deje
    Copy link
    Member

    p0deje commented Jun 20, 2024

    I noticed that edge still tried to execute the fedcm integration tests even though is only for chrome so I adjusted that and I moved the integration tests to the chrome folder

    Actually, I think what you were seeing is that your implementation actually works in Edge, while you marked it to work only in Chrome. You should move the tests back from Chrome folder and mark it as only: {browser: %w[chrome edge]}. The error in CI was suggesting that:

    expected pending 'Test guarded; Guarded by {:browser=>:chrome, :reason=>"No reason given"};' to fail. No error was raised.
    

    @aguspe
    Copy link
    Contributor Author

    aguspe commented Jun 21, 2024

    {browser: %w[chrome edge]}

    I noticed that edge still tried to execute the fedcm integration tests even though is only for chrome so I adjusted that and I moved the integration tests to the chrome folder

    Actually, I think what you were seeing is that your implementation actually works in Edge, while you marked it to work only in Chrome. You should move the tests back from Chrome folder and mark it as only: {browser: %w[chrome edge]}. The error in CI was suggesting that:

    expected pending 'Test guarded; Guarded by {:browser=>:chrome, :reason=>"No reason given"};' to fail. No error was raised.
    

    Thank you so much for the explanation, what throw me off before is that I tried this, but now that I looked more into the Guard implementation, this works and the test pass both with edge and chrome:

    describe FedCM, exclusive: [{bidi: false, reason: 'Not yet implemented with BiDi'}, {browser: %i[chrome edge]}]

    @aguspe
    Copy link
    Contributor Author

    aguspe commented Jun 23, 2024

    Is there anything I can do to help this get merged? All the ruby tests seemed to have passed in the last run, @p0deje @titusfortner

    @p0deje p0deje closed this Jun 24, 2024
    @p0deje p0deje reopened this Jun 24, 2024
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review [1-5] 3
    🧪 Relevant tests Yes
    🔒 Security concerns No
    ⚡ Key issues to review Possible Bug:
    The implementation of the FedCM API should ensure that it handles errors gracefully, especially in scenarios where the dialog is not present or the user interaction is cancelled. This should include proper exception handling and user feedback mechanisms.
    Performance Concern:
    The methods related to FedCM dialog interactions (like select_account, cancel, etc.) should ensure they do not introduce significant delays or performance issues, especially in web environments where responsiveness is crucial.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Initialize constants with appropriate values to avoid potential issues

    Consider initializing the constants LOGIN_STATE_SIGNIN and LOGIN_STATE_SIGNUP with
    appropriate values to avoid potential issues when these constants are used.

    rb/sig/lib/selenium/webdriver/fedcm/account.rbs [26-28]

    -LOGIN_STATE_SIGNIN: String
    -LOGIN_STATE_SIGNUP: String
    +LOGIN_STATE_SIGNIN: String = "signin"
    +LOGIN_STATE_SIGNUP: String = "signup"
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Initializing constants with explicit values can prevent runtime errors and ensure consistent behavior, which is crucial for constant usage.

    8
    Ensure the bridge method is called in the #click test

    In the #click test, the expectation should be on the method being called on the bridge,
    not on the return value of dialog.click. This ensures that the method call is verified
    correctly.

    rb/spec/unit/selenium/webdriver/common/fedcm/dialog_spec.rb [32-33]

    -expect(dialog.click).to be_nil
    +expect(bridge).to receive(:click_fedcm_dialog_button)
    +dialog.click
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion correctly identifies a better testing practice by verifying the method call on the bridge instead of the return value, which enhances the test's accuracy.

    7
    Ensure the bridge method is called in the #cancel test

    In the #cancel test, the expectation should be on the method being called on the bridge,
    not on the return value of dialog.cancel. This ensures that the method call is verified
    correctly.

    rb/spec/unit/selenium/webdriver/common/fedcm/dialog_spec.rb [39-40]

    -expect(dialog.cancel).to be_nil
    +expect(bridge).to receive(:cancel_fedcm_dialog)
    +dialog.cancel
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Similar to the first suggestion, this improves the test by checking the actual method call on the bridge, ensuring that the functionality is tested rather than just the outcome.

    7
    Possible bug
    Correct the method call in fedcm_subtitle to ensure the correct command is executed

    The fedcm_subtitle method should call get_fedcm_subtitle instead of get_fedcm_title to
    avoid confusion and ensure the correct command is executed.

    rb/lib/selenium/webdriver/remote/bridge.rb [625]

    -execute(:get_fedcm_title).fetch('subtitle', nil)
    +execute(:get_fedcm_subtitle).fetch('subtitle', nil)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This is a crucial fix as it corrects the method call to fetch the subtitle, which is likely a bug in the current implementation. Correcting this ensures the right API endpoint is used.

    8
    Enhancement
    Add a method to handle potential errors when interacting with the FedCM dialog

    Consider adding a method to handle potential errors when interacting with the FedCM
    dialog, which can improve the robustness of the code.

    rb/sig/lib/selenium/webdriver/fedcm/dialog.rbs [16-17]

     def select_account: (Integer index) -> Remote::Response?
     
    +def handle_error: (String error_message) -> void
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding error handling methods can significantly improve the robustness and reliability of the system by managing exceptions effectively.

    7
    Best practice
    Remove or replace the skip option in the #clicks the dialog test to ensure proper test execution

    The skip option in the #clicks the dialog test should be removed or replaced with a proper
    condition to ensure the test is executed or skipped based on a meaningful condition.

    rb/spec/integration/selenium/webdriver/fedcm_spec.rb [70]

    -it 'clicks the dialog', skip: 'Investigate IDP config issue' do
    +it 'clicks the dialog' do
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: This suggestion promotes best practices by encouraging the removal or proper conditional handling of the skip option in tests, which can lead to more robust testing scenarios.

    6
    Maintainability
    Group related methods together for better code organization and readability

    To maintain consistency, consider grouping all fedcm related methods together in the same
    section of the class definition.

    rb/sig/lib/selenium/webdriver/remote/bridge.rbs [35-66]

    +def fedcm_account_list: -> [FedCM::Account]
    +
    +def fedcm_dialog_type: -> String
    +
    +def fedcm_subtitle: -> String?
    +
    +def fedcm_title: -> String
    +
    +def reset_fedcm_cooldown: -> nil
    +
    +def select_fedcm_account: -> nil
    +
     def cancel_fedcm_dialog: -> nil
     
     def click_fedcm_dialog_button: -> nil
     
    +def fedcm_delay: -> bool
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Grouping related methods enhances code readability and maintainability, although it's not a critical issue.

    6

    @p0deje p0deje merged commit 826a6bd into SeleniumHQ:trunk Jun 24, 2024
    33 of 37 checks passed
    @p0deje
    Copy link
    Member

    p0deje commented Jun 24, 2024

    Thank you for the contribution!

    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