Skip to content

Commit d7291fd

Browse files
committed
[py] ensure all drivers stop service during driver quit
1 parent b1aae60 commit d7291fd

File tree

4 files changed

+43
-29
lines changed

4 files changed

+43
-29
lines changed

py/selenium/webdriver/chromium/webdriver.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,25 @@ def __init__(
4444
- service - Service object for handling the browser driver if you need to pass extra details
4545
- keep_alive - Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
4646
"""
47-
self.vendor_prefix = vendor_prefix
48-
4947
self.service = service
5048

5149
self.service.path = DriverFinder.get_path(self.service, options)
52-
5350
self.service.start()
5451

52+
executor = ChromiumRemoteConnection(
53+
remote_server_addr=self.service.service_url,
54+
browser_name=browser_name,
55+
vendor_prefix=vendor_prefix,
56+
keep_alive=keep_alive,
57+
ignore_proxy=options._ignore_local_proxy,
58+
)
59+
5560
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,
65-
)
61+
super().__init__(command_executor=executor, options=options)
6662
except Exception:
6763
self.quit()
6864
raise
65+
6966
self._is_remote = False
7067

7168
def launch_app(self, id):
@@ -181,8 +178,7 @@ def stop_casting(self, sink_name: str) -> dict:
181178
return self.execute("stopCasting", {"sinkName": sink_name})
182179

183180
def quit(self) -> None:
184-
"""Closes the browser and shuts down the ChromiumDriver executable that
185-
is started when starting the ChromiumDriver."""
181+
"""Closes the browser and shuts down the ChromiumDriver executable."""
186182
try:
187183
super().quit()
188184
except Exception:

py/selenium/webdriver/firefox/webdriver.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,27 @@ def __init__(
6161

6262
executor = FirefoxRemoteConnection(
6363
remote_server_addr=self.service.service_url,
64-
ignore_proxy=options._ignore_local_proxy,
6564
keep_alive=keep_alive,
65+
ignore_proxy=options._ignore_local_proxy,
6666
)
67-
super().__init__(command_executor=executor, options=options)
67+
68+
try:
69+
super().__init__(command_executor=executor, options=options)
70+
except Exception:
71+
self.quit()
72+
raise
6873

6974
self._is_remote = False
7075

7176
def quit(self) -> None:
72-
"""Quits the driver and close every associated window."""
77+
"""Closes the browser and shuts down the GeckoDriver executable."""
7378
try:
7479
super().quit()
7580
except Exception:
7681
# We don't care about the message because something probably has gone wrong
7782
pass
78-
79-
self.service.stop()
83+
finally:
84+
self.service.stop()
8085

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

py/selenium/webdriver/ie/webdriver.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,20 @@ def __init__(
5555
ignore_proxy=options._ignore_local_proxy,
5656
)
5757

58-
super().__init__(command_executor=executor, options=options)
58+
try:
59+
super().__init__(command_executor=executor, options=options)
60+
except Exception:
61+
self.quit()
62+
raise
63+
5964
self._is_remote = False
6065

6166
def quit(self) -> None:
62-
super().quit()
63-
self.service.stop()
67+
"""Closes the browser and shuts down the IEServerDriver executable."""
68+
try:
69+
super().quit()
70+
except Exception:
71+
# We don't care about the message because something probably has gone wrong
72+
pass
73+
finally:
74+
self.service.stop()

py/selenium/webdriver/safari/webdriver.py

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

18-
import http.client as http_client
19-
2018
from selenium.common.exceptions import WebDriverException
2119
from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
2220

@@ -58,16 +56,20 @@ def __init__(
5856
ignore_proxy=options._ignore_local_proxy,
5957
)
6058

61-
super().__init__(command_executor=executor, options=options)
59+
try:
60+
super().__init__(command_executor=executor, options=options)
61+
except Exception:
62+
self.quit()
63+
raise
6264

6365
self._is_remote = False
6466

6567
def quit(self):
66-
"""Closes the browser and shuts down the SafariDriver executable that
67-
is started when starting the SafariDriver."""
68+
"""Closes the browser and shuts down the SafariDriver executable."""
6869
try:
6970
super().quit()
70-
except http_client.BadStatusLine:
71+
except Exception:
72+
# We don't care about the message because something probably has gone wrong
7173
pass
7274
finally:
7375
if not self.service.reuse_service:

0 commit comments

Comments
 (0)