From 68b92607f4629e6406f6611b56a7e08fb9cfdba5 Mon Sep 17 00:00:00 2001 From: Oboleninov Anton Date: Fri, 8 Dec 2023 20:31:56 +0700 Subject: [PATCH] [py] Add page load strategy enum (#13258) Fixes #13236 --- py/selenium/webdriver/common/options.py | 19 ++++++++++++++++++- .../webdriver/marionette/mn_options_tests.py | 3 ++- .../webdriver/chrome/chrome_options_tests.py | 3 ++- .../webdriver/edge/edge_options_tests.py | 15 ++++++++------- .../firefox/firefox_options_tests.py | 15 ++++++++------- .../selenium/webdriver/ie/test_ie_options.py | 3 ++- .../webdriver/remote/new_session_tests.py | 5 +++-- .../webdriver/safari/safari_options_tests.py | 3 ++- .../webkitgtk/webkitgtk_options_tests.py | 3 ++- 9 files changed, 47 insertions(+), 22 deletions(-) diff --git a/py/selenium/webdriver/common/options.py b/py/selenium/webdriver/common/options.py index e9a56ecf3d44b..5952aa7376095 100644 --- a/py/selenium/webdriver/common/options.py +++ b/py/selenium/webdriver/common/options.py @@ -17,11 +17,28 @@ import typing from abc import ABCMeta from abc import abstractmethod +from enum import Enum from selenium.common.exceptions import InvalidArgumentException from selenium.webdriver.common.proxy import Proxy +class PageLoadStrategy(str, Enum): + """Enum of possible page load strategies. + + Selenium support following strategies: + * normal (default) - waits for all resources to download + * eager - DOM access is ready, but other resources like images may still be loading + * none - does not block `WebDriver` at all + + Docs: https://www.selenium.dev/documentation/webdriver/drivers/options/#pageloadstrategy. + """ + + normal = "normal" + eager = "eager" + none = "none" + + class _BaseOptionsDescriptor: def __init__(self, name): self.name = name @@ -348,7 +365,7 @@ def __init__(self) -> None: super().__init__() self._caps = self.default_capabilities self._proxy = None - self.set_capability("pageLoadStrategy", "normal") + self.set_capability("pageLoadStrategy", PageLoadStrategy.normal) self.mobile_options = None @property diff --git a/py/test/selenium/webdriver/marionette/mn_options_tests.py b/py/test/selenium/webdriver/marionette/mn_options_tests.py index 51e4e35765f97..1e9200836aa67 100644 --- a/py/test/selenium/webdriver/marionette/mn_options_tests.py +++ b/py/test/selenium/webdriver/marionette/mn_options_tests.py @@ -19,6 +19,7 @@ from selenium.webdriver.common.by import By from selenium.webdriver.common.desired_capabilities import DesiredCapabilities +from selenium.webdriver.common.options import PageLoadStrategy from selenium.webdriver.firefox.firefox_binary import FirefoxBinary from selenium.webdriver.firefox.firefox_profile import FirefoxProfile from selenium.webdriver.firefox.options import Log @@ -98,7 +99,7 @@ def test_arguments(self): def test_to_capabilities(self): opts = Options() firefox_caps = DesiredCapabilities.FIREFOX.copy() - firefox_caps.update({"pageLoadStrategy": "normal"}) + firefox_caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) assert opts.to_capabilities() == firefox_caps profile = FirefoxProfile() diff --git a/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py b/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py index d90a190e5854a..855eee77cb5d3 100644 --- a/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py +++ b/py/test/unit/selenium/webdriver/chrome/chrome_options_tests.py @@ -20,6 +20,7 @@ import pytest from selenium.webdriver.chrome.options import Options +from selenium.webdriver.common.options import PageLoadStrategy @pytest.fixture @@ -132,7 +133,7 @@ def test_starts_with_default_capabilities(options): from selenium.webdriver import DesiredCapabilities caps = DesiredCapabilities.CHROME.copy() - caps.update({"pageLoadStrategy": "normal"}) + caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) assert options._caps == caps diff --git a/py/test/unit/selenium/webdriver/edge/edge_options_tests.py b/py/test/unit/selenium/webdriver/edge/edge_options_tests.py index 3822ca66b9323..2de292f6def4f 100644 --- a/py/test/unit/selenium/webdriver/edge/edge_options_tests.py +++ b/py/test/unit/selenium/webdriver/edge/edge_options_tests.py @@ -17,6 +17,7 @@ import pytest +from selenium.webdriver.common.options import PageLoadStrategy from selenium.webdriver.edge.options import Options @@ -31,27 +32,27 @@ def test_raises_exception_with_invalid_page_load_strategy(options): def test_set_page_load_strategy(options): - options.page_load_strategy = "normal" + options.page_load_strategy = PageLoadStrategy.normal caps = options.to_capabilities() - assert caps["pageLoadStrategy"] == "normal" + assert caps["pageLoadStrategy"] == PageLoadStrategy.normal def test_get_page_load_strategy(options): - options._caps["pageLoadStrategy"] = "normal" - assert options.page_load_strategy == "normal" + options._caps["pageLoadStrategy"] = PageLoadStrategy.normal + assert options.page_load_strategy == PageLoadStrategy.normal def test_creates_capabilities(options): - options.page_load_strategy = "eager" + options.page_load_strategy = PageLoadStrategy.eager caps = options.to_capabilities() - assert caps["pageLoadStrategy"] == "eager" + assert caps["pageLoadStrategy"] == PageLoadStrategy.eager def test_starts_with_default_capabilities(options): from selenium.webdriver import DesiredCapabilities caps = DesiredCapabilities.EDGE.copy() - caps.update({"pageLoadStrategy": "normal"}) + caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) assert options._caps == caps diff --git a/py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py b/py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py index c1025346c54e8..9db5f7166fb5c 100644 --- a/py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py +++ b/py/test/unit/selenium/webdriver/firefox/firefox_options_tests.py @@ -18,6 +18,7 @@ import pytest from selenium.common.exceptions import InvalidArgumentException +from selenium.webdriver.common.options import PageLoadStrategy from selenium.webdriver.common.proxy import Proxy from selenium.webdriver.common.proxy import ProxyType from selenium.webdriver.firefox.firefox_binary import FirefoxBinary @@ -150,7 +151,7 @@ def test_starts_with_default_capabilities(options): from selenium.webdriver import DesiredCapabilities caps = DesiredCapabilities.FIREFOX.copy() - caps.update({"pageLoadStrategy": "normal"}) + caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) assert options._caps == caps @@ -166,19 +167,19 @@ def test_raises_exception_with_invalid_page_load_strategy(options): def test_set_page_load_strategy(options): - options.page_load_strategy = "normal" - assert options._caps["pageLoadStrategy"] == "normal" + options.page_load_strategy = PageLoadStrategy.normal + assert options._caps["pageLoadStrategy"] == PageLoadStrategy.normal def test_get_page_load_strategy(options): - options._page_load_strategy = "normal" - assert options._caps["pageLoadStrategy"] == "normal" + options._page_load_strategy = PageLoadStrategy.normal + assert options._caps["pageLoadStrategy"] == PageLoadStrategy.normal def test_creates_capabilities_with_page_load_strategy(options): - options.page_load_strategy = "eager" + options.page_load_strategy = PageLoadStrategy.eager caps = options.to_capabilities() - assert caps["pageLoadStrategy"] == "eager" + assert caps["pageLoadStrategy"] == PageLoadStrategy.eager def test_enables_firefox_mobile(options): diff --git a/py/test/unit/selenium/webdriver/ie/test_ie_options.py b/py/test/unit/selenium/webdriver/ie/test_ie_options.py index 71332a9b584af..8f8f5008ababb 100644 --- a/py/test/unit/selenium/webdriver/ie/test_ie_options.py +++ b/py/test/unit/selenium/webdriver/ie/test_ie_options.py @@ -18,6 +18,7 @@ import pytest +from selenium.webdriver.common.options import PageLoadStrategy from selenium.webdriver.ie.options import ElementScrollBehavior from selenium.webdriver.ie.options import Options @@ -193,7 +194,7 @@ def test_starts_with_default_capabilities(opts): from selenium.webdriver import DesiredCapabilities caps = DesiredCapabilities.INTERNETEXPLORER.copy() - caps.update({"pageLoadStrategy": "normal"}) + caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) assert opts._caps == caps diff --git a/py/test/unit/selenium/webdriver/remote/new_session_tests.py b/py/test/unit/selenium/webdriver/remote/new_session_tests.py index 98e9ad4cad3cd..95195f6e89a3f 100644 --- a/py/test/unit/selenium/webdriver/remote/new_session_tests.py +++ b/py/test/unit/selenium/webdriver/remote/new_session_tests.py @@ -22,6 +22,7 @@ from selenium.webdriver.chrome.options import Options as ChromeOptions from selenium.webdriver.common.options import ArgOptions +from selenium.webdriver.common.options import PageLoadStrategy from selenium.webdriver.common.proxy import Proxy from selenium.webdriver.common.proxy import ProxyType from selenium.webdriver.remote import webdriver @@ -77,7 +78,7 @@ def test_always_match_if_2_of_the_same_options(): "capabilities": { "alwaysMatch": { "browserName": "chrome", - "pageLoadStrategy": "normal", + "pageLoadStrategy": PageLoadStrategy.normal, }, "firstMatch": [ {"goog:chromeOptions": {"args": ["foo"], "extensions": []}}, @@ -95,7 +96,7 @@ def test_first_match_when_2_different_option_types(): expected = { "capabilities": { - "alwaysMatch": {"pageLoadStrategy": "normal"}, + "alwaysMatch": {"pageLoadStrategy": PageLoadStrategy.normal}, "firstMatch": [ {"browserName": "chrome", "goog:chromeOptions": {"extensions": [], "args": []}}, { diff --git a/py/test/unit/selenium/webdriver/safari/safari_options_tests.py b/py/test/unit/selenium/webdriver/safari/safari_options_tests.py index 88e9132eec487..8823dca6ece3d 100644 --- a/py/test/unit/selenium/webdriver/safari/safari_options_tests.py +++ b/py/test/unit/selenium/webdriver/safari/safari_options_tests.py @@ -17,6 +17,7 @@ import pytest +from selenium.webdriver.common.options import PageLoadStrategy from selenium.webdriver.safari.options import Options @@ -29,7 +30,7 @@ def test_starts_with_default_capabilities(options): from selenium.webdriver import DesiredCapabilities caps = DesiredCapabilities.SAFARI.copy() - caps.update({"pageLoadStrategy": "normal"}) + caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) assert options._caps == caps diff --git a/py/test/unit/selenium/webdriver/webkitgtk/webkitgtk_options_tests.py b/py/test/unit/selenium/webdriver/webkitgtk/webkitgtk_options_tests.py index 9638c5d4f34e3..b6921e37465cf 100644 --- a/py/test/unit/selenium/webdriver/webkitgtk/webkitgtk_options_tests.py +++ b/py/test/unit/selenium/webdriver/webkitgtk/webkitgtk_options_tests.py @@ -17,6 +17,7 @@ import pytest +from selenium.webdriver.common.options import PageLoadStrategy from selenium.webdriver.webkitgtk.options import Options @@ -61,7 +62,7 @@ def test_starts_with_default_capabilities(options): from selenium.webdriver import DesiredCapabilities caps = DesiredCapabilities.WEBKITGTK.copy() - caps.update({"pageLoadStrategy": "normal"}) + caps.update({"pageLoadStrategy": PageLoadStrategy.normal}) assert options._caps == caps