Python Selenium Unittest+htmltestrunner for automated testing and sending test report messages

Source: Internet
Author: User
Tags repetition

1. UI test Framework Setup-directory structure

2. Introduction of documents

2.1, baseinfo->__init__.py

configuration file Definition Base parameters

#-*-coding:utf-8-*-#test Case Configuration ParametersBase_url="http://xxxxx.com"Undirect_username="username"Undirect_password="Password"Direct_username="username"Direct_password="Password"Buyername='username'Buyerpassword='Password'Sellername='username'Sellerpassword='Password'#Send mail configuration ParametersSmtp_server ='smtp.xxxx.com'Smtp_sender='[email protected]'Smtp_sender_password='Password'#smtp_receiver = [' [email protected] ', ' [email protected] ', ' [email protected] ', ' [email protected] 'Smtp_receiver = ['[email protected]']smtp_receiver_pre= ['[email protected]']#Test Mailbox Configuration#smtp_server = ' smtp.163.com '#smtp_sender = ' [email protected] '#Smtp_sender_password = ' Password '#smtp_receiver = [' [email protected] ']#smtp_receiver_pre = [' [email protected] ']#test Case and report path configuration parameters#case_dir = './test_case '#test_report = './test_report/'#test Case and report path configuration parametersTest_dir ='D:\\workspace\\python\\uitest\\test_case'Test_report='d:\\workspace\\python\\uitest\\test_report\\'

2.2, Casedata

For storing test data, this is used to store uploaded files in the file upload function.

2.3, Test_case

Used to store test cases, according to the defined pattern values, so the use case must start with test

2.4, Test_report

User holds test report file

The report reads as follows

2.5. Test execution procedure

Write the use case execution program runtest.py

#-*-coding:utf-8-*-#time:2017/7/1-0:18#Author:yangyangjunImportsysreload (SYS) sys.setdefaultencoding ('UTF8')ImportSmtplibImportUnitTestImport TimeImportOS fromHtmltestrunnerImportHtmltestrunner fromEmail.headerImportHeader fromEmail.mime.textImportMimetext
ImportBaseinfo fromEmail.mime.multipartImportMimemultipart fromSeleniumImportWebdriverdefGet_newreport (testreport):#get the file in the Testreport directory to return a listDirs =Os.listdir (Testreport)#sort the list of files and order them in ascending orderDirs.sort ()#gets the last element of the sequence, which is the largest one. Newreportname = dirs[-1] Print('The new report name: {0}'. Format (newreportname)) File_new=Os.path.join (Testreport, Newreportname)Printfile_newreturnfile_newdefget_result (filename): Driver=Webdriver. Firefox () Driver.maximize_window ()##得到测试报告路径Result_url ="file://%s"%filename driver.get (result_url) time.sleep (5) Result= Driver.find_element_by_xpath ("Html/body/div[1]/p[3]"). Text result= Result.split (':') Printresult Driver.quit ()returnResult[-1]defSend_mail (File_new,result): F= Open (File_new,'RB') #reading the test report bodyMail_body =F.read () f.close ( )Try: SMTP= Smtplib. SMTP (Baseinfo. Smtp_server, 25) Sender=Baseinfo. Smtp_sender Password=Baseinfo. Smtp_sender_password receiver=Baseinfo. Smtp_receiver smtp.login (sender, password) msg=Mimemultipart ()#Write an HTML-type message body, Mimetext () to define the message body #Send BodyText = Mimetext (Mail_body,'HTML','Utf-8') #defining message body headingstext['Subject'] = Header ('xxxxui Automated test report','Utf-8') Msg.attach (text)#Send Attachments #header () is used to define the subject of the message, subject to the time, to prevent repetition of the theme, subject repetition, sent too frequently, the message will not be sent out. msg['Subject'] = Header ('[Execution Result:'+ result +']'+'xxxxui Automated test report'+ Now,'Utf-8') Msg_file= Mimetext (Mail_body,'HTML','Utf-8') msg_file['Content-type'] ='Application/octet-stream'msg_file["content-disposition"] ='attachment; filename= "testreport.html"'Msg.attach (msg_file)#defines the sender, if not written, the sender is emptymsg[' from'] =Sender#define recipient, if not write, the recipient is emptymsg[' to'] =",". Join (receiver) TMP=Smtp.sendmail (sender, receiver, msg.as_string ())Printtmp smtp.quit ()returnTrueexceptSmtplib. Smtpexception as E:Print(str (e))returnFalseif __name__=='__main__': #Test_dir = Os.path.join (OS.GETCWD (), ' test_case ') #print (Test_dir) #Report_dir = Os.path.join (OS.GETCWD (), ' Test_report ') #Test Case PathTest_dir =Baseinfo.test_dir#Est_dir = Os.path.join (OS.GETCWD (), ' test_case ') #print (Test_dir) #Report_dir = Os.path.join (OS.GETCWD (), ' Test_report ') #test Report Storage PathReport_dir =Baseinfo.test_report Test_discover= Unittest.defaultTestLoader.discover (Test_dir, pattern='test*.py') Now= Time.strftime ("%y-%m-%d-%h_%m_%s") filename= Report_dir +'result-'+ Now +'. html' Printfilename FP= open (filename,'WB') Runner= Htmltestrunner (STREAM=FP, title='xxxxui Automated test report', description='use case Execution') Runner.run (test_discover) fp.close ()#In fact, there is no need to get the latest report, filename is the latest report, you can directly use the #New_report = Get_newreport (report_dir)result =get_result (filename) Mail=Send_mail (Filename,result)PrintMailifMail:Print(U"message sent successfully! ") Else: Print(U"message failed to send! ")

Python Selenium Unittest+htmltestrunner for automated testing and sending test report messages

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.