问题:最开始吧登录和请求头全部封装,在执行用例的时候,每条用例都需要登录
解决方法:吧登录获取的token保存在文件,在读取出来,传给headers
注意:在执行用例之前,先执行writeToken()吧token写入文件
import yaml
from API_Automation.Common.getToken import login
import os
# 把登录的token写入到token.yaml文件,所有用例只需要登录一次,不用每次登录
def writeToken():
curPath = os.path.abspath(os.path.dirname(__file__))
tokenFile = os.path.join(curPath, "token.yaml")
glodonM2Token = {
"glodonM2Token": login("test")["glodonM2Token"]
}
with open(tokenFile, "w", encoding="utf-8") as f:
yaml.dump(glodonM2Token, f)
# print("denglu chengg ")
# 读取token.yaml文件的token,后边直接传入headers
def readToken():
yaml.warnings({'YAMLLoadWarning': False}) # 处理读取文件报错
curPath = os.path.abspath(os.path.dirname(__file__))
tokenFile = os.path.join(curPath, "token.yaml")
openfile = open(tokenFile)
readfile = openfile.read()
content = yaml.load(readfile)
openfile.close()
# print(content["glodonM2Token"])
return content["glodonM2Token"]
if __name__ == '__main__':
# writeToken()
readToken()