Web UI automated testing framework (Pytest+Selenium+Allure/Pytest-html+Loguru)

1. Introduction to the framework

This framework is mainly a WEB UI automation framework based on Python + pytest + selenium + pytest-html/Allure + loguru + email notification/enterprise WeChat notification/DingTalk notification.

2. Implement functions

  • Based on the combination of PO design patterns, the platform can realize automated execution of test cases and the generation of automated test reports, including screenshots of failed use cases during automated test execution.
  • Use webdriver_manager to automatically download webdriver, say goodbye to manual downloading, and no longer have to worry about webdriver version issues.
  • Supports specifying one or more browsers through the command line, and multiple browsers can run at the same time.
  • Supports specifying the operating environment through the command line, enabling one-click switching of environments and solving the problem of mutual interaction between multiple environments.
  • Using loguru to manage logs can output more elegant and concise logs
  • DingTalk and Enterprise WeChat notifications: Supports multiple notification scenarios. After successful execution, you can choose to send DingTalk, Enterprise WeChat, or email notifications
  • Use pipenv to manage virtual environments and dependency files, and you can pipenv installinstall dependency packages with one click.
  • Choose from a variety of reports: the framework supports pytest-html and Allure test reports, and the required reports can be dynamically configured.
  • Supports using allure to set use case priorities and run use cases with specified priorities.

3. Directory structure

├────case_utils/
│    ├────__init__.py
│    ├────allure_handle.py
│    ├────basepage.py
│    ├────data_handle.py
│    ├────get_driver.py
│    ├────get_results_handle.py
│    ├────platform_handle.py
│    ├────send_result_handle.py
│    └────url_handle.py
├────common_utils/
│    ├────__init__.py
│    ├────base_request.py
│    ├────bs4_handle.py
│    ├────dingding_handle.py
│    ├────files_handle.py
│    ├────time_handle.py
│    ├────wechat_handle.py
│    ├────yagmail_handle.py
│    └────yaml_handle.py
├────config/
│    ├────__init__.py
│    ├────allure_config/
│    │    ├────http_server.exe
│    │    ├────logo.svg
│    │    └────双击打开Allure报告.bat
│    ├────global_vars.py
│    ├────models.py
│    ├────path_config.py
│    ├────pytest_html_config/
│    │    └────pytest_html_report.css
│    └────settings.py
├────data/
│    ├────create_project_data.py
│    └────login_data.py
├────lib/
├────outputs/
├────page/
│    ├────__init__.py
│    ├────common_page.py
│    ├────home_page.py
│    ├────login_page.py
│    └────projects/
│    │    ├────__init__.py
│    │    ├────create_project_page.py
│    │    ├────project_detail_page.py
│    │    └────projects_page.py
└────test_case/
│    ├────__init__.py
│    ├────conftest.py
│    ├────test_login.py
│    └────test_projects/
│    │    ├────__init__.py
│    │    ├────conftest.py
│    │    └────test_create_project.py
├────conftest.py
├────Pipfile
├────Pipfile.lock
├────pytest.ini
├────README.md
├────.gitignore
├────run.py

4. Dependency libraries

jsonpath = "==0.82"
openpyxl = "==3.0.9"
pytest = "==6.2.5"
pyyaml = "==6.0"
requests = "==2.26.0"
loguru = "*"
pytest-rerunfailures = "*"
faker = "*"
deepdiff = "*"
pymysql = "*"
yagmail = "*"
selenium = "*"
pyautogui = "*"
pywinauto = "*"
pytest-html = "==2.1.1"
allure-pytest = "*"
beautifulsoup4 = "*"
webdriver-manager = "*"
requests-toolbelt = "*"

5. Installation tutorial

  1. Use the Git tool to clone the code locally or directly download the compressed package ZIP
git clone https://gitlink.org.cn/floraachy/uiautotest.git
  1. Set up a python environment on the local computer. The python version I use is 3.9

  2. Install pipenv

# 建议在项目根目录下执行命令安装
pip install pipenv
  1. Use pipenv to manage the installation of environmental dependency packages: pipenv install (must be executed in the project root directory)
   注意:使用pipenv install会自动安装Pipfile里面的依赖包,该依赖包仅安装在虚拟环境里,不安装在测试机。

The above environment has been set up, including the framework dependency packages.

6. How to create use cases

1. Modify configuration fileconfig.settings.py

1) Confirm the various parameters of RunConfig. You can adjust the number of failed reruns rerunand the interval between failed reruns reruns_delay. When the maximum number of failures is reached, execution will stop. max_fail
2) Confirm whether to send the test results after the test is completed. This is controlled by SEND_RESULT_TYPE and fill in the corresponding email/ DingTalk/Enterprise WeChat configuration information
3) Specify the log collection level, controlled by LOG_LEVEL

2. Modify global variables and add test dataconfig.global_vars.py

  1. ENV_VARS["common"] are some public parameters, such as report title, report name, tester, and test department. It will be displayed on the test report later. If there are others, you can add them yourself.
    2) ENV_VARS["test"] is to save some test data of the test environment. ENV_VARS["live"] saves some test data of the live environment. If there are other environments, you can continue to add them, such as adding ENV_VARS["dev"] = {"host": "", …}

3. Delete the sample use case data in the framework

1) Delete dataall files in the directory
2) Delete pageall files in the directory
3) Delete test_caseall written use cases in the directory

4. Write test cases

page1. Create a new file in the directory pyto manage the positioning of elements and page operation methods.

2. dataCreate a new pyfile in the directory to manage the test data required in the test case

  • A dictionary needs to exist in the test data case_commonto configure the allure report. The reference is as follows:
case_common = {
    
    
    "allure_epic": "GitLink",
    "allure_feature": "登录模块",
}
  • The actual execution data of the use case needs to be referenced as follows:
# allure_story以及severity用于配置allure报告,建议配置
# 字典命名可以自定义
# title字段建议保留,涉及到报告上的显示
login_pop_success = {
    
    
    "allure_story": "弹窗登录",
    "cases":
        [
            {
    
    "title": "弹窗登录: 正确用户名和密码登录成功", "user": "${login}", "password": "${password}",
             "run": False,
             "severity": "critical"}
        ]
}

3. test_caseWrite test cases in the directory

  • Note: Test case commands need to follow pytestnaming rules.

7. Run automated tests

1. Activate an existing virtual environment

  • (If it does not exist, it will create one): pipenv shell (must be executed in the project root directory)

2. run

在pycharm>terminal或者电脑命令窗口,进入项目根路径,执行如下命令(如果依赖包是安装在虚拟环境中,需要先启动虚拟环境)。
  > python run.py  (默认在test环境运行测试用例, 报告采用allure)
  > python run.py -m demo 在test环境仅运行打了标记demo用例, 默认报告采用allure
  > python run.py -env live 在live环境运行测试用例
  > python run.py -env=test 在test环境运行测试用例
  > python run.py -report=pytest-html (默认在test环境运行测试用例, 报告采用pytest-html)
  > python run.py -driver chrome edge  (使用chrome以及edge浏览器运行测试用例)

Notice:

  • If pycharm.interpreter has all the dependency packages required by the framework, you can run.pyrun it directly through pycharm by right-clicking

8. Detailed function description

todo to be added

9. Problems that may be encountered when initializing the project

10. Contact me

For any questions about the framework, please feel free to contact me!

Guess you like

Origin blog.csdn.net/FloraCHY/article/details/131762483