项目中如何添加log日志-登录功能
听添加日志的必要性下面这张图是没添加log的报错信息,控制台底部很多内容,杂而乱,不知道具体哪一行报差错了。
http://www.51testing.com/attachments/2021/04/15326825_2021042516002912wxs.png
所以,给代码中添加日志,可以帮助我们快速的定位问题。
添加日志步骤:
1.封装日志工具类
2.调用日志工具包,在想添加日志的代码处调用
实现过程:
·封装日志工具类
·日志的四大组件
http://www.51testing.com/attachments/2021/04/15326825_202104251600292lK59.png
封装日志工具
1.util.py文件中封装get_logger方法,实现日志记录功能的封装,包括级别,日志格式,发送位置等。
def get_logger():
import logging
import logging.handlers
import datetime
logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
rf_handler = logging.handlers.TimedRotatingFileHandler('all.log', when='midnight', interval=1, backupCount=7,
atTime=datetime.time(0, 0, 0, 0))
rf_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
f_handler = logging.FileHandler('error.log')
f_handler.setLevel(logging.ERROR)
f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s"))
logger.addHandler(rf_handler)
logger.addHandler(f_handler)
return logger
2.调用调试一下,确认日志封装成功,新建test.py文件,运行如下脚本:
from util import util
logger=util.get_logger()
logger.info('123')
3.在log文件夹下生成日志文件:
http://www.51testing.com/attachments/2021/04/15326825_202104251600293Ma5A.png
实际应用-登录功能
1.导入工具类
from util import util
2.初始化函数实例化获取日志函数
self.logger = util.get_logger()
self.logger.info('用户登录用例')
3.在需要添加日志信息处添加如下代码
self.logger.debug('输入用户名称:%s ',username)
4.断言处添加日志
源码:
assert expect == self.driver.title
修改后:
try:
assert expect == self.driver.title
except AssertionError as ae:
self.logger.error("验证错误:%s", "报错了", exc_info=1)
下面以admin账号登录功能为例:
源代码:
import time
import pytest
from selenium import webdriver #将webdriver驱动导入selenium框架中
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from util.util import *
class Test_admin_login(object):
def setup_class(self):
options = webdriver.ChromeOptions()
# 添加无界面参数
options.add_argument('--headless')
self.driver=webdriver.Chrome(options=options)
self.driver.get('http://localhost:8888/jpress/admin/login')
self.driver.maximize_window()
login_data=[('wx','123456','666','验证码不正确,请重新输入'),
@pytest.mark.dependency(name='admin_login')
@pytest.mark.parametrize('username,pwd,captcha,expect',login_data)
def test1(self,username,pwd,captcha,expect):
#测试数据
#正确用户名
self.driver.find_element_by_name('user').clear()
self.driver.find_element_by_name('user').send_keys(username)
# 正确密码
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
if captcha!='666':
# 正确的验证码
js = "document.getElementsByTagName('img').setAttribute('id','captchaimg')"
self.driver.execute_script(js)
captcha = get_code(self.driver, id='captchaimg', uname='luoditao', pwd='xinsheng2', )
self.driver.find_element_by_name('captcha').clear()
self.driver.find_element_by_name('captcha').send_keys(captcha)
# 登录按钮
self.driver.find_element_by_class_name('btn-flat').click()
WebDriverWait(self.driver, 5).until(EC.title_is(expect))
assert expect == self.driver.title
time.sleep(3)
else:
#错误的验证码
self.driver.find_element_by_name('captcha').clear()
self.driver.find_element_by_name('captcha').send_keys(captcha)
#登录按钮
self.driver.find_element_by_class_name('btn-flat').click()
WebDriverWait(self.driver,20).until(EC.alert_is_present())
alert=self.driver.switch_to.alert
assert alert.text==expect
alert.accept()
time.sleep(2)
if __name__ == '__main__':
pytest.main(['test_admin_login.py'])
添加日志后代码:
import time
import pytest
from selenium import webdriver #将webdriver驱动导入selenium框架中
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from util import util
from util.util import get_code
class Test_admin_login(object):
def setup_class(self):
options = webdriver.ChromeOptions()
# 添加无界面参数
options.add_argument('--headless')
self.driver=webdriver.Chrome(options=options)
self.driver.get('http://localhost:8888/jpress/admin/login')
self.driver.maximize_window()
self.logger = util.get_logger()
self.logger.info('测试admin登录')
login_data=[('wx','123456','666','验证码不正确,请重新输入'),
@pytest.mark.dependency(name='admin_login')
@pytest.mark.parametrize('username,pwd,captcha,expect',login_data)
def test1(self,username,pwd,captcha,expect):
#测试数据
#正确用户名
self.driver.find_element_by_name('user').clear()
self.driver.find_element_by_name('user').send_keys(username)
self.logger.debug('输入用户名称:%s ',username)
# 正确密码
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.logger.debug('输入密码:%s ', pwd)
if captcha!='666':
# 正确的验证码
js = "document.getElementsByTagName('img').setAttribute('id','captchaimg')"
self.driver.execute_script(js)
captcha = get_code(self.driver, id='captchaimg', uname='luoditao', pwd='xinsheng2', )
self.driver.find_element_by_name('captcha').clear()
self.driver.find_element_by_name('captcha').send_keys(captcha)
self.logger.debug('输入验证码:%s ', captcha)
# 登录按钮
self.driver.find_element_by_class_name('btn-flat').click()
self.logger.debug('点击登录按钮')
WebDriverWait(self.driver, 5).until(EC.title_is(expect))
# 验证
try:
assert expect == self.driver.title
except AssertionError as ae:
self.logger.error("验证错误:%s", "报错了", exc_info=1)
time.sleep(3)
else:
#错误的验证码
self.driver.find_element_by_name('captcha').clear()
self.driver.find_element_by_name('captcha').send_keys(captcha)
self.logger.debug('输入验证码:%s ', captcha)
#登录按钮
self.driver.find_element_by_class_name('btn-flat').click()
self.logger.debug('点击登录按钮')
WebDriverWait(self.driver,20).until(EC.alert_is_present())
alert=self.driver.switch_to.alert
try:
assertalert.text==expect
except AssertionError as ae:
self.logger.error("验证错误:%s", "报错了", exc_info=1)
alert.accept()
time.sleep(2)
if __name__ == '__main__':
pytest.main(['test_admin_login.py'])
页:
[1]