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:

[<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. Judging the result, write a function that determines if there is a failure case

 # coding:utf-8from bs4 import BeautifulSoupimport sysreload(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 Falseif __name__ == "__main__":    print("qq交流群:226296743")    print(is_result_pass())

Operation Result:

测试过程有不通过用例: Pass 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.