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

Function driver

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

Source from the content-addressed store, hash-verified

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