Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6da9cd0
FedCM python implementation
navin772 Nov 1, 2024
729f2bd
set dialog.py methods to use webdriver methods
navin772 Nov 1, 2024
eca9892
fix implementation and unit tests
navin772 Nov 2, 2024
bd2b3f6
use webserver in tests
navin772 Nov 2, 2024
8ff195d
use a custom fedcm_webserver fixture to use the ExtendedHandler
navin772 Nov 2, 2024
10aa166
rename to `extended_webserver`
navin772 Nov 2, 2024
4df92a3
Merge branch 'SeleniumHQ:trunk' into py-fedcm
navin772 Nov 3, 2024
484b621
use `webserver`
navin772 Nov 4, 2024
1559f8f
Merge branch 'SeleniumHQ:trunk' into py-fedcm
navin772 Nov 4, 2024
c2f5115
move fedcm commands to a new `fedcm` namespace
navin772 Nov 6, 2024
f5d47fa
support both edge and chrome browser for fedcm
navin772 Nov 6, 2024
25d3431
fix tests as per new implementation
navin772 Nov 6, 2024
bfb5a42
remove old commented code
navin772 Nov 6, 2024
6ade5a8
Merge branch 'trunk' into py-fedcm
navin772 Nov 6, 2024
2a2772a
Merge branch 'trunk' into py-fedcm
navin772 Nov 6, 2024
9e52be8
remove mixin and implement in remote webdriver
navin772 Nov 6, 2024
b925513
fix python usage in docstrings
navin772 Nov 7, 2024
f86e3bf
Merge branch 'trunk' into py-fedcm
navin772 Nov 7, 2024
c2c4faf
move to `accept` and `dismiss` for dialog
navin772 Nov 7, 2024
229154e
Merge branch 'trunk' into py-fedcm
navin772 Nov 8, 2024
46302f3
update docs and remove duplicate methods
navin772 Nov 9, 2024
02f3ca9
Merge branch 'trunk' into py-fedcm
navin772 Nov 12, 2024
687813a
Merge branch 'trunk' into py-fedcm
navin772 Nov 14, 2024
a91eb5a
Merge branch 'trunk' into py-fedcm
VietND96 Nov 14, 2024
035f9d0
Merge branch 'trunk' into py-fedcm
VietND96 Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion py/selenium/webdriver/chrome/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

from selenium.webdriver.chromium.webdriver import ChromiumDriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.driver_extensions.has_fedcm_dialog import HasFedCmDialog

from .options import Options
from .service import Service


class WebDriver(ChromiumDriver):
class WebDriver(ChromiumDriver, HasFedCmDialog):
"""Controls the ChromeDriver and allows you to drive the browser."""

