100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 【Selenium06篇】python+selenium实现Web自动化:日志处理

【Selenium06篇】python+selenium实现Web自动化:日志处理

时间:2023-06-17 08:06:14

相关推荐

【Selenium06篇】python+selenium实现Web自动化:日志处理

一、前言

最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新!

这是python+selenium实现Web自动化第六篇博文

二、Selenium成套博文地址,总有你需要的:

【Selenium篇01】python+selenium实现Web自动化:搭建环境,Selenium原理,定位元素以及浏览器常规操作!

/showweb/0/0/906255588.aspx

【Selenium02篇】python+selenium实现Web自动化:鼠标操作和键盘操作!

/showweb/0/0/906258176.aspx

【Selenium03篇】python+selenium实现Web自动化:元素三类等待,多窗口切换,警告框处理,下拉框选择

/showweb/0/0/906262739.aspx

【Selenium04篇】python+selenium实现Web自动化:文件上传,Cookie操作,调用 JavaScript,窗口截图

/content/20/0415/19/69336923_906263765.shtml

【Selenium05篇】python+selenium实现Web自动化:读取ini配置文件,元素封装,代码封装,异常处理,兼容多浏览器执行

/showweb/0/0/906618131.aspx

【Selenium06篇】python+selenium实现Web自动化:日志处理

/showweb/0/0/906618633.aspx

【Selenium07篇】python+selenium实现Web自动化:PO模型,PageObject模式!

/showweb/0/0/906619184.aspx

【Selenium08篇】python+selenium实现Web自动化:数据驱动框架,ddt,关键字驱动

/showweb/0/0/906619813.aspx

三、Selenium之-日志处理

到这里已经搞了好多,但是在排查问题的时候,不是很方便,我们需要对程序的执行中错误的地方进行记录。

1.在 console 输出log

可以将日志信息输出的console中,但是这种方式不常用。日常更多使用的是2的方法,将日志信息输出到log文件中。

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

"""

@Time : /4/17

@Author : 公众号:软测之家 更多技术干货,软测视频,面试资料请关注!

@Contact : 软件测试技术群:695458161

@License : (C)Copyright -, Micro-Circle

@Desc : None

"""

import logging

class RecordLog(object):

def __init__(self):

self.logger = logging.getLogger()

self.logger.setLevel(logging.DEBUG)

# 1. 在 console 中输出日志文件

# 能够将日志信息输出到sys.stdout, sys.stderr 或者类文件对象

# 日志信息会输出到指定的stream中,如果stream为空则默认输出到sys.stderr。

console = logging.StreamHandler(stream=None)

# 将sys.stderr中的信息添加到logger中

self.logger.addHandler(console)

# 输出调试信息

self.logger.debug("这是一条在控制台线上的log")

# 关闭流

console.close()

# 移除

self.logger.removeHandler(console)

if __name__ == "__main__":

rl = RecordLog()

2.输出日志到log文件

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

"""

@Time : /4/17

@Author : 公众号:软测之家 更多技术干货,软测视频,面试资料请关注!

@Contact : 软件测试技术群:695458161

@License : (C)Copyright -, Micro-Circle

@Desc : None

"""

import logging

import os

from datetime import datetime

class RecordLog(object):

def __init__(self):

self.logger = logging.getLogger()

self.logger.setLevel(logging.DEBUG)

# 2.将log信息输出到log文件中

# 2.1 先定位看将log文件输出到哪里去

current_dir = os.path.dirname(os.path.abspath(__file__))

print(current_dir) # D:\MySpace\Python\WebTesting\util

log_dir = os.path.join('../logs')

# 日志名称构建

log_file_name = datetime.now().strftime("%Y-%m-%d") + '.log'

log_file_path = log_dir + '/' + log_file_name

print(log_file_path)

# 2.2 好的,将日志写进log文件中

self.file_handle = logging.FileHandler(log_file_path, 'a', encoding='utf-8')

formatter = logging.Formatter(

'%(asctime)s %(filename)s %(funcName)s %(levelno)s: [%(levelname)s] ---> %(message)s')

self.file_handle.setFormatter(formatter)

self.logger.addHandler(self.file_handle)

def get_log(self):

return self.logger

def close_handle(self):

self.logger.removeHandler(self.file_handle)

self.file_handle.close()

if __name__ == "__main__":

rl = RecordLog()

log_info = rl.get_log()

log_info.debug('输出到文件中去')

rl.close_handle()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。