一、前言

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

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

二、Selenium前五篇博文地址:

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

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

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

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

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

三、Selenium之-日志处理

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

1.在 console 输出log

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

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Time : 2020/4/17
@Author : 公众号:软测之家 更多技术干货,软测视频,面试资料请关注!
@Contact : 软件测试技术群:695458161
@License : (C)Copyright 2017-2019, 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 : 2020/4/17
@Author : 公众号:软测之家 更多技术干货,软测视频,面试资料请关注!
@Contact : 软件测试技术群:695458161
@License : (C)Copyright 2017-2019, 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()

四、持续更新中请关注

如果你对此文有任何疑问,如果你觉得此文对你有帮助,如果你对软件测试、接口测试、自动化测试、面试经验交流感兴趣欢迎加入:

软件测试技术群:695458161,群里发放的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

作者:来自公众号:软测之家
出处:https://www.cnblogs.com/csmashang/p/12719079.html
原创不易,欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。

【Selenium06篇】python+selenium实现Web自动化:日志处理的更多相关文章

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

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 二.话不多说,直接开干,开始搭建自动化测试环境 这里以前在 ...

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

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

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

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

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

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

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

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

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

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

  7. 【python+selenium的web自动化】- 元素的常用操作详解(一)

    如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html ​ 本篇主要内容:1.元素 ...

  8. 【python+selenium的web自动化】- PageObject模式解析及案例

    如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html PO模式 ​ Page O ...

  9. 【python+selenium的web自动化】- Selenium WebDriver原理及安装

    简单介绍 selenium ​ selenium是一个用于测试web网页的自动化测试工具,它直接运行在浏览器中,模拟用户的操作.

随机推荐

  1. WPF 启动缓慢问题

    Actually there's 2 main reasons that the default project type for WPF applications is x86. Intellitr ...

  2. 视觉目标跟踪算法——SRDCF算法解读

    首先看下MD大神2015年ICCV论文:Martin Danelljan, Gustav Häger, Fahad Khan, Michael Felsberg. "Learning Spa ...

  3. Journal of Proteome Research | 人类牙槽骨蛋白的蛋白质组学和n端分析:改进的蛋白质提取方法和LysargiNase消化策略增加了蛋白质组的覆盖率和缺失蛋白的识别 | (解读人:卜繁宇)

    文献名:Proteomic and N-Terminomic TAILS Analyses of Human Alveolar Bone Proteins: Improved Protein Extr ...

  4. Spring Boot + LayUI 批量修改数据 数据包含着对象

    页面展示 HTML 代码 <blockquote class="layui-elem-quote demoTable"> <div class="lay ...

  5. Django HttpResponse

    HttpResponse 概述:给浏览器返回数据 HttpRequest对象是由django创建的,HttpResponse对象由程序员创建 用法 1:不调用模板,直接返回数据. 例: def get ...

  6. command > /dev/null command > /dev/null 2>&1nohup command &> /dev/null的区别

    1.对以下命令进行依次区分 command 执行一条普通的命令 command > /dev/null   '>'表示将标准输出重定向 '>>'表示追加,/dev/null是一 ...

  7. 记录一些服务端术语和搭建web服务器

    菜单快捷导航 服务端常用术语 搭建web服务器和配置虚拟主机 记录一些服务端方面的常用术语 1.CS架构和BS架构 1.1 CS架构 CS(Client/Server),基于安装包类型的桌面或手机软件 ...

  8. 高性能RabbitMQ

    1,什么是RabbitMq RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开 ...

  9. 在Ngnix中配置支持Websocket

    使用SignalR实现Websocket实时数据传输时,前后端各自实现编码后,无法将Websocket调试通过.沮丧之时,负责配置网络代理的同事说,网络访问这块使用了Ngnix代理设置,可能是造成We ...

  10. 学习笔记-EL

    仅作为学习过程中笔记作用,若有不正确的地方欢迎指正 目标 理解El的作用,熟练使用EL EL表达式与Jsp表达式对比来记 EL表达式的概念,作用,语法 Jsp作用主要是用来实现动态网页的,而动态网页中 ...