歡迎關注【無量測試之道】公眾號,回復【領取資源】,
Python編程學習資源干貨、
Python+Appium框架APP的UI自動化、
Python+Selenium框架Web的UI自動化、
Python+Unittest框架API自動化、
資源和代碼 免費送啦~
文章下方有公眾號二維碼,可直接微信掃一掃關注即可。
今天分享的文章主要講解如何從郵件里面提取用戶返回的線上問題內容並做解析,通過拿到的數據信息進行分析整理,然后進行封裝請求禪道里的接口進行提交,提交請求過程中會對數據庫中是否存在進行一次判斷處理,如果沒有存在的就提交,如果數據庫中存在就不用再提交,基於這個思路來看下今天的分享。
基礎信息准備
1 import imaplib, email,re,requests,time,pymysql 2 imapserver = 'smtp.office365.com' 3 emailuser = "qa.notice@qq.com" 4 emailpasswd = "test123" 5 6 #beta環境禪道地址 7 beta_loginhost="http://zen.beta.com/index.php?m=user&f=login" 8 beta_add_bughost="http://zen.beta.com/index.php?m=bug&f=create&productID=10&branch=0&extra=moduleID=0" 9 10 #live環境禪道地址 11 live_loginhost="https://zen.live.com/index.php?m=user&f=login" 12 live_add_bughost="https://zen.live.com/index.php?m=bug&f=create&productID=10&branch=0&extra=moduleID=0" 13 14 envs="live" #定義使用的環境
數據庫連接信息
1 2 #連接數據庫相關的信息: 3 beta_dicts={ 4 "HOST" : '10.8.2.3', 5 "PORT" : 3306, 6 "USER": 'zentao', 7 "PASSWORD" : 'test123', 8 "NAME":"zentao" 9 } 10 live_dicts={ 11 "HOST" : '10.7.1.7', 12 "PORT" : 3306, 13 "USER": 'zentao', 14 "PASSWORD" : 'test123', 15 "NAME":"zentao" 16 }
數據庫查詢
1 2 #數據庫查詢操作 3 def executesql(query,envs): 4 try: 5 if(envs=="beta"): 6 conn = pymysql.connect(beta_dicts['HOST'], beta_dicts['USER'], beta_dicts['PASSWORD'], beta_dicts['NAME'], int(beta_dicts['PORT']),charset='utf8') 7 print(beta_dicts) 8 else: 9 conn = pymysql.connect(live_dicts['HOST'], live_dicts['USER'], live_dicts['PASSWORD'], live_dicts['NAME'], int(live_dicts['PORT']),charset='utf8') 10 print(live_dicts) 11 cursor = conn.cursor() 12 cursor.execute(query) 13 result =cursor.fetchall() 14 print("execute successfully!!!") 15 if(len(result)==0): 16 return 0 17 else: 18 return result[0][0] 19 except Exception as e: 20 print(e) 21 print("execute failed") 22 finally: 23 cursor.close() 24 conn.close()
建立連接與檢索
1 2 #建立連接與檢索匹配的郵件 3 def search(): 4 print("start to connect") 5 conn = imaplib.IMAP4_SSL(imapserver) 6 conn.login(emailuser, emailpasswd) 7 conn.select('INBOX') # 選擇收件箱(默認) 8 print(conn) 9 now = time.localtime() 10 nowt = time.strftime("%d-%b-%Y", now) 11 print(nowt) 12 results , data = conn.search(None,'(FROM "Liang.Wu")','(ON "'+str(nowt)+'")') 13 mailidlist = data[0].split() 14 print(mailidlist) 15 try: 16 for id in mailidlist: 17 print(id) 18 resultss, data = conn.fetch(id, '(RFC822)') # 通過郵件id獲取郵件,data是fetch到的郵件具體內容 19 e = email.message_from_bytes(data[0][1])
解釋說明與Print
1 ''' 2 Header()類: 3 email.header.Header(s=None, charset=None, maxlinelen=None, header_name=None, continuation_ws=' ', errors='strict') 4 其中參數的含義理解如下: 5 s:標頭的值,也就是對應 From、To、Subject 的值; 6 charset:字符集格式,默認是 ASCII,但是一般指定 UTF-8 格式以兼容更多字符; 7 header_name:標頭名,就是 From、To、Subject、Time 等; 8 ''' 9 subject = email.header.make_header(email.header.decode_header(e['SUBJECT'])) 10 mail_from = email.header.make_header(email.header.decode_header(e['From'])) 11 print("郵件的subject是%s" % subject) 12 print("郵件的發件人是%s" % mail_from) 13 body = str(get_body(e), encoding='ISO-8859-1') # utf-8 gb2312 GB18030解析中文日文英文 14 print("郵件內容是%s" % body) 15 parse1(body) 16 print("good job") 17 except Exception as e: 18 print("we catch an error!!!",e) 19 finally: 20 print("logout is success") 21 print("the finally of operation!!!") 22 conn.logout()
獲取郵件主體信息
1 #獲取郵件主體信息 2 def get_body(msg): 3 if msg.is_multipart ():#Return True if the message’s payload is a list of sub-Message objects, otherwise return False. When is_multipart() returns False, the payload should be a string object. 4 return get_body(msg.get_payload(0)) 5 else: 6 '''Return the current payload, which will be a list of Message objects when is_multipart() is True, 7 or a string when is_multipart() is False. If the payload is a list and you mutate the list object, 8 you modify the message’s payload in place.''' 9 return msg.get_payload(None , decode=True)
解析郵件內容並提交禪道
1 2 # 解析郵件內容並調用禪道提交(上一篇文章結合來看) 3 def parse1(body): 4 pattern = re.compile('Dear Colleagues,<br>(.*?)Thanks and Regards,<br>', re.S) 5 pattern1 = re.compile('black">(.*?)<o:p>', re.S) 6 pattern2=re.compile(';">\r(.*?);\r<',re.S) 7 lists = re.findall(pattern, body) 8 print("*"*10) 9 lists = str(lists[0]).replace("\n", "").split("<br>") 10 print(lists) 11 resultlist = [] 12 for i in range(len(lists)): 13 if (len(lists[i]) > 1): 14 resultlist.append(lists[i]) 15 print(resultlist) 16 id = resultlist[1] 17 ids=str(str(resultlist[1]).split(":")[1]).lstrip() 18 Subject = resultlist[2] 19 Subjects="[FeedBack-"+str(str(resultlist[1]).split(":")[1]).lstrip() + "]--"+str(str(resultlist[2]).split(":")[1]) 20 Creator = resultlist[3] 21 Creators = str(str(resultlist[3]).split(":")[1]) 22 Category = resultlist[4] 23 IssueCategory = resultlist[5] 24 if ("Low" in resultlist[6]): 25 Severity = "4" 26 Severity_desc = "Severity: Low (Limited business impact)" 27 if ("Medium" in resultlist[6]): 28 Severity = "3" 29 Severity_desc = "Severity: Medium (Functional but impact operations)" 30 if ("High" in resultlist[6]): 31 Severity = "2" 32 Severity_desc = "Severity: High (Major system outage)" 33 Module = resultlist[7] 34 if('black">' in resultlist[8] and '<o:p>' in resultlist[8]): 35 Details = str(re.findall(pattern1, resultlist[8])[0]).replace(""", "\"") 36 if(';">\r' in resultlist[8] and ';\r<' in resultlist[8]): 37 Details = str(re.findall(pattern2, resultlist[8])[0]).replace(""", "\"") 38 link = resultlist[9] 39 steps = id + "<br>" + Subject + "<br>" + Creator + "<br>" + Category + "<br>" + IssueCategory + "<br>" + Severity_desc + "<br>" + Module + "<br>" + Details + "<br>" + link 40 print(steps.replace("<br>", "\n")) 41 sql="SELECT * FROM zt_bug WHERE title LIKE \"[FeedBack-"+str(ids)+"%\"" 42 print(sql) 43 if(executesql(sql,envs)>=1): 44 print("there is an record exists!!!") 45 #add_bug(Subjects, Creators, Severity, steps,envs) 46 else: 47 add_bug(Subjects,Creators,Severity,steps,envs)
提交bug至禪道
1 #提交bug到禪道的方法 2 def add_bug(a,b,c,d,e): #此方法可以與上一遍文章結合在一起提交到禪道 3 pass
以上內容就是今天分享的全部內容,這個最后的方法也是空着的,所以這里也就回答了上一篇文章中大家提到的疑問—->自動提交bug到禪道的使用場景會是怎么樣的。
備注:我的個人公眾號已正式開通,致力於測試技術的分享,包含:大數據測試、功能測試,測試開發,API接口自動化、測試運維、UI自動化測試等,微信搜索公眾號:“無量測試之道”,或掃描下方二維碼:
添加關注,讓我們一起共同成長!