Skip to content

Commit cadb9de

Browse files
committed
[py] have superclass always create remote connection
1 parent edac26c commit cadb9de

8 files changed

Lines changed: 84 additions & 72 deletions

File tree

py/selenium/webdriver/chrome/remote_connection.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@
1616
# under the License.
1717
import typing
1818

19+
from selenium.webdriver import DesiredCapabilities
1920
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
2021

2122

2223
class ChromeRemoteConnection(ChromiumRemoteConnection):
24+
browser_name = DesiredCapabilities.CHROME["browserName"]
25+
2326
def __init__(
2427
self,
2528
remote_server_addr: str,
2629
keep_alive: bool = True,
2730
ignore_proxy: typing.Optional[bool] = False,
2831
) -> None:
29-
super().__init__(remote_server_addr, 'goog', keep_alive, ignore_proxy=ignore_proxy)
32+
super().__init__(
33+
remote_server_addr=remote_server_addr,
34+
vendor_prefix="goog",
35+
browser_name="chrome",
36+
keep_alive=keep_alive,
37+
ignore_proxy=ignore_proxy,
38+
)

py/selenium/webdriver/chrome/webdriver.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# under the License.
1717

1818
from selenium.webdriver.chromium.webdriver import ChromiumDriver
19-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2019

2120
from .options import Options
2221
from .service import Service
@@ -42,10 +41,4 @@ def __init__(
4241
service = service if service else Service()
4342
options = options if options else Options()
4443

45-
super().__init__(
46-
DesiredCapabilities.CHROME["browserName"],
47-
"goog",
48-
options,
49-
service,
50-
keep_alive,
51-
)
44+
super().__init__(options=options, service=service, keep_alive=keep_alive)

py/selenium/webdriver/chromium/webdriver.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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
1718

1819
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
1920
from selenium.webdriver.common.driver_finder import DriverFinder
@@ -28,11 +29,12 @@ class ChromiumDriver(RemoteWebDriver):
2829

2930
def __init__(
3031
self,
31-
browser_name,
32-
vendor_prefix,
33-
options: ArgOptions,
34-
service: Service,
35-
keep_alive=True,
32+
browser_name: str = None,
33+
vendor_prefix: str = None,
34+
options: ArgOptions = ArgOptions(),
35+
service: Service = None,
36+
keep_alive: bool = True,
37+
remote_connection: ChromiumRemoteConnection = None,
3638
) -> None:
3739
"""Creates a new WebDriver instance of the ChromiumDriver. Starts the
3840
service and then creates new WebDriver instance of ChromiumDriver.
@@ -44,25 +46,34 @@ def __init__(
4446
- service - Service object for handling the browser driver if you need to pass extra details
4547
- keep_alive - Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
4648
"""
47-
self.vendor_prefix = vendor_prefix
4849

49-
self.service = service
50+
if browser_name:
51+
warnings.warn(
52+
"browser_name is not necessary when an Options class is used", DeprecationWarning, stacklevel=2
53+
)
5054

55+
self.service = service
5156
self.service.path = DriverFinder.get_path(self.service, options)
52-
5357
self.service.start()
5458

55-
try:
56-
super().__init__(
57-
command_executor=ChromiumRemoteConnection(
58-
remote_server_addr=self.service.service_url,
59-
browser_name=browser_name,
60-
vendor_prefix=vendor_prefix,
61-
keep_alive=keep_alive,
62-
ignore_proxy=options._ignore_local_proxy,
63-
),
64-
options=options,
59+
if vendor_prefix:
60+
warnings.warn(
61+
"vendor_prefix is deprecated, use a ChromiumRemoteConnection with command_executor instead",
62+
DeprecationWarning,
63+
stacklevel=2,
6564
)
65+
remote_connection = ChromiumRemoteConnection(
66+
remote_server_addr=self.service.service_url,
67+
browser_name=browser_name,
68+
vendor_prefix=vendor_prefix,
69+
keep_alive=keep_alive,
70+
ignore_proxy=options._ignore_local_proxy,
71+
)
72+
73+
command_executor = remote_connection if remote_connection else self.service.service_url
74+
75+
try:
76+
super().__init__(command_executor=command_executor, options=options, keep_alive=keep_alive)
6677
except Exception:
6778
self.quit()
6879
raise
@@ -181,8 +192,7 @@ def stop_casting(self, sink_name: str) -> dict:
181192
return self.execute("stopCasting", {"sinkName": sink_name})
182193

183194
def quit(self) -> None:
184-
"""Closes the browser and shuts down the ChromiumDriver executable that
185-
is started when starting the ChromiumDriver."""
195+
"""Ends the driver session and shuts down the ChromiumDriver executable."""
186196
try:
187197
super().quit()
188198
except Exception:

py/selenium/webdriver/edge/remote_connection.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@
1616
# under the License.
1717
import typing
1818

19+
from selenium.webdriver import DesiredCapabilities
1920
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
2021

2122

2223
class EdgeRemoteConnection(ChromiumRemoteConnection):
24+
browser_name = DesiredCapabilities.EDGE["browserName"]
25+
2326
def __init__(
2427
self,
2528
remote_server_addr: str,
2629
keep_alive: bool = True,
2730
ignore_proxy: typing.Optional[bool] = False,
2831
) -> None:
29-
super().__init__(remote_server_addr, 'ms', keep_alive, ignore_proxy=ignore_proxy)
32+
super().__init__(
33+
remote_server_addr=remote_server_addr,
34+
vendor_prefix="goog",
35+
browser_name="MicrosoftEdge",
36+
keep_alive=keep_alive,
37+
ignore_proxy=ignore_proxy,
38+
)

py/selenium/webdriver/edge/webdriver.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# under the License.
1717

1818
from selenium.webdriver.chromium.webdriver import ChromiumDriver
19-
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2019

2120
from .options import Options
2221
from .service import Service
@@ -29,7 +28,7 @@ def __init__(
2928
self,
3029
options: Options = None,
3130
service: Service = None,
32-
keep_alive=True,
31+
keep_alive: bool = True,
3332
) -> None:
3433
"""Creates a new instance of the edge driver. Starts the service and
3534
then creates new instance of edge driver.
@@ -42,10 +41,4 @@ def __init__(
4241
service = service if service else Service()
4342
options = options if options else Options()
4443

45-
super().__init__(
46-
DesiredCapabilities.EDGE["browserName"],
47-
"ms",
48-
options,
49-
service,
50-
keep_alive,
51-
)
44+
super().__init__(options=options, service=service, keep_alive=keep_alive)

py/selenium/webdriver/firefox/webdriver.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2727

2828
from .options import Options
29-
from .remote_connection import FirefoxRemoteConnection
3029
from .service import Service
3130

3231
logger = logging.getLogger(__name__)
@@ -42,7 +41,7 @@ def __init__(
4241
self,
4342
options: Options = None,
4443
service: Service = None,
45-
keep_alive=True,
44+
keep_alive: bool = True,
4645
) -> None:
4746
"""Creates a new instance of the Firefox driver. Starts the service and
4847
then creates new instance of Firefox driver.
@@ -59,24 +58,23 @@ def __init__(
5958
self.service.path = DriverFinder.get_path(self.service, options)
6059
self.service.start()
6160

62-
executor = FirefoxRemoteConnection(
63-
remote_server_addr=self.service.service_url,
64-
ignore_proxy=options._ignore_local_proxy,
65-
keep_alive=keep_alive,
66-
)
67-
super().__init__(command_executor=executor, options=options)
61+
try:
62+
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
63+
except Exception:
64+
self.quit()
65+
raise
6866

6967
self._is_remote = False
7068

7169
def quit(self) -> None:
72-
"""Quits the driver and close every associated window."""
70+
"""Ends the driver session and shuts down the geckodriver executable."""
7371
try:
7472
super().quit()
7573
except Exception:
7674
# We don't care about the message because something probably has gone wrong
7775
pass
78-
79-
self.service.stop()
76+
finally:
77+
self.service.stop()
8078

8179
def set_context(self, context) -> None:
8280
self.execute("SET_CONTEXT", {"context": context})

py/selenium/webdriver/ie/webdriver.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# under the License.
1717

1818
from selenium.webdriver.common.driver_finder import DriverFinder
19-
from selenium.webdriver.remote.remote_connection import RemoteConnection
2019
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2120

2221
from .options import Options
@@ -49,15 +48,20 @@ def __init__(
4948
self.service.path = DriverFinder.get_path(self.service, options)
5049
self.service.start()
5150

52-
executor = RemoteConnection(
53-
remote_server_addr=self.service.service_url,
54-
keep_alive=keep_alive,
55-
ignore_proxy=options._ignore_local_proxy,
56-
)
51+
try:
52+
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
53+
except Exception:
54+
self.quit()
55+
raise
5756

58-
super().__init__(command_executor=executor, options=options)
5957
self._is_remote = False
6058

61-
def quit(self) -> None:
62-
super().quit()
63-
self.service.stop()
59+
def quit(self):
60+
"""Ends the driver session and shuts down the safaridriver executable if necessary."""
61+
try:
62+
super().quit()
63+
except Exception:
64+
# We don't care about the message because something probably has gone wrong
65+
pass
66+
finally:
67+
self.service.stop()

py/selenium/webdriver/safari/webdriver.py

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

18-
import http.client as http_client
1918
import warnings
2019

2120
from selenium.common.exceptions import WebDriverException
2221
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2322

2423
from ..common.driver_finder import DriverFinder
2524
from .options import Options
26-
from .remote_connection import SafariRemoteConnection
2725
from .service import Service
2826

2927

@@ -63,22 +61,20 @@ def __init__(
6361
if not self._reuse_service:
6462
self.service.start()
6563

66-
executor = SafariRemoteConnection(
67-
remote_server_addr=self.service.service_url,
68-
keep_alive=keep_alive,
69-
ignore_proxy=options._ignore_local_proxy,
70-
)
71-
72-
super().__init__(command_executor=executor, options=options)
64+
try:
65+
super().__init__(command_executor=self.service.service_url, options=options, keep_alive=keep_alive)
66+
except Exception:
67+
self.quit()
68+
raise
7369

7470
self._is_remote = False
7571

7672
def quit(self):
77-
"""Closes the browser and shuts down the SafariDriver executable that
78-
is started when starting the SafariDriver."""
73+
"""Ends the driver session and shuts down the safaridriver executable if necessary."""
7974
try:
8075
super().quit()
81-
except http_client.BadStatusLine:
76+
except Exception:
77+
# We don't care about the message because something probably has gone wrong
8278
pass
8379
finally:
8480
if not self._reuse_service:

0 commit comments

Comments
 (0)