MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / driver

Function driver

py/conftest.py:563–616  ·  view source on GitHub ↗
(request, server)

Source from the content-addressed store, hash-verified

561
562@pytest.fixture
563def driver(request, server):
564 global selenium_driver
565 driver_class = getattr(request, "param", "Chrome").lower()
566
567 if selenium_driver is None:
568 selenium_driver = Driver(driver_class, request)
569 if server:
570 selenium_driver._server = server
571
572 # skip tests if not available on the platform
573 if not selenium_driver.is_platform_valid:
574 pytest.skip(f"{driver_class} tests can only run on {selenium_driver.exe_platform}")
575
576 _skip_unless_remote(request, selenium_driver.is_remote)
577
578 # skip tests for drivers that don't support BiDi when --bidi is enabled
579 if selenium_driver.bidi and driver_class.lower() not in selenium_driver.supported_bidi_drivers:
580 pytest.skip(f"{driver_class} does not support BiDi")
581
582 _apply_xfail_markers(request, driver_class, selenium_driver.is_remote)
583
584 # For BiDi tests, only restart driver when explicitly marked as needing fresh driver.
585 # Tests marked with @pytest.mark.needs_fresh_driver get full driver restart for test isolation.
586 # Cleanup after every test is recommended.
587 if selenium_driver is not None and selenium_driver.bidi:
588 if request.node.get_closest_marker("needs_fresh_driver"):
589 request.addfinalizer(selenium_driver.stop_driver)
590 else:
591
592 def ensure_valid_window():
593 try:
594 driver = selenium_driver._driver
595 if driver:
596 try:
597 # Check if current window is still valid
598 driver.current_window_handle
599 except Exception:
600 # restart driver
601 selenium_driver.stop_driver()
602 except Exception:
603 pass
604
605 request.addfinalizer(ensure_valid_window) # noqa: PT021
606
607 yield selenium_driver.driver
608
609 if request.node.get_closest_marker("no_driver_after_test") and selenium_driver is not None:
610 try:
611 selenium_driver.stop_driver()
612 except WebDriverException:
613 pass
614 except Exception:
615 raise
616 selenium_driver = None
617
618
619@pytest.fixture(scope="session", autouse=True)

Callers

nothing calls this directly

Calls 5

stop_driverMethod · 0.95
_skip_unless_remoteFunction · 0.85
_apply_xfail_markersFunction · 0.85
skipMethod · 0.80
DriverClass · 0.70

Tested by

no test coverage detected