0


selenium进阶之web自动化项目框架搭建(Python版)

web自动化项目框架搭建

1、项目结构
web自动化框架的设计,同接口自动化框架一样,采用分层设计。
文件或目录说明common常用模块,常用的一些函数封装testcases用例模块,所有的测试用例test_data用例数据logs日志目录reports报告settings.py配置main.py项目入口
2、settings.py

import os
# 项目根目录
BASE_DIR = os.path.dirname(os.path.abspath(__file__))# 测试用例路径
TEST_CASE_DIR = os.path.join(BASE_DIR,'testcases')# 项目主机
PROJECT_HOST ='https://blog.csdn.net'# 接口信息
INTERFACE ={'login': PROJECT_HOST +'/testleaf'}# 日志配置
LOG_CONFIG ={'name':'project','filename': os.path.join(BASE_DIR,'logs','testleaf.log'),# 'encoding': 'utf-8',# 'fmt': None,# 'when': 'd',# 'interval': 1,# 'backup_count': 7,'debug':True}# 测试账户信息
TEST_NORMAL_USERNAME ='admin'
TEST_NORMAL_PASSWORD ='888888'

3、main.py

import pytest
import settings

if __name__ =='__main__':
    pytest.main(['-s','-v', settings.TEST_CASE_DIR])

4、common\log_handler.py

import logging
from logging.handlers import TimedRotatingFileHandler

defget_logger(name, filename, encoding='utf-8', fmt=None, when='d', interval=1, backup_count=7, debug=False):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)# 文件处理器的等级一般情况一定比控制台要高if debug:
        file_level = logging.DEBUG
        console_level = logging.DEBUG
    else:
        file_level = logging.WARNING
        console_level = logging.INFO

    if fmt isNone:
        fmt ='%(levelname)s %(asctime)s [%(filename)s-->line:%(lineno)d]:%(message)s'

    file_handler = TimedRotatingFileHandler(
        filename=filename, when=when, interval=interval, backupCount=backup_count, encoding=encoding)
    file_handler.setLevel(file_level)

    console_handler = logging.StreamHandler()
    console_handler.setLevel(console_level)

    formatter = logging.Formatter(fmt=fmt)
    file_handler.setFormatter(formatter)
    console_handler.setFormatter(formatter)

    logger.addHandler(file_handler)
    logger.addHandler(console_handler)return logger

if __name__ =='__main__':import settings
    log = get_logger(**settings.LOG_CONFIG)
    log.info('我是普通信息')
    log.warning('我是警告信息')

5、testcases\test_login.py

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import settings

classTestLogin:deftest_login(self):# 1. 打开浏览器with webdriver.Chrome()as driver:# 2. 访问登录页面
            driver.get(settings.INTERFACE['login'])# 3. 登录# 3.1 定位用户名输入框
            wait = WebDriverWait(driver, timeout=3)
            username_input = wait.until(EC.visibility_of_element_located(('xpath','//input[@name="phone"]')))# 3.2 输入用户名
            username_input.send_keys(settings.TEST_NORMAL_USERNAME)# 3.3 定位密码输入框# 3.4 输入密码# 3.5 定位登录按钮# 3.6 点击登录按钮# 4. 断言登录:根据右上角的退出按钮的出现assert wait.until(EC.visibility_of_element_located(('xpath','//a[text()="退出"]')))

本文转载自: https://blog.csdn.net/testleaf/article/details/123302863
版权归原作者 testleaf 所有, 如有侵权,请联系我们删除。

“selenium进阶之web自动化项目框架搭建(Python版)”的评论:

还没有评论