import pytest import pandas as pd from page_objects.login_page import LoginPage @pytest.fixture(scope="function") def logged_in_driver(driver, base_url): """ A fixture that provides a logged-in WebDriver instance. It performs the following steps: 1. Reads valid login credentials from 'data/login_data.csv'. 2. Navigates to the login page. 3. Performs the login action. 4. Yields the driver to the test function. The test function then runs on a page that requires authentication. """ # 1. 读取有效的登录凭据 (假设第一行为有效数据) df = pd.read_csv('data/login_data.csv') valid_user = df[df['test_case'] == 'valid_credentials'].iloc[0] email = valid_user['email'] password = valid_user['password'] # 2. 初始化登录页面并打开 login_page = LoginPage(driver) login_page.open(base_url) # 3. 执行登录 login_page.login(email, password) # 4. 将已登录的 driver 提供给测试用例 yield driver # 测试结束后,driver fixture 会自动关闭浏览器