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 pytestimport settingsif __name__ == '__main__':pytest.main(['-s', '-v', settings.TEST_CASE_DIR])

4、common\log_handler.py

import loggingfrom logging.handlers import TimedRotatingFileHandlerdef get_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.DEBUGconsole_level = logging.DEBUGelse:file_level = logging.WARNINGconsole_level = logging.INFOif fmt is None: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 loggerif __name__ == '__main__':import settingslog = get_logger(**settings.LOG_CONFIG)log.info('我是普通信息')log.warning('我是警告信息')

5、testcases\test_login.py

from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import Byimport settingsclass TestLogin:def test_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()="退出"]')))