def __init__(
Expand Down
62 changes: 62 additions & 0 deletions py/selenium/webdriver/common/driver_extensions/has_fedcm_dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from selenium.webdriver.common.fedcm.dialog import Dialog
from selenium.webdriver.remote.webdriver import WebDriver


class HasFedCmDialog(WebDriver):
"""Mixin that provides FedCM-specific functionality."""

@property
def fedcm_dialog(self):
"""Returns the FedCM dialog object for interaction."""
return Dialog(self)

def enable_fedcm_delay(self, enable):
"""Disables the promise rejection delay for FedCm.
FedCm by default delays promise resolution in failure cases for
privacy reasons. This method allows turning it off to let tests
run faster where this is not relevant.
"""
self.set_fedcm_delay(enable)

def fedcm_cooldown(self):
"""Resets the FedCm dialog cooldown.
If a user agent triggers a cooldown when the account chooser is
dismissed, this method resets that cooldown so that the dialog
can be triggered again immediately.
"""
self.reset_fedcm_cooldown()

def wait_for_fedcm_dialog(self, timeout=5, poll_frequency=0.5, ignored_exceptions=None):
"""Waits for the FedCM dialog to appear."""
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.wait import WebDriverWait

if ignored_exceptions is None:
ignored_exceptions = (NoAlertPresentException,)

def check_fedcm():
try:
return self.fedcm_dialog if self.fedcm_dialog.type else None
except NoAlertPresentException:
return None

wait = WebDriverWait(self, timeout, poll_frequency=poll_frequency, ignored_exceptions=ignored_exceptions)
return wait.until(lambda _: check_fedcm())
71 changes: 71 additions & 0 deletions py/selenium/webdriver/common/fedcm/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from enum import Enum
from typing import Optional


class LoginState(Enum):
SIGN_IN = "SignIn"
SIGN_UP = "SignUp"


class Account:
"""Represents an account displayed in a FedCM account list.

See: https://w3c-fedid.github.io/FedCM/#dictdef-identityprovideraccount
https://w3c-fedid.github.io/FedCM/#webdriver-accountlist
"""

def __init__(self, account_data):
self._account_data = account_data

@property
def account_id(self) -> Optional[str]:
return self._account_data.get("accountId")

@property
def email(self) -> Optional[str]:
return self._account_data.get("email")

@property
def name(self) -> Optional[str]:
return self._account_data.get("name")

@property
def given_name(self) -> Optional[str]:
return self._account_data.get("givenName")

@property
def picture_url(self) -> Optional[str]:
return self._account_data.get("pictureUrl")

@property
def idp_config_url(self) -> Optional[str]:
return self._account_data.get("idpConfigUrl")

@property
def terms_of_service_url(self) -> Optional[str]:
return self._account_data.get("termsOfServiceUrl")

@property
def privacy_policy_url(self) -> Optional[str]:
return self._account_data.get("privacyPolicyUrl")

@property
def login_state(self) -> Optional[str]:
return self._account_data.get("loginState")
64 changes: 64 additions & 0 deletions py/selenium/webdriver/common/fedcm/dialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from typing import List
from typing import Optional

from .account import Account


class Dialog:
"""Represents a FedCM dialog that can be interacted with."""

DIALOG_TYPE_ACCOUNT_LIST = "AccountChooser"
DIALOG_TYPE_AUTO_REAUTH = "AutoReauthn"

def __init__(self, driver) -> None:
self._driver = driver

@property
def type(self) -> Optional[str]:
"""Gets the type of the dialog currently being shown."""
return self._driver.get_fedcm_dialog_type()

@property
def title(self) -> str:
"""Gets the title of the dialog."""
return self._driver.get_fedcm_title()

@property
def subtitle(self) -> Optional[str]:
"""Gets the subtitle of the dialog."""
result = self._driver.get_fedcm_subtitle()
return result.get("subtitle") if result else None

def get_accounts(self) -> List[Account]:
"""Gets the list of accounts shown in the dialog."""
accounts = self._driver.get_fedcm_account_list()
return [Account(account) for account in accounts]

def select_account(self, index: int) -> None:
"""Selects an account from the dialog by index."""
self._driver.select_fedcm_account(index)

def click_continue(self) -> None:
"""Clicks the continue button in the dialog."""
self._driver.click_fedcm_dialog_button()

def cancel(self) -> None:
"""Cancels/dismisses the dialog."""
self._driver.cancel_fedcm_dialog()
10 changes: 10 additions & 0 deletions py/selenium/webdriver/remote/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ class Command:
GET_DOWNLOADABLE_FILES: str = "getDownloadableFiles"
DOWNLOAD_FILE: str = "downloadFile"
DELETE_DOWNLOADABLE_FILES: str = "deleteDownloadableFiles"

# Federated Credential Management (FedCM)
GET_FEDCM_TITLE: str = "getFedcmTitle"
GET_FEDCM_DIALOG_TYPE: str = "getFedcmDialogType"
GET_FEDCM_ACCOUNT_LIST: str = "getFedcmAccountList"
SELECT_FEDCM_ACCOUNT: str = "selectFedcmAccount"
CLICK_FEDCM_DIALOG_BUTTON: str = "clickFedcmDialogButton"
CANCEL_FEDCM_DIALOG: str = "cancelFedcmDialog"
SET_FEDCM_DELAY: str = "setFedcmDelay"
RESET_FEDCM_COOLDOWN: str = "resetFedcmCooldown"
9 changes: 9 additions & 0 deletions py/selenium/webdriver/remote/remote_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@
Command.GET_DOWNLOADABLE_FILES: ("GET", "/session/$sessionId/se/files"),
Command.DOWNLOAD_FILE: ("POST", "/session/$sessionId/se/files"),
Command.DELETE_DOWNLOADABLE_FILES: ("DELETE", "/session/$sessionId/se/files"),
# Federated Credential Management (FedCM)
Command.GET_FEDCM_TITLE: ("GET", "/session/$sessionId/fedcm/gettitle"),
Command.GET_FEDCM_DIALOG_TYPE: ("GET", "/session/$sessionId/fedcm/getdialogtype"),
Command.GET_FEDCM_ACCOUNT_LIST: ("GET", "/session/$sessionId/fedcm/accountlist"),
Command.CLICK_FEDCM_DIALOG_BUTTON: ("POST", "/session/$sessionId/fedcm/clickdialogbutton"),
Command.CANCEL_FEDCM_DIALOG: ("POST", "/session/$sessionId/fedcm/canceldialog"),
Command.SELECT_FEDCM_ACCOUNT: ("POST", "/session/$sessionId/fedcm/selectaccount"),
Command.SET_FEDCM_DELAY: ("POST", "/session/$sessionId/fedcm/setdelayenabled"),
Command.RESET_FEDCM_COOLDOWN: ("POST", "/session/$sessionId/fedcm/resetcooldown"),
}


