Selenium+python Automation 88-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

First, the HTML report

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

Second, BS4 parsing html

1.BS4 module in my blog [Shanghai-Yo] (http://www.cnblogs.com/yoyoketang/tag/beautifulsoup4/) before the introduction, do not repeat, first locate the HTML page elements, First through its class attribute: attribute to these three rows of records

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

```

1 #Coding:utf-82 3  fromBs4ImportBeautifulSoup4 5  6 7 #open HTML file to read report contents8 9With open ("result.html","R") as FP:Ten  Onef = Fp.read ()#Read the report A  -   -  the #parsing HTML, finding the class attribute attribute -  -Soup = BeautifulSoup (f,"Html.parser") -  +Status = Soup.find_all (class_="attribute") -  +   A  at #Print Find Content -  - Print(status) -  -   -  in Print("QQ Exchange Group: 226296743") -  toresult = Status[2].contents[-1]#Get report Results +  - Print(Result) the  *```

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>]

Pass 2 Failure 1 Error 1

```

Three, write a judgment function

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

1 ```2 3  #Coding:utf-84 5  fromBs4ImportBeautifulSoup6 7  8 9 ImportSYSTen  One Reload (SYS) A  -Sys.setdefaultencoding ('UTF8') -  the   -  - defIs_result_pass (): -  +     Try: -  +With open ("result.html","R") as FP: A  atf = Fp.read ()#Read the report -  -Soup = BeautifulSoup (f,"Html.parser") -  -Status = Soup.find_all (class_="attribute") -  inresult = Status[2].contents[-1]#Get report Results -  to         if "Failure" inchResultor "Error" inchResult: +  -             Print("The test process has not passed the use case:%s"%result) the  *             returnFalse $ Panax Notoginseng         Else: -  the             returnTrue +  A     exceptException as msg: the  +         Print("error in judging process:%s"%str (msg)) -  $         returnFalse $  -   -  the if __name__=="__main__": - Wuyi     Print("QQ Exchange Group: 226296743") the  -     Print(Is_result_pass ()) Wu  -```

Operation Result:

```

The test process has not passed the use case: Pass 2 Failure 1 Error 1

False

```

2. Add a judgment before the last email

```

1  if  notIs_result_pass ():2 3         #determine if the HTML report has an error4 5         6 7         #execute the Send mail function, write your own e-mail function8 9         #send_mail (sender, PSW, receiver, Smtp_server, Report_file)Ten  One          A  -     Else: -  the         Print("test cases are all passed and no messages are sent")

Selenium+python Automation 88-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.