首页 > 其他 > 详细

selenium--记录日志信息

时间:2020-03-14 11:20:00      阅读:97      评论:0      收藏:0      [点我收藏+]

前戏

在我们进行web自动化的时候,我们希望记录下日志信息,方便我们进行定位分析,我们可以使用logging模块来进行记录

实战

先写个配置文件Logger.conf来管理日志的配置

[loggers]
keys=root,example01,example02
[logger_root]
level=DEBUG
handlers=hand01,hand02
[logger_example01]
handlers=hand01,hand02
qualname=example01
propagate=0
[logger_example02]
handlers=hand01,hand03
qualname=example02
propagate=0
#######################################
[handlers]
keys=hand01,hand02,hand03
[handler_hand01]
class=StreamHandler
level=DEBUG
formatter=form01
args=(sys.stderr,)
[handler_hand02]
class=FileHandler
level=DEBUG
formatter=form01
args=(d:\\AutoTestLog.log,a)
[handler_hand03]
class=handlers.RotatingFileHandler
level=INFO
formatter=form01
args=(d:\\AutoTestLog.log,a,10*1024*1024,5)
#####################################################
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
detefmt=%Y-%m-%d %H:%M:%S
[formatter_form02]
format=%(name)-12s: %(levelname)-8s %(message)s
detefmt=%Y-%m-%d %H:%M:%S

在写个Log.py文件来管理日志的级别

import logging.config

logging.config.fileConfig("Logger.conf")


def debug(message):
    # 打印debug级别的日志方法
    logging.debug(message)


def warning(message):
    # 打印warning级别的日志方法
    logging.warning(message)


def info(message):
    # 打印info级别的日志方法
    logging.info(message)

最后再我们的脚本里写日志信息

from selenium import webdriver
import unittest
from Log import *

class TestSoGouSearch(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Chrome()

    def testSoGouSearch(self):
        info(u=======搜索=======)
        url=http://www.sogou.com
        self.driver.get(url)
        info(访问sogou首页)
        import time

        time.sleep(3)
        self.driver.find_element_by_id(query).send_keys(自动化测试)
        info(在搜索框中输入自动化测试)
        self.driver.find_element_by_id(stb).click()
        info(单击搜索按钮)
        info(=====测试用例执行结束=====)
    def tearDown(self):
        self.driver.quit()

if __name__==__main__:
    unittest.main()

执行SoGou.py文件后,会在本地磁盘D盘中生成一个日志文件AutoTestLog.log

selenium--记录日志信息

原文:https://www.cnblogs.com/zouzou-busy/p/11285789.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!