diff --git a/py/selenium/webdriver/remote/webdriver.py b/py/selenium/webdriver/remote/webdriver.py index 2c192673a7089..e43913f570028 100644 --- a/py/selenium/webdriver/remote/webdriver.py +++ b/py/selenium/webdriver/remote/webdriver.py @@ -14,7 +14,9 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. + """The WebDriver implementation.""" + import base64 import contextlib import copy @@ -343,9 +345,14 @@ def start_session(self, capabilities: dict) -> None: """ caps = _create_caps(capabilities) - response = self.execute(Command.NEW_SESSION, caps)["value"] - self.session_id = response.get("sessionId") - self.caps = response.get("capabilities") + try: + response = self.execute(Command.NEW_SESSION, caps)["value"] + self.session_id = response.get("sessionId") + self.caps = response.get("capabilities") + except Exception: + if self.service is not None: + self.service.stop() + raise def _wrap_value(self, value): if isinstance(value, dict): diff --git a/py/test/selenium/webdriver/chrome/chrome_service_tests.py b/py/test/selenium/webdriver/chrome/chrome_service_tests.py index d1c565b99aabc..345f6b5d1bfdd 100644 --- a/py/test/selenium/webdriver/chrome/chrome_service_tests.py +++ b/py/test/selenium/webdriver/chrome/chrome_service_tests.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. + import os import subprocess import time @@ -21,6 +22,8 @@ import pytest +from selenium.common.exceptions import SessionNotCreatedException +from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service @@ -105,6 +108,17 @@ def test_log_output_null_default(driver, capfd) -> None: driver.quit() +@pytest.mark.no_driver_after_test +def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None: + options = Options() + options.add_argument("--user-data-dir=/no/such/location") + service = Service() + with pytest.raises(SessionNotCreatedException): + clean_driver(options=options, service=service) + assert not service.is_connectable() + assert service.process.poll() is not None + + @pytest.fixture def service(): return Service() diff --git a/py/test/selenium/webdriver/edge/edge_service_tests.py b/py/test/selenium/webdriver/edge/edge_service_tests.py index c997aad13aa79..2ac1f0e2c6e2c 100644 --- a/py/test/selenium/webdriver/edge/edge_service_tests.py +++ b/py/test/selenium/webdriver/edge/edge_service_tests.py @@ -14,6 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. + import os import subprocess import time @@ -21,6 +22,8 @@ import pytest +from selenium.common.exceptions import SessionNotCreatedException +from selenium.webdriver.edge.options import Options from selenium.webdriver.edge.service import Service @@ -105,6 +108,17 @@ def test_log_output_null_default(driver, capfd) -> None: driver.quit() +@pytest.mark.no_driver_after_test +def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None: + options = Options() + options.add_argument("--user-data-dir=/no/such/location") + service = Service() + with pytest.raises(SessionNotCreatedException): + clean_driver(options=options, service=service) + assert not service.is_connectable() + assert service.process.poll() is not None + + @pytest.fixture def service(): return Service() diff --git a/py/test/selenium/webdriver/firefox/firefox_service_tests.py b/py/test/selenium/webdriver/firefox/firefox_service_tests.py index 4c64ef4c211f4..567686ef9114e 100644 --- a/py/test/selenium/webdriver/firefox/firefox_service_tests.py +++ b/py/test/selenium/webdriver/firefox/firefox_service_tests.py @@ -14,13 +14,16 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. + import os import subprocess from unittest.mock import patch import pytest +from selenium.common.exceptions import SessionNotCreatedException from selenium.webdriver import Firefox +from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.service import Service @@ -61,6 +64,16 @@ def test_log_output_as_stdout(capfd) -> None: driver.quit() +def test_driver_is_stopped_if_browser_cant_start(clean_driver) -> None: + options = Options() + options.add_argument("-profile=/no/such/location") + service = Service() + with pytest.raises(SessionNotCreatedException): + clean_driver(options=options, service=service) + assert not service.is_connectable() + assert service.process.poll() is not None + + @pytest.fixture def service(): return Service()