diff --git a/Utils/webhook_handler.py b/Utils/webhook_handler.py index 99fb99dc78b71bbbff7a922c427b4c304d252d3c..ce682964432ab533dc3e5467ed581e173ce49faf 100644 --- a/Utils/webhook_handler.py +++ b/Utils/webhook_handler.py @@ -1,3 +1,5 @@ +import subprocess + import requests import time import hmac @@ -29,10 +31,11 @@ class webhookHandler: "msgtype": "markdown", "markdown": { "title":"#willdesk接口自动化测试报告", - "text": "#### 本次接口自动化测试结果如下 \n > ###### 用例总数:%s\n > ###### 成功用例数量:%s\n > ###### 失败用例数量:%s\n > ###### 报错用例数量:%s\n > ###### 跳过用例数量:%s \n > ###### 耗时:%s秒\n > ###### 报告生成时间:%s [测试报告](https://www.baidu.com)"%(result._numcollected,len(result.stats.get('passed', [])),len(result.stats.get('failed', [])),len(result.stats.get('error', [])),len(result.stats.get('skipped', [])),second,current_date_time_str) + "text": "#### 本次接口自动化测试结果如下 \n > ###### 用例总数:%s\n > ###### 成功用例数量:%s\n > ###### 失败用例数量:%s\n > ###### 报错用例数量:%s\n > ###### 跳过用例数量:%s \n > ###### 耗时:%s秒\n > ###### 报告生成时间:%s [测试报告](%s)"%(result._numcollected,len(result.stats.get('passed', [])),len(result.stats.get('failed', [])),len(result.stats.get('error', [])),len(result.stats.get('skipped', [])),second,current_date_time_str,self.get_local_ip_address()) }, } res = requests.post(url=self.webhookUrl,json=data) + # print(res.json()) def sendMsg(self,title,text): @@ -45,5 +48,18 @@ class webhookHandler: } res = requests.post(url=self.webhookUrl,data=data) - + def get_local_ip_address(self): + cmd = 'ifconfig' + result = subprocess.run(cmd,capture_output=True,text=True,shell=True) + output = result.stdout + ip = None + for line in output.split('\n'): + if 'inet ' in line and '127.0.0.1' not in line: + ip = line.split(' ')[1] + break + if ip: + ip = ip + ':8000/report.html' + else: + ip = 'https://www.baidu.com' + return ip webhook = webhookHandler() \ No newline at end of file