1+ import pytest
2+ import json
3+ from selenium import webdriver
4+ from selenium .webdriver .chrome .service import Service
5+ from webdriver_manager .chrome import ChromeDriverManager
6+
7+
8+ def pytest_addoption (parser ):
9+ """
10+ This is the correct hook to add custom INI options.
11+ """
12+ parser .addini ("base_url" , help = "The base URL for the application under test" , default = None )
13+
14+
15+ @pytest .fixture (scope = "session" )
16+ def base_url (request ):
17+ """
18+ Fixture to get the base_url from the pytest.ini config file.
19+ """
20+ return request .config .getini ("base_url" )
21+
22+ @pytest .fixture (scope = "session" )
23+ def test_data ():
24+ """
25+ Fixture to load test data from the JSON file.
26+ """
27+ with open ("data/test_data.json" ) as f :
28+ data = json .load (f )
29+ return data
30+
31+
32+ @pytest .fixture (scope = "function" )
33+ def driver ():
34+ """
35+ This fixture creates a new REMOTE Chrome WebDriver instance.
36+ It connects to the 'selenium' service defined in docker-compose.yml.
37+ """
38+
39+ # --- Setup Chrome Options ---
40+ chrome_options = webdriver .ChromeOptions ()
41+ chrome_options .add_argument ("--incognito" )
42+ chrome_options .add_argument ("--start-maximized" )
43+ chrome_options .add_argument ("--disable-extensions" )
44+ chrome_options .add_argument ("--disable-features=PasswordLeakDetection" )
45+
46+ prefs = {
47+ "credentials_enable_service" : False ,
48+ "profile.password_manager_enabled" : False ,
49+ "profile.password_manager_leak_detection_enabled" : False
50+ }
51+ chrome_options .add_experimental_option ("prefs" , prefs )
52+ chrome_options .add_experimental_option ("excludeSwitches" , ["enable-automation" ])
53+
54+ # --- Driver Initialization ---
55+ # The URL now points to our new service name: 'selenium'
56+ REMOTE_HUB_URL = "http://selenium:4444/wd/hub"
57+
58+ # No more try/except. When running in Docker, it MUST
59+ # connect to this URL, or we want it to fail.
60+ driver_instance = webdriver .Remote (
61+ command_executor = REMOTE_HUB_URL ,
62+ options = chrome_options
63+ )
64+
65+ yield driver_instance
66+
67+ # --- Teardown ---
68+ driver_instance .quit ()
0 commit comments