Skip to content

Commit 9f5801c

Browse files
titusfortnerdiemol
andauthored
[py] Remove deprecated code in driver classes (#12030)
* [py] remove chrome w3c warnings * [py] this constant is always true for supported versions of Python * [py] remove deprecations * [py] Fixing linting issues --------- Co-authored-by: Diego Molina <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent abad15e commit 9f5801c

File tree

17 files changed

+124
-705
lines changed

17 files changed

+124
-705
lines changed

py/conftest.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import pytest
2828

2929
from selenium import webdriver
30-
from selenium.webdriver import DesiredCapabilities
3130

3231
drivers = (
3332
"chrome",
@@ -120,9 +119,7 @@ def fin():
120119
if driver_class == "Chrome":
121120
options = get_options(driver_class, request.config)
122121
if driver_class == "Remote":
123-
capabilities = DesiredCapabilities.FIREFOX.copy()
124-
kwargs.update({"desired_capabilities": capabilities})
125-
options = get_options("Firefox", request.config)
122+
options = get_options("Firefox", request.config) or webdriver.FirefoxOptions()
126123
if driver_class == "WebKitGTK":
127124
options = get_options(driver_class, request.config)
128125
if driver_class == "Edge":

py/selenium/webdriver/chrome/webdriver.py

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,84 +14,42 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
import warnings
1817

1918
from selenium.webdriver.chromium.webdriver import ChromiumDriver
2019
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2120
from selenium.webdriver.common.driver_finder import DriverFinder
2221

2322
from .options import Options
24-
from .service import DEFAULT_EXECUTABLE_PATH
2523
from .service import Service
2624

27-
DEFAULT_PORT = 0
28-
DEFAULT_SERVICE_LOG_PATH = None
29-
DEFAULT_KEEP_ALIVE = None
30-
3125

3226
class WebDriver(ChromiumDriver):
33-
"""Controls the ChromeDriver and allows you to drive the browser.
34-
35-
You will need to download the ChromeDriver executable from
36-
http://chromedriver.storage.googleapis.com/index.html
37-
"""
27+
"""Controls the ChromeDriver and allows you to drive the browser."""
3828

3929
def __init__(
4030
self,
41-
executable_path=DEFAULT_EXECUTABLE_PATH,
42-
port=DEFAULT_PORT,
4331
options: Options = None,
44-
service_args=None,
45-
desired_capabilities=None,
46-
service_log_path=DEFAULT_SERVICE_LOG_PATH,
47-
chrome_options=None,
4832
service: Service = None,
49-
keep_alive=DEFAULT_KEEP_ALIVE,
33+
keep_alive: bool = True,
5034
) -> None:
5135
"""Creates a new instance of the chrome driver. Starts the service and
5236
then creates new instance of chrome driver.
5337
5438
:Args:
55-
- executable_path - Deprecated: path to the executable. If the default is used it assumes the executable is in the $PATH
56-
- port - Deprecated: port you would like the service to run, if left as 0, a free port will be found.
5739
- options - this takes an instance of ChromeOptions
5840
- service - Service object for handling the browser driver if you need to pass extra details
59-
- service_args - Deprecated: List of args to pass to the driver service
60-
- desired_capabilities - Deprecated: Dictionary object with non-browser specific
61-
capabilities only, such as "proxy" or "loggingPref".
62-
- service_log_path - Deprecated: Where to log information from the driver.
63-
- keep_alive - Deprecated: Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
41+
- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
6442
"""
65-
if executable_path != "chromedriver":
66-
warnings.warn(
67-
"executable_path has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
68-
)
69-
if chrome_options:
70-
warnings.warn("use options instead of chrome_options", DeprecationWarning, stacklevel=2)
71-
options = chrome_options
72-
if keep_alive != DEFAULT_KEEP_ALIVE:
73-
warnings.warn(
74-
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
75-
)
76-
else:
77-
keep_alive = True
78-
if not options:
79-
options = self.create_options()
80-
if not service:
81-
service = Service(executable_path, port, service_args, service_log_path)
82-
service.path = DriverFinder.get_path(service, options)
43+
self.service = service if service else Service()
44+
self.options = options if options else Options()
45+
self.keep_alive = keep_alive
46+
47+
self.service.path = DriverFinder.get_path(self.service, self.options)
8348

8449
super().__init__(
8550
DesiredCapabilities.CHROME["browserName"],
8651
"goog",
87-
port,
88-
options,
89-
service_args,
90-
desired_capabilities,
91-
service_log_path,
92-
service,
93-
keep_alive,
52+
self.options,
53+
self.service,
54+
self.keep_alive,
9455
)
95-
96-
def create_options(self) -> Options:
97-
return Options()

py/selenium/webdriver/chromium/options.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ def add_experimental_option(self, name: str, value: Union[str, int, dict, List[s
132132
name: The experimental option name.
133133
value: The option value.
134134
"""
135-
if name.lower() == "w3c" and (value == "false" or value is False):
136-
warnings.warn(UserWarning("Manipulating `w3c` setting can have unintended consequences."), stacklevel=2)
137135
self._experimental_options[name] = value
138136

139137
@property
@@ -176,17 +174,6 @@ def to_capabilities(self) -> dict:
176174
"""
177175
caps = self._caps
178176
chrome_options = self.experimental_options.copy()
179-
if "w3c" in chrome_options:
180-
if chrome_options["w3c"]:
181-
warnings.warn(
182-
"Setting 'w3c: True' is redundant and will no longer be allowed", DeprecationWarning, stacklevel=2
183-
)
184-
else:
185-
raise AttributeError(
186-
"setting w3c to False is not allowed, "
187-
"Please update to W3C Syntax: "
188-
"https://www.selenium.dev/blog/2022/legacy-protocol-support/"
189-
)
190177
if self.mobile_options:
191178
chrome_options.update(self.mobile_options)
192179
chrome_options["extensions"] = self.extensions

py/selenium/webdriver/chromium/webdriver.py

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,11 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
import warnings
19-
20-
from selenium.webdriver.chrome.options import Options as ChromeOptions
2118
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
22-
from selenium.webdriver.common.options import BaseOptions
19+
from selenium.webdriver.common.options import ArgOptions
2320
from selenium.webdriver.common.service import Service
24-
from selenium.webdriver.edge.options import Options as EdgeOptions
2521
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2622

27-
DEFAULT_PORT = 0
28-
DEFAULT_SERVICE_LOG_PATH = None
29-
DEFAULT_KEEP_ALIVE = None
30-
3123

3224
class ChromiumDriver(RemoteWebDriver):
3325
"""Controls the WebDriver instance of ChromiumDriver and allows you to
@@ -37,67 +29,25 @@ def __init__(
3729
self,
3830
browser_name,
3931
vendor_prefix,
40-
port=DEFAULT_PORT,
41-
options: BaseOptions = None,
42-
service_args=None,
43-
desired_capabilities=None,
44-
service_log_path=DEFAULT_SERVICE_LOG_PATH,
45-
service: Service = None,
46-
keep_alive=DEFAULT_KEEP_ALIVE,
32+
options: ArgOptions,
33+
service: Service,
34+
keep_alive=True,
4735
) -> None:
4836
"""Creates a new WebDriver instance of the ChromiumDriver. Starts the
4937
service and then creates new WebDriver instance of ChromiumDriver.
5038
5139
:Args:
5240
- browser_name - Browser name used when matching capabilities.
5341
- vendor_prefix - Company prefix to apply to vendor-specific WebDriver extension commands.
54-
- port - Deprecated: port you would like the service to run, if left as 0, a free port will be found.
5542
- options - this takes an instance of ChromiumOptions
56-
- service_args - Deprecated: List of args to pass to the driver service
57-
- desired_capabilities - Deprecated: Dictionary object with non-browser specific
58-
capabilities only, such as "proxy" or "loggingPref".
59-
- service_log_path - Deprecated: Where to log information from the driver.
60-
- keep_alive - Deprecated: Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
43+
- service - Service object for handling the browser driver if you need to pass extra details
44+
- keep_alive - Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
6145
"""
62-
if desired_capabilities:
63-
warnings.warn(
64-
"desired_capabilities has been deprecated, please pass in a Service object",
65-
DeprecationWarning,
66-
stacklevel=2,
67-
)
68-
if port != DEFAULT_PORT:
69-
warnings.warn("port has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2)
70-
self.port = port
71-
if service_log_path != DEFAULT_SERVICE_LOG_PATH:
72-
warnings.warn(
73-
"service_log_path has been deprecated, please pass in a Service object",
74-
DeprecationWarning,
75-
stacklevel=2,
76-
)
77-
if keep_alive != DEFAULT_KEEP_ALIVE and type(self) == __class__:
78-
warnings.warn(
79-
"keep_alive has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
80-
)
81-
else:
82-
keep_alive = True
83-
8446
self.vendor_prefix = vendor_prefix
8547

86-
_ignore_proxy = None
87-
if not options:
88-
options = self.create_options()
89-
90-
if desired_capabilities:
91-
for key, value in desired_capabilities.items():
92-
options.set_capability(key, value)
93-
94-
if options._ignore_local_proxy:
95-
_ignore_proxy = options._ignore_local_proxy
96-
97-
if not service:
98-
raise AttributeError("service cannot be None")
99-
10048
self.service = service
49+
self.options = options
50+
10151
self.service.start()
10252

10353
try:
@@ -107,9 +57,9 @@ def __init__(
10757
browser_name=browser_name,
10858
vendor_prefix=vendor_prefix,
10959
keep_alive=keep_alive,
110-
ignore_proxy=_ignore_proxy,
60+
ignore_proxy=self.options._ignore_local_proxy,
11161
),
112-
options=options,
62+
options=self.options,
11363
)
11464
except Exception:
11565
self.quit()
@@ -240,6 +190,3 @@ def quit(self) -> None:
240190
pass
241191
finally:
242192
self.service.stop()
243-
244-
def create_options(self) -> BaseOptions:
245-
return EdgeOptions() if self.vendor_prefix == "ms" else ChromeOptions()

py/selenium/webdriver/common/service.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
logger = logging.getLogger(__name__)
3636

3737

38-
_HAS_NATIVE_DEVNULL = True
39-
40-
4138
class Service(ABC):
4239
"""The abstract base class for all service objects. Services typically
4340
launch a child program in a new process as an interim process to
@@ -61,7 +58,7 @@ def __init__(
6158
) -> None:
6259
self._path = executable
6360
self.port = port or utils.free_port()
64-
self.log_file = open(os.devnull, "wb") if not _HAS_NATIVE_DEVNULL and log_file == DEVNULL else log_file
61+
self.log_file = open(os.devnull, "wb") if not log_file == DEVNULL else log_file
6562
self.start_error_message = start_error_message or ""
6663
# Default value for every python subprocess: subprocess.Popen(..., creationflags=0)
6764
self.popen_kw = kwargs.pop("popen_kw", {})
@@ -132,7 +129,7 @@ def send_remote_shutdown_command(self) -> None:
132129

133130
def stop(self) -> None:
134131
"""Stops the service."""
135-
if self.log_file != PIPE and not (self.log_file == DEVNULL and _HAS_NATIVE_DEVNULL):
132+
if self.log_file != PIPE and not (self.log_file == DEVNULL):
136133
try:
137134
# Todo: Be explicit in what we are catching here.
138135
if hasattr(self.log_file, "close"):

py/selenium/webdriver/edge/webdriver.py

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,42 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
import warnings
1817

1918
from selenium.webdriver.chromium.webdriver import ChromiumDriver
2019
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2120
from selenium.webdriver.common.driver_finder import DriverFinder
2221

2322
from .options import Options
24-
from .service import DEFAULT_EXECUTABLE_PATH
2523
from .service import Service
2624

27-
DEFAULT_PORT = 0
28-
DEFAULT_SERVICE_LOG_PATH = None
29-
3025

3126
class WebDriver(ChromiumDriver):
32-
"""Controls the Microsoft Edge driver and allows you to drive the browser.
33-
34-
You will need to download the MSEdgeDriver executable from
35-
https://developer.microsoft.com/microsoft-edge/tools/webdriver/
36-
"""
27+
"""Controls the MDEdgeDriver and allows you to drive the browser."""
3728

3829
def __init__(
3930
self,
40-
executable_path=DEFAULT_EXECUTABLE_PATH,
41-
port=DEFAULT_PORT,
42-
options: Options = Options(),
43-
service_args=None,
44-
capabilities=None,
45-
service_log_path=DEFAULT_SERVICE_LOG_PATH,
31+
options: Options = None,
4632
service: Service = None,
47-
keep_alive=False,
48-
verbose=False, # Todo: Why is this now unused?
33+
keep_alive=True,
4934
) -> None:
5035
"""Creates a new instance of the edge driver. Starts the service and
5136
then creates new instance of edge driver.
5237
5338
:Args:
54-
- executable_path - Deprecated: path to the executable. If the default is used it assumes the executable is in the $PATH
55-
- port - Deprecated: port you would like the service to run, if left as 0, a free port will be found.
5639
- options - this takes an instance of EdgeOptions
57-
- service_args - Deprecated: List of args to pass to the driver service
58-
- capabilities - Deprecated: Dictionary object with non-browser specific
59-
capabilities only, such as "proxy" or "loggingPref".
60-
- service_log_path - Deprecated: Where to log information from the driver.
6140
- service - Service object for handling the browser driver if you need to pass extra details
6241
- keep_alive - Whether to configure EdgeRemoteConnection to use HTTP keep-alive.
63-
- verbose - whether to set verbose logging in the service.
6442
"""
65-
if executable_path != "msedgedriver":
66-
warnings.warn(
67-
"executable_path has been deprecated, please pass in a Service object", DeprecationWarning, stacklevel=2
68-
)
43+
self.service = service if service else Service()
44+
self.options = options if options else Options()
45+
self.keep_alive = keep_alive
6946

70-
if not options:
71-
options = self.create_options()
72-
if not service:
73-
service = Service(executable_path, port, service_args, service_log_path)
74-
service.path = DriverFinder.get_path(service, options)
47+
self.service.path = DriverFinder.get_path(self.service, self.options)
7548

7649
super().__init__(
7750
DesiredCapabilities.EDGE["browserName"],
7851
"ms",
79-
port,
80-
options,
81-
service_args,
82-
capabilities,
83-
service_log_path,
84-
service,
85-
keep_alive,
52+
self.options,
53+
self.service,
54+
self.keep_alive,
8655
)
87-
88-
def create_options(self) -> Options:
89-
return Options()

0 commit comments

Comments
 (0)