Selenium+python Automation 89-sending mail when a use case does not pass

Source: Internet
Author: User

Objective

Implementation requirements: When the test case is all passed, do not send the message when the use case error or failure

Solution: After generating the HTML test report, parse the HTML page with BS4, write a function to determine that there are no records on the page

HTML report

1. View the HTML report, mainly see if the status of this line will appear failure or error record

BS4 Parsing HTML

1.BS4 module on my blog Shanghai-long before the introduction, do not repeat the writing, first locate the HTML page element, first through its class properties: attribute to the three rows of records

2. Remove the last line and then read the text content.

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

Operation Result:

[<Pclass="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 Exchange Group: 226296743 Pass 2 Failure 1 Error 1            

Write a judgment function

1. Judging the result, write a function that determines if there is a failure case

 # Coding:utf-8From BS4Import BeautifulSoupImport sysreload (SYS) sys.setdefaultencoding (' UTF8 ')DefIs_result_pass():TryWith open ("Result.html","R")As Fp:f = Fp.read ()# Read Report soup = BeautifulSoup (f,"Html.parser") status = Soup.find_all (class_="attribute") result = status[2].contents[-1]# get report results if  "Failure" in result or " Error " in result:print ( "test procedure has not passed use case:%s"%result) return false else: return true except Exception as msg:print (" Judging process exception:%s "%str (msg)) return Span class= "Hljs-keyword" >falseif __name__ =  "__main__": Print ( "QQ Exchange Group: 226296743") res = Is_result_pass () print (res)      

Operation Result:

2 Failure 1 Error 1False

2. Add a judgment before the last email

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

Selenium+python Automation 89-sending mail when a use case does not pass

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.