cs_test_demo_0924/
├── config/
│ ├── __init__.py
│ └── logging.py # 日志配置
├── lib/ # 新增:测试库模块
│ ├── __init__.py
│ ├── config.py # 测试配置常量
│ ├── browser_driver.py # 浏览器驱动管理
│ ├── page_operations.py # 页面操作函数
│ └── fixtures.py # pytest fixture
├── tests/
│ └── test_page.py # 测试文件(仅包含测试逻辑)
├── logs/
├── reports/
└── requirements.txt
- TestConfig: 测试相关配置(超时时间、URL、选择器等)
- BrowserConfig: 浏览器相关配置(支持的浏览器、启动参数等)
get_default_chrome_options(): Chrome浏览器选项配置get_default_firefox_options(): Firefox浏览器选项配置choose_driver(): 根据浏览器名称创建WebDriver实例
wait_for_loading_to_disappear(): 等待加载遮罩层消失find_element_with_multiple_selectors(): 多选择器元素查找safe_click_element(): 安全点击元素wait_for_page_ready(): 等待页面完全加载check_element_visibility(): 检查元素可见性
driver(): Chrome WebDriver fixturefirefox_driver(): Firefox WebDriver fixture
test_page_load_strategy_normal(): 页面加载和登录按钮交互测试test_page_elements_visibility(): 页面元素可见性测试
pytest tests/test_page.py -vpytest tests/test_page.py::test_page_load_strategy_normal -v# 在测试函数中使用firefox_driver fixture
def test_with_firefox(firefox_driver):
firefox_driver.get("https://example.com")# 在 lib/config.py 中修改
class TestConfig:
TEST_URL = "https://your-test-site.com"# 在 lib/config.py 中添加
class TestConfig:
LOGIN_SELECTORS = [
"//button[contains(text(), '登录')]",
".login-button",
"#login-btn",
# 添加新的选择器
]# 在 lib/config.py 中修改
class TestConfig:
DEFAULT_TIMEOUT = 30 # 增加到30秒
CLICK_TIMEOUT = 15 # 增加到15秒确保项目根目录在Python路径中:
import sys
from pathlib import Path
root_dir = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(root_dir))检查浏览器驱动是否正确安装:
# Chrome
chromedriver --version
# Firefox
geckodriver --version