打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Selenium2+python自动化72-logging日志使用

前言

脚本运行的时候,有时候不知道用例的执行情况,这时候可以加入日志,这样出现问题后方便查阅,也容易排查哪些用例执行了,哪些没有执行。

一、封装logging模块

1.关于logging日志的介绍,我这里就不详细讲解了,主要有两大功能,一个是控制台的输出,一个是保存到本地文件

2.先封装logging模块,保存到common文件夹命名为logger.py,以便于调用

# coding:utf-8
import logging,time,os
# 这个是日志保存本地的路径
log_path = "D:\\test\\newp\\report"
class Log:
    def __init__(self):
        # 文件的命名
        self.logname = os.path.join(log_path, '%s.log'%time.strftime('%Y_%m_%d'))
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)
        # 日志输出格式
        self.formatter = logging.Formatter('[%(asctime)s] - %(filename)s[line:%(lineno)d] - fuc:%(funcName)s- %(levelname)s: %(message)s')
    def __console(self, level, message):
        # 创建一个FileHandler,用于写到本地
        fh = logging.FileHandler(self.logname, 'a')  # 追加模式
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(self.formatter)
        self.logger.addHandler(fh)

        # 创建一个StreamHandler,用于输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(self.formatter)
        self.logger.addHandler(ch)

        if level == 'info':
            self.logger.info(message)
        elif level == 'debug':
            self.logger.debug(message)
        elif level == 'warning':
            self.logger.warning(message)
        elif level == 'error':
            self.logger.error(message)
        # 这两行代码是为了避免日志输出重复问题
        self.logger.removeHandler(ch)
        self.logger.removeHandler(fh)
        # 关闭打开的文件
        fh.close()

    def debug(self, message):
        self.__console('debug', message)

    def info(self, message):
        self.__console('info', message)

    def warning(self, message):
        self.__console('warning', message)

    def error(self, message):
        self.__console('error', message)

if __name__ == "__main__":
   log = Log()
   log.info("---测试开始----")
   log.info("输入密码")
   log.warning("----测试结束----")

二、log保存本地

1.logger模块的封装在9.2章节,我的用例参考目录如下

2.先设置保存log到本地的文件路径地址,如:log_path = "D:\\test\\newp\\report"

三、用例代码

以下是简单的一个百度的搜索案例仅供参考

# coding:utf-8

import unittest,time

from common.logger import Log

from selenium import webdriver

log = Log()

class Test(unittest.TestCase):

    def setUp(self):

        self.driver = webdriver.Firefox()

        self.driver.get("https://www.baidu.com")

        self.driver.implicitly_wait(30)

    def test_01(self):

        log.info("-------测试用例开始---------")

        self.driver.find_element_by_id("kw").send_keys("yoyo")

        log.info("输入内容:yoyo")

        self.driver.find_element_by_id("su").click()

        log.info("点击按钮:id = su")

        time.sleep(2)

        t = self.driver.title

        log.info(u"获取title内容:%s"%t)

        self.assertIn(u"百度搜索",t)

    def tearDown(self):

        self.driver.quit()

        log.info("-------测试用例结束----------")

if __name__ == "__main__":

    unittest.main()

四、运行结果:

1.执行run_all脚本(3.9章节)

2.打开存放日志文件的目录,找到log文件

3.打开报告,看到的效果如下

《selenium高级自动化》已出书,阅读全本可以购买此书(点左下角阅读原文)https://yuedu.baidu.com/ebook/0f6a093b7dd184254b35eefdc8d376eeaeaa17e3

(已购买的同学,可以凭流水单号进读书交流群获取一份对应PDF文档)

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python关键字UI自动化测试框架(3)-日志和断言封装
二次封装logging模块,做好日志记录
python日志包从头到尾讲清楚
python标准日志模块logging的使用方法
Python中的logging模块解析
应用程序Python的日志记录模板 | 区块链研究实验室
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服