login.py
3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import json
from commons import common as com
from commons.Logging import Logger
log=Logger()
class login():
url="http://test.uap.diligrp.com/login/login.action"
header={
"Host": "test.uap.diligrp.com",
"Connection": "keep-alive",
"Content-Length": "33",
"Cache-Control": "max-age=0",
"Upgrade-Insecure-Requests": "1",
"Origin": "http://test.uap.diligrp.com",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.212Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Referer": "http://test.uap.diligrp.com/login/index.html",
"Accept-Encoding": "gzip,deflate",
"Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7",
"Cookie": "UAP_accessToken=;UAP_refreshToken=;UAP_loginPath="}
body="userName=sg_wenze&password=111111"
def __init__(self):
"如下代码,可以通过配置文件来控制测试环境和灰度环境,http和https"
self.url=login.url.replace("http://test.",com.get_global_config("global_data", "environment", "en") )
self.header=login.header
self.body=login.body
def post(self,name,password,**kwargs):
"""为登录接口单独写的一个POST请求方法;
#参数话账户和密码,通过再业务代码中传递不同的账户和密码,实现测试的不同场景"""
self.body=self.body.replace("sg_wenze", name)
self.body=self.body.replace("111111", password)
re=requests.post(url=self.url, headers=self.header,data=self.body,**kwargs)
#返回请求对象,供断言使用
return re
def get_session(self,account,**kwargs):
"为其他接口提供获取session的方法"
#如下代码,可以通过配置文件来控制登录的账户
self.body=self.body.replace("sg_wenze", com.get_global_config("global_data", "account", account).split("&")[0])
self.body=self.body.replace("111111", com.get_global_config("global_data", "account", account).split("&")[1])
#requests.session()会话保持,比如使用session成功的登录了某个网站,
#则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数
self.se=requests.session()
#使用session对象的方法POST/GET等
re=self.se.post(url=self.url, headers=self.header,data=self.body,**kwargs)
#返回session对象,供其他接口使用
return self.se
def close_session(self):
"关闭session,释放缓存"
self.se.close()
# t=login()
# re=t.post("sg_wenze", "111111",proxies={'http': 'http://localhost:8888'})
# print(re.content)
# print(re.raw.read(10))
# print(dir(re.raw.read(10)))
# log.info(re.headers)
# log.info(re.cookies)
# log.info(t.body)
# log.info(t.header)
# t.get_session("user01",proxies={'http': 'http://localhost:8888'},allow_redirects=False)