zoukankan      html  css  js  c++  java
  • python封装logging日志模块,与selenium连用

    推荐一篇博主博客:https://www.cnblogs.com/CJOKER/p/8295272.html

    它里面讲述了日志的显示顺序、如何在控制台进行显示,以及文件配置都挺详细的,可以先看看推荐的内容  我这边按项目的情况对使用的日志模块进行了一个封装

    1.日志文件的配置 Logger.conf

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    ################################################
    ###########propagate 是否继承父类的log信息,0:否
    [loggers]
    keys=root,example01,example02   #3个格式的日志
    [logger_root]
    level=DEBUG     #打印日志级别,从高到低
    handlers=hand01,hand02
    [logger_example01]
    handlers=hand01,hand02 #用哪个日志处理程序来打日志(怎么打日志:1)打在屏幕  2 打在文件里 3)打在可以回滚的文件里)
    qualname=example01
    propagate=0
    [logger_example02]
    handlers=hand01,hand03
    qualname=example02
    propagate=0
    ###############################################
    [handlers]
    keys=hand01,hand02,hand03
    [handler_hand01]
    class=StreamHandler  #用StreamHandler(流模式)打印
    level=DEBUG   #打印模式
    formatter=form01    #打印格式
    args=(sys.stderr,)
    [handler_hand02]
    class=FileHandler  #文件模式
    level=DEBUG
    formatter=form01
    args=('e:\AutoTestLog.log''a')  #存储的位置
    [handler_hand03]     #回滚日志
    class=handlers.RotatingFileHandler
    level=INFO
    formatter=form01
    args=('e:\AutoTestLog.log''a'10*1024*10245)
    ###############################################
    [formatters]
    keys=form01,form02
    [formatter_form01]
    format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
    datefmt=%Y-%m-%%H:%M:%S
    [formatter_form02]
    format=%(name)-12s%(levelname)-8s %(message)s
    datefmt=%Y-%m-%%H:%M:%S
    # asctime:时间  filename:打印日志的文件名  lineno:行号  levelname:日志级别 message:具体你打印的日志消息#注 上面的文字是我做的注释,需要运行时需要去除他们 

    二:引用封装的日志文件Log.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    #encoding=utf-8
    import logging.config
    import logging
     
    logging.config.fileConfig("Logger.conf")
    logger = logging.getLogger("example01")
     
    #日志配置文件:多个logger,每个logger,指定不同的handler
    #handler:设定了日志输出行的格式
    #handler:以及设定写日志到文件(是否回滚)?还是到屏幕
    #handler:还定了打印日志的级别。
     
    def debug(message):
        # 打印debug级别的日志方法
       logger.debug(message)
     
    def warning(message):
        # 打印warning级别的日志方法
        logger.warning(message)
     
    def info(message):
        # 打印info级别的日志方法
        logger.info(message)
     
    def error(message):
        # 打印error级别的日志方法
        logger.error(message)
    if __name__=="__main__":
        debug("hi")
        info("info hi ")
        warning("warning hello")
        error("error hello")

    3.selenium中使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    #encoding=utf-8
    from selenium import webdriver
    import unittest
    import logging
     
    # 从当前文件所在目录中导入Log.py文件中所有内容
    from logdemo.Log import *
     
    class TestSoGouByObjectMap(unittest.TestCase):
     
        def setUp(self):
            # 启动浏览器
            self.driver = webdriver.Chrome(executable_path="D:\driver\chromedriver")
     
        def testSoGouSearch(self):
            debug(u"============== 搜索 ==============")
            url = "http://www.baidu.com"
     
            self.driver.get(url)
            debug(u"访问sogou首页")
            self.driver.find_element_by_id("kw").send_keys(u"肺炎 ")
            warning(u"在输入框中输入搜索关键字串“肺炎”"+self.driver.find_element_by_id("kw").get_attribute("value"))
            self.driver.find_element_by_id("su").click()
            info(u"点击搜索按钮")
            debug(u"========== 测试用例执行结束 ==========")
     
        def tearDown(self):
            # 退出IE浏览器
            self.driver.quit()
     
    if __name__ == '__main__':
        unittest.main()

     

     

  • 相关阅读:
    axis
    LRU
    apk 反编译
    android 设置 button 不同状态的图片
    resin
    scrum 项目管理
    android 国外广告平台
    JNI
    java 通信
    google网站分析
  • 原文地址:https://www.cnblogs.com/fighter007/p/13720315.html
Copyright © 2011-2022 走看看