Expand Down
49 changes: 49 additions & 0 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,3 +1222,52 @@ def delete_downloadable_files(self) -> None:
raise WebDriverException("You must enable downloads in order to work with downloadable files.")

self.execute(Command.DELETE_DOWNLOADABLE_FILES)

# Federated Credential Management (FedCM)
def get_fedcm_title(self) -> str:
"""Gets the title of the dialog."""
return self.execute(Command.GET_FEDCM_TITLE)["value"].get("title")

def get_fedcm_subtitle(self) -> Optional[str]:
"""Gets the subtitle of the dialog."""
return self.execute(Command.GET_FEDCM_TITLE)["value"].get("subtitle")

def get_fedcm_dialog_type(self):
"""Gets the type of the dialog currently being shown."""
return self.execute(Command.GET_FEDCM_DIALOG_TYPE).get("value")

def get_fedcm_account_list(self):
"""Gets the list of accounts shown in the dialog."""
return self.execute(Command.GET_FEDCM_ACCOUNT_LIST).get("value")

def select_fedcm_account(self, index: int) -> None:
"""Selects an account from the dialog by index."""
self.execute(Command.SELECT_FEDCM_ACCOUNT, {"accountIndex": index})

def click_fedcm_dialog_button(self) -> None:
"""Clicks the continue button in the dialog."""
self.execute(Command.CLICK_FEDCM_DIALOG_BUTTON, {"dialogButton": "ConfirmIdpLoginContinue"})

def cancel_fedcm_dialog(self) -> None:
"""Cancels/dismisses the FedCM dialog."""
self.execute(Command.CANCEL_FEDCM_DIALOG)

def set_fedcm_delay(self, enabled: bool) -> None:
"""Disables the promise rejection delay for FedCM.

FedCM by default delays promise resolution in failure cases for privacy reasons.
This method allows turning it off to let tests run faster where this is not relevant.

Args:
enabled: True to enable the delay, False to disable it
"""
self.execute(Command.SET_FEDCM_DELAY, {"enabled": enabled})

def reset_fedcm_cooldown(self) -> None:
"""Resets the FedCM dialog cooldown.

If a user agent triggers a cooldown when the account chooser is
dismissed, this method resets that cooldown so that the dialog
can be triggered again immediately.
"""
self.execute(Command.RESET_FEDCM_COOLDOWN)
Loading
Loading