import requests
import re
import time
from requests_toolbelt import MultipartEncoder
class ZenTaoAPI(object):
def __init__(self):
self.url = "http://zbox.xxxx.com/zentao/"
self.headers = {
'Content-Type': "application/x-www-form-urlencoded"
}
self.data = {
"account": "zhaojuan",
"password": "Z123"
}
self.s = requests.session()
def get_session(self):
url = self.url + 'api-getSessionID.json'
print("---> get_session:", url);
self.s.get(url)
def login(self):
#url = self.url + "user-login.json"
url = self.url + "user-login.html"
response = self.s.post(url, headers=self.headers, data=self.data)
print("------->login response=", response.json)
if "登录失败" in response.content.decode("utf-8"):
print("登录失败")
return;
else:
print("登录成功")
return;
def logout(self):
url = self.url + "user-logout.json"
response = self.s.post(url, headers=self.headers, data=self.data)
return response.status_code
def add_bug(self):
#url = self.url + "bug-create-1-0-moduleID=0.json"
url = self.url + "bug-create-1-0-moduleID=0.html"
print("------->add_bug url=", url)
headers = {
'Content-Type': "application/x-www-form-urlencoded; charset=utf-8"
}
#t = time.asctime(time.localtime(time.time()))
datas = {
"product": "7", # int 所属产品 * 必填
"openedBuild": "0", # int | trunk 影响版本 * 必填
"branch": "2", # int 分支 / 平台
"module": "1", # int 所属模块
"project": "2", # int 所属项目
"assignedTo": "zhanggaoqiang", # string 指派给
"deadline": "", # date 截止日期 日期格式:YY - mm - dd,如:2019 - 01 - 01
"type": "codeerror", # string Bug类型
"os": "", # string 操作系统 取值范围:
"browser": "", # string
"color": "", # string 颜色格式: # RGB,如:#3da7f5
"severity": "3", # int 严重程度 取值范围:1 | 2 | 3 | 4
"pri": "3", # int 优先级 取值范围:0 | 1 | 2 | 3 | 4
"mailto": "", # string 抄送给 填写帐号,多个账号用','分隔。
"keywords": "", # string 关键词
"title": "ffffffffff", # string Bug标题 * 必填
"steps": "set bug link in here" # string 重现步骤
}
body2 = MultipartEncoder(
fields=[
('product', "7"),
('module', '0'),
('project', ' '),
('openedBuild[]', 'trunk'),
('assignedTo', 'zhaojuan'),
('deadline', ''),
('type', 'codeerror'),
('os', ''),
('browser', ''),
('title', '正确的账号密码登录失败'), # bug 名称
('color', ''),
('severity', '3'),
('pri', '3'),
('steps', '<p>[步骤]</p>\n<p>输入正确的账号名密码进行完成登录</p>\n<br />\n<p>[结果]</p>\n登录失败<br />\n<p>[期望]</p>\n登录成功<br />'),
('story', '0'),
('task','0'),
('oldTaskID', '0'),
('mailto[]', ''),
('contactListMenu', ''),
('keywords', ''),
('status', 'active'),
('labels[]', ''),
('files[]', ''),
('uid', '602f5eb06ddc9'),
('case', '0'),
('caseVersion', '0'),
('caseVersion', '0'),
('result', '0'),
('testtask', '0'),
],
)
#response = self.s.post(url, headers=headers, data=datas)
#print(response.content.decode('utf-8'))
#{'Content-Type': body2.content_type}
response = self.s.post(url, headers={'Content-Type': body2.content_type}, data=body2, timeout=15)
print("提交bug,响应内容:{}".format(response.text))
print("-------->response.text:", response.text)
if '保存成功' in response.text:
print('bug提交成功!')
else:
print('bug提交失败')
if __name__ == "__main__":
bug = ZenTaoAPI()
bug.get_session()
bug.login()
bug.add_bug()
bug.logout()
结果是:登录成功,但是bug提交失败请大佬给与指点,哪个地方有错误。
登录成功
------->add_bug url= http://zbox.xxxx.com/zentao/bug-create-1-0-moduleID=0.html
提交bug,响应内容:<html><meta charset='utf-8'/><style>body{background:white}</style><script>self.location='/zentao/user-login-L3plbnRhby9idWctY3JlYXRlLTEtMC1tb2R1bGVJRD0wLmh0bWw=.html';
</script>
-------->response.text: <html><meta charset='utf-8'/><style>body{background:white}</style><script>self.location='/zentao/user-login-L3plbnRhby9idWctY3JlYXRlLTEtMC1tb2R1bGVJRD0wLmh0bWw=.html';
</script>
bug提交失败