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文章来源:https://www.toymoban.com/news/detail-677146.html
import logging
from logging.handlers import TimedRotatingFileHandler
def 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.DEBUG
console_level = logging.DEBUG
else:
file_level = logging.WARNING
console_level = logging.INFO
if 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 logger
if __name__ == '__main__':
import settings
log = get_logger(**settings.LOG_CONFIG)
log.info('我是普通信息')
log.warning('我是警告信息')
5、testcases\test_login.py文章来源地址https://www.toymoban.com/news/detail-677146.html
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
class 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()="退出"]')))
到了这里,关于selenium进阶之web自动化项目框架搭建(Python版)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!