DEBUG
INFO
WARNING
ERROR
>> import logging
>> logging.error("Error Message!")
ERROR:root:Error Message!
>> logging.info("Info Message!") # 默认只有等级大于INFO的才会显示。
>> logger = logging.getLogger("logger") # 设定Info级别的log就显示
>> logger.setLevel("INFO")
>> logger.info("Info Message!")
INFO:logger:Info Message!
project > log
project > data > data.yaml
project > page > page_baidu.py
project > PO > test_baidu.py
project > PO > conftest.py
project > utils > get_filepath.py
project > utils > log_util.py
project > utils > read.py
# project > data > data.yaml
skill:
- 接口自动化
- UI自动化
- 性能测试
# project > page > page_baidu.py
from selenium.webdriver.common.by import By
from base.base_page import BasePage
from utils.log_util import logger
class PageBaidu(BasePage):
# 新闻
news = (By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]')
# 百度一下按钮
button = (By.ID, 'su')
# 百度输入框
input = (By.ID, 'kw')
def search_keyword(self, keyword): # 把操作封装到方法里面去。
logger.info("查找元素并输入内容")
self.driver.find_element(*self.input).send_keys(keyword)
logger.info("点击按钮")
self.driver.find_element(*self.button).click()
# project > PO > test_baidu.py
from time import sleep
import pytest
from page.page_baidu import PageBaidu
from utils.read import read_yaml
class TestBaidu:
@pytest.mark.parametrize("key",read_yaml()['skill'])
def test_baidu1(self,driver,key):
page = PageBaidu(driver)
page.search_keyword(key)
sleep(2)
def test_baidu2(self,driver):
page = PageBaidu(driver)
page.search_keyword("UI自动化")
sleep(2)
def test_baidu3(self,driver):
page = PageBaidu(driver)
driver.find_element(*page.input).send_keys("RDMA")
driver.find_element(*page.button).click()
sleep(2)
# project > PO > conftest.py
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope="session")
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
print("打开浏览器")
yield driver
print("关闭浏览器")
driver.close()
driver.quit()
@pytest.fixture()
def fixture():
print("我是前置步骤")
yield "老白"
print("我是后置步骤")
# project > utils > get_filepath.py
import os
import time
def get_report_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "allure-report/export",
'prometheusData.txt')
return path
def get_screen_shot_path():
file_name = "截图{}.png".format(time.strftime("%Y-%m-%d_%H-%M-%S"))
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", file_name)
return path
def get_logo_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", "logo.jpg")
return path
def download_file_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file")
return path
def get_yaml_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "data.yaml")
return path
def get_ini_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "settings.ini")
return path
def get_log_path():
path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "log")
return path
if __name__ == '__main__':
print(get_report_path())
# project > utils > log_util.py
import logging
import os
import time
from utils.get_filepath import get_log_path
log_path = get_log_path()
if not os.path.exists(log_path):
os.mkdir(log_path)
class Logger:
def __init__(self):
# 定义日志位置和文件名
self.logname = os.path.join(log_path, "{}.log".format(time.strftime("%Y-%m-%d")))
# 定义一个日志容器
self.logger = logging.getLogger("log")
# 设置日志打印的级别,大于DEBUG都可以被打印出来
self.logger.setLevel(logging.DEBUG)
# 创建日志输入的格式
self.formater = logging.Formatter(
'[%(asctime)s][%(filename)s %(lineno)d][%(levelname)s]: %(message)s')
# 创建日志处理器,用来存放日志文件
self.filelogger = logging.FileHandler(self.logname, mode='a', encoding="UTF-8")
# 文件存放日志级别
self.filelogger.setLevel(logging.DEBUG)
# 文件存放日志格式
self.filelogger.setFormatter(self.formater)
# 创建日志处理器,在控制台打印
self.console = logging.StreamHandler()
# 设置控制台打印日志界别
self.console.setLevel(logging.DEBUG)
# 控制台打印日志格式
self.console.setFormatter(self.formater)
# 将日志输出渠道添加到日志收集器中
self.logger.addHandler(self.filelogger)
self.logger.addHandler(self.console)
logger = Logger().logger
if __name__ == '__main__':
logger.debug("我打印DEBUG日志")
logger.info("我打印INFO日志")
logger.warning("我打印WARNING日志")
logger.error("我打印ERROR日志")
# project > utils > read.py
import configparser
import yaml
from utils.get_file_path import get_yaml_path, get_ini_path
path = get_yaml_path()
ini_path = get_ini_path()
def read_yaml():
with open(path, encoding="utf8") as f:
data = yaml.safe_load(f)
return data
def read_ini():
config = configparser.ConfigParser()
config.read(ini_path, encoding='utf8')
return config
if __name__ == '__main__':
print(read_yaml())
# print(read_ini()['mysql']['HOST'])
# 执行记录 project > log > xxx.log
[2024-03-24 13:56:16,392][log_util.py 45][DEBUG]: 我打印DEBUG日志
[2024-03-24 13:56:16,393][log_util.py 46][INFO]: 我打印INFO日志
[2024-03-24 13:56:16,393][log_util.py 47][WARNING]: 我打印WARNING日志
[2024-03-24 13:56:16,393][log_util.py 48][ERROR]: 我打印ERROR日志
[2024-03-24 14:33:00,058][page_baidu.py 16][INFO]: 查找元素并输入内容
[2024-03-24 14:33:00,544][page_baidu.py 18][INFO]: 点击按钮
[2024-03-24 14:33:03,176][page_baidu.py 16][INFO]: 查找元素并输入内容
[2024-03-24 14:33:03,332][page_baidu.py 18][INFO]: 点击按钮
[2024-03-24 14:33:05,647][page_baidu.py 16][INFO]: 查找元素并输入内容
[2024-03-24 14:33:05,798][page_baidu.py 18][INFO]: 点击按钮
[2024-03-24 14:33:08,237][page_baidu.py 16][INFO]: 查找元素并输入内容
[2024-03-24 14:33:08,549][page_baidu.py 18][INFO]: 点击按钮
网友评论