Python接口自動化測試腳本-實現禪道登錄


 未來應用方向:UI自動化測試或接口自動化測試發現的Bug可自動錄入禪道,帶截圖與相關報錯信息、

 

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File   :NingMengProject -> PostZentao.py
@IDE    :PyCharm
@Author :孤問萬古愁
@Date   :2020/3/14 13:13
@Desc   :
=================================================='''

import requests
import hashlib
import re
# 禪道登錄流程:
# 1、打開登陸頁面的時候頁面產生一個zentaosid的唯一標識,以及一個verifyRand隨機值
# 2、對輸入的密碼進行md5加密
# 3、已經加密的密碼+verifyRand再一次md5加密
# 4、然后post請求登陸地址根據唯一的zentaosid驗密

class GetToken():
    """獲取禪道的token值及登錄后的cookies"""

    def __init__(self, username='admin', pwd='admin123456'):  # 用戶名、密碼設置默認值
        self.username = username
        self.pwd = pwd

    def get_token(self):
        loginUrl = "http://127.0.0.1:8088/zentao/user-login.html"
        headers_1 = {"Content-Type": "application/x-www-form-urlencoded"}    # 定義headers

        get_sid = requests.get(loginUrl)   # get方法請求登錄頁面,用於獲取SID
        get_sid.encoding = 'utf-8'
        verifyRand = re.findall("id='verifyRand' value='(\d+)'", get_sid.text)[0]  # 獲取verifyRand值
        SID = get_sid.cookies["zentaosid"]
        # print(SID)

        hlFirst = hashlib.md5()
        hlFirst.update(self.pwd.encode(encoding='utf-8'))  # 第一次對密碼進行加密
        # print('Md5 第一次加密結果 = ' + hlFirst.hexdigest())
        passwordResult = hlFirst.hexdigest() + verifyRand
        # print("passwordResult=" + passwordResult)
        hlLast = hashlib.md5()
        hlLast.update(passwordResult.encode(encoding='utf-8'))  # 第二次加密
        # print('Md5 第二次加密結果 = ' + hlLast.hexdigest())

        # 定義請求參數body
        bodyRequest = {"account": self.username, "password": hlLast.hexdigest(), "passwordStrength": 1,
                       "referer": "/zentao/", "verifyRand": verifyRand, "keepLogin": 1}

        # 定義cookies
        loginCookies = dict(zentaosid=SID, lang='zh-cn', keepLogin='on')
        loginRequest = requests.post(loginUrl, data=bodyRequest, cookies=loginCookies)

        token = loginRequest.cookies['zp']   # 從cookies中獲取token

        # 測試一下:訪問我的地盤頁面
        test = requests.get("http://127.0.0.1:8088/zentao/my/",cookies=loginRequest.cookies)
        # print(test.text)
        # print(token)
        # # print(loginRequest.text)

        return token,loginRequest.cookies

if __name__ == '__main__':
    # GetToken().get_token()
    # print(GetToken().get_token())
    url="http://127.0.0.1:8088/zentao/product-create.html"
    text_1=requests.get(url,cookies=GetToken().get_token()[1]).text
    # print(text_1)
    uid = re.findall("var kuid = '(.*?)';", text_1)[0]
    headers_1 = {"Content-Type": "application/x-www-form-urlencoded"}
    body_te="name=&code=&line=0&PO=admin&QD=&RD=&type=normal&status=normal&desc=&acl=open&uid=5e6f56b6cb28f"
    body_add_product={"name":"禪道接口自動化","code":"ZenTaoInterface","line":0,"PO":"admin","QD":"","RD":"","type":"normal","status":"normal","desc":"","acl":"open","uid":uid}
    result = requests.post(url,data=body_add_product,headers=headers_1,cookies=GetToken().get_token()[1])
    print(result.text)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM