From 08dd4cf456e4ef1c3ab4d0063bfaef32b74a0c34 Mon Sep 17 00:00:00 2001
From: Oboleninov Anton <oboleninoff.anton@yandex.ru>
Date: Fri, 8 Dec 2023 20:31:56 +0700
Subject: [PATCH] [py] Add page load strategy enum (#13258)

Fixes #13236

NOKEYCHECK=True
GitOrigin-RevId: 68b92607f4629e6406f6611b56a7e08fb9cfdba5
---
 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/selenium/webdriver/common/options.py b/selenium/webdriver/common/options.py
index e9a56ecf..5952aa73 100644
--- a/selenium/webdriver/common/options.py
+++ b/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 @@ class BaseOptions(metaclass=ABCMeta):
         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/test/selenium/webdriver/marionette/mn_options_tests.py b/test/selenium/webdriver/marionette/mn_options_tests.py
index 51e4e357..1e920083 100644
--- a/test/selenium/webdriver/marionette/mn_options_tests.py
+++ b/test/selenium/webdriver/marionette/mn_options_tests.py
@@ -19,6 +19,7 @@ import pytest
 
 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 @@ class TestUnit:
     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/test/unit/selenium/webdriver/chrome/chrome_options_tests.py b/test/unit/selenium/webdriver/chrome/chrome_options_tests.py
index d90a190e..855eee77 100644
--- a/test/unit/selenium/webdriver/chrome/chrome_options_tests.py
+++ b/test/unit/selenium/webdriver/chrome/chrome_options_tests.py
@@ -20,6 +20,7 @@ from os import path
 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/test/unit/selenium/webdriver/edge/edge_options_tests.py b/test/unit/selenium/webdriver/edge/edge_options_tests.py
index 3822ca66..2de292f6 100644
--- a/test/unit/selenium/webdriver/edge/edge_options_tests.py
+++ b/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/test/unit/selenium/webdriver/firefox/firefox_options_tests.py b/test/unit/selenium/webdriver/firefox/firefox_options_tests.py
index c1025346..9db5f716 100644
--- a/test/unit/selenium/webdriver/firefox/firefox_options_tests.py
+++ b/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/test/unit/selenium/webdriver/ie/test_ie_options.py b/test/unit/selenium/webdriver/ie/test_ie_options.py
index 71332a9b..8f8f5008 100644
--- a/test/unit/selenium/webdriver/ie/test_ie_options.py
+++ b/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/test/unit/selenium/webdriver/remote/new_session_tests.py b/test/unit/selenium/webdriver/remote/new_session_tests.py
index 98e9ad4c..95195f6e 100644
--- a/test/unit/selenium/webdriver/remote/new_session_tests.py
+++ b/test/unit/selenium/webdriver/remote/new_session_tests.py
@@ -22,6 +22,7 @@ import pytest
 
 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/test/unit/selenium/webdriver/safari/safari_options_tests.py b/test/unit/selenium/webdriver/safari/safari_options_tests.py
index 88e9132e..8823dca6 100644
--- a/test/unit/selenium/webdriver/safari/safari_options_tests.py
+++ b/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/test/unit/selenium/webdriver/webkitgtk/webkitgtk_options_tests.py b/test/unit/selenium/webdriver/webkitgtk/webkitgtk_options_tests.py
index 9638c5d4..b6921e37 100644
--- a/test/unit/selenium/webdriver/webkitgtk/webkitgtk_options_tests.py
+++ b/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