一、前言

最近问我自动化的人确实有点多,个人突发奇想:想从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
原创不易,欢迎转载,但未经作者同意请保留此段声明,并在文章页面明显位置给出原文链接。

最新文章

  1. JS中函数的调用和this的值
  2. TCP/IP详解系列 --- 概念总结01
  3. Latex常用指令学习
  4. selenium处理Ajax浮动框方法
  5. C++ 封装互斥对象
  6. zoj 3627 Treasure Hunt II (贪心)
  7. 建立Clojure开发环境-使用IDEA和Leiningen
  8. Instruction (hdu 5083)
  9. Struts+Tomcat搭建
  10. UI篇—UITableview
  11. JSP 入门
  12. 代理模式(Proxy)
  13. Docker常见故障
  14. redis发布/订阅
  15. django + 阿里云云服务器网站搭建
  16. BIM平台 http://gzcd.bim001.cn
  17. NOIP2017感想
  18. php 字符串固定长度,不够补充其他字符串
  19. BZOJ4460 : [Jsoi2013]广告计划
  20. Java之成员访问控制

热门文章

  1. SpringBoot源码分析(一)@SpringBootApplication解析
  2. Swift 4.0 字典(Dictionary)学习
  3. Journal of Proteome Research | Utilization of the Proteome Data Deposited in SRMAtlas for Validating the Existence of the Human Missing Proteins in GPM (解读人:梁嘉琪)
  4. (一)iview的校验TypeError: Cannot read property 'validateField' of undefined"
  5. 字符串-mask-每个元音包含偶数次的最长子字符串
  6. 面试刷题21:java并发工具中的队列有哪些?
  7. 《JavaScript 模式》读书笔记(5)— 对象创建模式2
  8. SpringMVC常见面试题总结(超详细回答)
  9. 粒子群优化算法(PSO)之基于离散化的特征选择(FS)(一)
  10. Linux下段错误(C语言)