Skip to content

altedic/web-auto-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

测试框架说明

📁 项目结构

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

🔧 模块说明

1. lib/config.py - 配置管理

  • TestConfig: 测试相关配置(超时时间、URL、选择器等)
  • BrowserConfig: 浏览器相关配置(支持的浏览器、启动参数等)

2. lib/browser_driver.py - 浏览器驱动

  • get_default_chrome_options(): Chrome浏览器选项配置
  • get_default_firefox_options(): Firefox浏览器选项配置
  • choose_driver(): 根据浏览器名称创建WebDriver实例

3. lib/page_operations.py - 页面操作

  • wait_for_loading_to_disappear(): 等待加载遮罩层消失
  • find_element_with_multiple_selectors(): 多选择器元素查找
  • safe_click_element(): 安全点击元素
  • wait_for_page_ready(): 等待页面完全加载
  • check_element_visibility(): 检查元素可见性

4. lib/fixtures.py - pytest fixture

  • driver(): Chrome WebDriver fixture
  • firefox_driver(): Firefox WebDriver fixture

5. tests/test_page.py - 测试用例

  • test_page_load_strategy_normal(): 页面加载和登录按钮交互测试
  • test_page_elements_visibility(): 页面元素可见性测试

🚀 使用方法

运行所有测试

pytest tests/test_page.py -v

运行特定测试

pytest tests/test_page.py::test_page_load_strategy_normal -v

使用Firefox浏览器

# 在测试函数中使用firefox_driver fixture
def test_with_firefox(firefox_driver):
    firefox_driver.get("https://example.com")

📝 配置自定义

修改测试URL

# 在 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))

WebDriver错误

检查浏览器驱动是否正确安装:

# Chrome
chromedriver --version

# Firefox  
geckodriver --version

About

web-auto-test

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages