selenium--記錄日志信息


前戲

在我們進行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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM