selenium+python automation 89-send email when the use case fails

foreword

Implementation requirements: when all the test cases pass, no email is sent, and when an Error or Failure occurs in the use case, an email is sent

Solution: After generating the html test report, use bs4 to parse the html page, and write a function to judge whether there are any failed records on the page

html report

1. Check the html report, mainly to see if there is a record of Failure or Error in the Status line

bs4 parsing html

1. The bs4 module was introduced on my blog Shanghai- Yuyou before, so I won't repeat it. First locate the html page element, first locate the three lines of records through its class attribute: attribute

2. Take the last line and read out the text.

# coding:utf-8
from bs4 import BeautifulSoup

# 打开html文件,读取报告内容
with open("result.html", "r") as fp: f = fp.read() # 读报告 # 解析html,查找class属性attribute soup = BeautifulSoup(f, "html.parser") status = soup.find_all(class_="attribute") # 打印查找内容 print(status) print("qq交流群:226296743") result = status[2].contents[-1] # 获取报告结果 print(result)

operation result:

[<p class="attribute"><strong>Start Time:</strong> 2018-01-18 16:35:49</p>, <p class="attribute"><strong>Duration:</strong> 0:00:00</p>, <p class="attribute"><strong>Status:</strong> Pass 2 Failure 1 Error 1</p>] qq交流群:226296743 Pass 2 Failure 1 Error 1

write a judgment function

1. To judge the result, write a function to judge whether there is a failed use case

 # coding:utf-8
from bs4 import BeautifulSoup

import sys
reload(sys)
sys.setdefaultencoding('utf8') def is_result_pass(): try: with open("result.html", "r") as fp: f = fp.read() # 读报告 soup = BeautifulSoup(f, "html.parser") status = soup.find_all(class_="attribute") result = status[2].contents[-1] # 获取报告结果 if "Failure" in result or "Error" in result: print("测试过程有不通过用例:%s"%result) return False else: return True except Exception as msg: print("判断过程出现异常:%s"%str(msg)) return False if __name__ == "__main__": print("qq交流群:226296743") res = is_result_pass() print(res)

operation result:

测试过程有不通过用例: Pass 2 Failure 1 Error 1
False

2. Add a judgment before the final email

 if not is_result_pass():
        # 判断html报告是否有报错
        
        # 执行发送邮件函数,自己写一个发邮件函数
        # send_mail(sender, psw, receiver, smtp_server, report_file) else: print("测试用例全部通过,不发送邮件")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476111&siteId=291194637