Skip to content

Commit 8f40a8f

Browse files
authored
[py] Call service.stop() when session can't be started (SeleniumHQ#15636)
1 parent f5b8d3e commit 8f40a8f

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

py/selenium/webdriver/remote/webdriver.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
"""The WebDriver implementation."""
19+
1820
import base64
1921
import contextlib
2022
import copy
@@ -343,9 +345,14 @@ def start_session(self, capabilities: dict) -> None:
343345
"""
344346

345347
caps = _create_caps(capabilities)
346-
response = self.execute(Command.NEW_SESSION, caps)["value"]
347-
self.session_id = response.get("sessionId")
348-
self.caps = response.get("capabilities")
348+
try:
349+
response = self.execute(Command.NEW_SESSION, caps)["value"]
350+
self.session_id = response.get("sessionId")
351+
self.caps = response.get("capabilities")
352+
except Exception:
353+
if self.service is not None:
354+
self.service.stop()
355+
raise
349356

350357
def _wrap_value(self, value):
351358
if isinstance(value, dict):

py/test/selenium/webdriver/chrome/chrome_service_tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
import os
1819
import subprocess
1920
import time
2021
from unittest.mock import patch
2122

2223
import pytest
2324

25+
from selenium.common.exceptions import SessionNotCreatedException
26+
from selenium.webdriver.chrome.options import Options
2427
from selenium.webdriver.chrome.service import Service
2528

2629

@@ -105,6 +108,17 @@ def test_log_output_null_default(driver, capfd) -> None:
105108
driver.quit()
106109

107110

111+
@pytest.mark.no_driver_after_test
112+
def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None:
113+
options = Options()
114+
options.add_argument("--user-data-dir=/no/such/location")
115+
service = Service()
116+
with pytest.raises(SessionNotCreatedException):
117+
clean_driver(options=options, service=service)
118+
assert not service.is_connectable()
119+
assert service.process.poll() is not None
120+
121+
108122
@pytest.fixture
109123
def service():
110124
return Service()

py/test/selenium/webdriver/edge/edge_service_tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
import os
1819
import subprocess
1920
import time
2021
from unittest.mock import patch
2122

2223
import pytest
2324

25+
from selenium.common.exceptions import SessionNotCreatedException
26+
from selenium.webdriver.edge.options import Options
2427
from selenium.webdriver.edge.service import Service
2528

2629

@@ -105,6 +108,17 @@ def test_log_output_null_default(driver, capfd) -> None:
105108
driver.quit()
106109

107110

111+
@pytest.mark.no_driver_after_test
112+
def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None:
113+
options = Options()
114+
options.add_argument("--user-data-dir=/no/such/location")
115+
service = Service()
116+
with pytest.raises(SessionNotCreatedException):
117+
clean_driver(options=options, service=service)
118+
assert not service.is_connectable()
119+
assert service.process.poll() is not None
120+
121+
108122
@pytest.fixture
109123
def service():
110124
return Service()

py/test/selenium/webdriver/firefox/firefox_service_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
1718
import os
1819
import subprocess
1920
from unittest.mock import patch
2021

2122
import pytest
2223

24+
from selenium.common.exceptions import SessionNotCreatedException
2325
from selenium.webdriver import Firefox
26+
from selenium.webdriver.firefox.options import Options
2427
from selenium.webdriver.firefox.service import Service
2528

2629

@@ -61,6 +64,16 @@ def test_log_output_as_stdout(capfd) -> None:
6164
driver.quit()
6265

6366

67+
def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None:
68+
options = Options()
69+
options.add_argument("-profile=/no/such/location")
70+
service = Service()
71+
with pytest.raises(SessionNotCreatedException):
72+
clean_driver(options=options, service=service)
73+
assert not service.is_connectable()
74+
assert service.process.poll() is not None
75+
76+
6477
@pytest.fixture
6578
def service():
6679
return Service()

0 commit comments

Comments
 (0)