Commit cc2cca77c61d45f2462ea80c22526c066fa9327d
1 parent
224ffd15
修改mysession客户端操作
Showing
5 changed files
with
98 additions
and
95 deletions
.idea/card-app.iml
.idea/dictionaries/Ljq.xml
0 → 100644
.idea/modules.xml
... | ... | @@ -3,7 +3,6 @@ |
3 | 3 | <component name="ProjectModuleManager"> |
4 | 4 | <modules> |
5 | 5 | <module fileurl="file://$PROJECT_DIR$/.idea/card-app.iml" filepath="$PROJECT_DIR$/.idea/card-app.iml" /> |
6 | - <module fileurl="file://$PROJECT_DIR$/../../../../08自动化项目/09AutoTest/cardApp/.idea/cardApp.iml" filepath="$PROJECT_DIR$/../../../../08自动化项目/09AutoTest/cardApp/.idea/cardApp.iml" /> | |
7 | 6 | </modules> |
8 | 7 | </component> |
9 | 8 | </project> |
10 | 9 | \ No newline at end of file | ... | ... |
commons/MySession.py
... | ... | @@ -7,72 +7,78 @@ from commons.Logging import Logger |
7 | 7 | from commons.clientSession import cliSession |
8 | 8 | log=Logger() |
9 | 9 | |
10 | + | |
10 | 11 | class mysession(): |
11 | 12 | "封装了requests的基类,以供后期统一使用" |
12 | - | |
13 | - url="http://test.uap.diligrp.com/login/login.action" | |
14 | - header={ | |
15 | - "Host": "test.uap.diligrp.com", | |
16 | - "Connection": "keep-alive", | |
17 | - "Content-Length": "33", | |
18 | - "Cache-Control": "max-age=0", | |
19 | - "Upgrade-Insecure-Requests": "1", | |
20 | - "Origin": "http://test.uap.diligrp.com", | |
21 | - "Content-Type": "application/x-www-form-urlencoded", | |
22 | - "User-Agent": "Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.212Safari/537.36", | |
23 | - "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", | |
24 | - "Referer": "http://test.uap.diligrp.com/login/index.html", | |
25 | - "Accept-Encoding": "gzip,deflate", | |
26 | - "Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7", | |
27 | - "Cookie": "UAP_accessToken=;UAP_refreshToken=;UAP_loginPath="} | |
28 | - body="userName=sg_wenze&password=111111" | |
29 | - | |
13 | + | |
14 | + url = "http://test.uap.diligrp.com/login/login.action" | |
15 | + header = { | |
16 | + "Host": "test.uap.diligrp.com", | |
17 | + "Connection": "keep-alive", | |
18 | + "Content-Length": "33", | |
19 | + "Cache-Control": "max-age=0", | |
20 | + "Upgrade-Insecure-Requests": "1", | |
21 | + "Origin": "http://test.uap.diligrp.com", | |
22 | + "Content-Type": "application/x-www-form-urlencoded", | |
23 | + "User-Agent": "Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/90.0.4430.212Safari/537.36", | |
24 | + "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", | |
25 | + "Referer": "http://test.uap.diligrp.com/login/index.html", | |
26 | + "Accept-Encoding": "gzip,deflate", | |
27 | + "Accept-Language": "zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7", | |
28 | + "Cookie": "UAP_accessToken=;UAP_refreshToken=;UAP_loginPath="} | |
29 | + body = "userName=sg_wenze&password=111111" | |
30 | + | |
30 | 31 | def __init__(self): |
31 | 32 | "如下代码,可以通过配置文件来控制测试环境和灰度环境,http和https" |
32 | - self.url=mysession.url.replace("http://test.",com.get_global_config("global_data", "environment", "en") ) | |
33 | - self.header=mysession.header | |
34 | - self.body=mysession.body | |
35 | - self.timeout = (5,5) | |
33 | + self.url = mysession.url.replace("http://test.", com.get_global_config("global_data", "environment", "en")) | |
34 | + self.header = mysession.header | |
35 | + self.body = mysession.body | |
36 | + self.timeout = (5, 5) | |
36 | 37 | self.max_retries = 3 |
37 | 38 | self.keep_alive = False |
38 | - self.ssl_verify=False | |
39 | - self.proxies=None | |
40 | - self.allow_redirects=False | |
41 | - self.webHeaders,self.clientHeaders,self.userInfo = cliSession().loginUser() | |
39 | + self.ssl_verify = False | |
40 | + self.proxies = None | |
41 | + self.allow_redirects = False | |
42 | 42 | |
43 | -# self.proxies={'http': 'http://localhost:8888', 'https': 'http://localhost:8888'} | |
43 | + # self.proxies={'http': 'http://localhost:8888', 'https': 'http://localhost:8888'} | |
44 | + def cliLogin(self): | |
45 | + self.webHeaders, self.clientHeaders, self.userInfo = cliSession().loginUser() | |
44 | 46 | |
45 | - def get_session(self,account,**kwargs): | |
47 | + def get_session(self, account, **kwargs): | |
48 | + print("get_session") | |
46 | 49 | "如下代码,可以通过配置文件来控制登录的账户session" |
47 | - self.body=self.body.replace("sg_wenze", com.get_global_config("global_data", "account", account).split("&")[0]) | |
48 | - self.body=self.body.replace("111111", com.get_global_config("global_data", "account", account).split("&")[1]) | |
49 | - #requests.session()会话保持,比如使用session成功的登录了某个网站, | |
50 | - #则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数 | |
51 | - self.se=requests.session() | |
52 | - #使用session对象的方法POST/GET等 | |
53 | - re=self.se.post(url=self.url, headers=self.header,data=self.body,proxies=self.proxies,**kwargs) | |
54 | - #返回session对象,供其他接口使用 | |
55 | - return self.se | |
50 | + self.body = self.body.replace("sg_wenze", | |
51 | + com.get_global_config("global_data", "account", account).split("&")[0]) | |
52 | + self.body = self.body.replace("111111", com.get_global_config("global_data", "account", account).split("&")[1]) | |
53 | + # requests.session()会话保持,比如使用session成功的登录了某个网站, | |
54 | + # 则在再次使用该session对象求求该网站的其他网页都会默认使用该session之前使用的cookie等参数 | |
55 | + self.se = requests.session() | |
56 | + # 使用session对象的方法POST/GET等 | |
57 | + re = self.se.post(url=self.url, headers=self.header, data=self.body, proxies=self.proxies, **kwargs) | |
58 | + # 返回session对象,供其他接口使用 | |
59 | + print(self.se.cookies) | |
60 | + return self.se | |
56 | 61 | |
57 | 62 | def close_session(self): |
58 | 63 | "关闭session" |
59 | 64 | self.se.close() |
60 | 65 | |
61 | - | |
62 | - def check_login(self,account,**kwargs): | |
66 | + def check_login(self, account, **kwargs): | |
63 | 67 | "验证登录接口" |
64 | - self.body=self.body.replace("sg_wenze", com.get_global_config("global_data", "account", account).split("&")[0]) | |
65 | - self.body=self.body.replace("111111", com.get_global_config("global_data", "account", account).split("&")[1]) | |
66 | - #POST请求, | |
67 | - re=self.se.post(url=self.url, headers=self.header,data=self.body,proxies=self.proxies,allow_redirects=False,**kwargs) | |
68 | - #判断请求 | |
68 | + self.body = self.body.replace("sg_wenze", | |
69 | + com.get_global_config("global_data", "account", account).split("&")[0]) | |
70 | + self.body = self.body.replace("111111", com.get_global_config("global_data", "account", account).split("&")[1]) | |
71 | + # POST请求, | |
72 | + re = self.se.post(url=self.url, headers=self.header, data=self.body, proxies=self.proxies, | |
73 | + allow_redirects=False, **kwargs) | |
74 | + # 判断请求 | |
69 | 75 | assert "UAP_accessToken" in re.headers["Set-Cookie"] |
70 | 76 | assert "UAP_refreshToken" in re.headers["Set-Cookie"] |
71 | 77 | assert "UAP_loginPath" in re.headers["Set-Cookie"] |
72 | 78 | print("登录接口验证成功") |
73 | 79 | return re |
74 | 80 | |
75 | - def useHeadersRequests(self,method=None,url=None,headers=None,data=None,**kwargs): | |
81 | + def useHeadersRequests(self, method=None, url=None, headers=None, data=None, **kwargs): | |
76 | 82 | """ |
77 | 83 | 模拟客户端接口操作,使用headers进行cookies传递,调用requests库进行接口访问 |
78 | 84 | :param method:接口请求方式,POST,GET等 |
... | ... | @@ -84,18 +90,16 @@ class mysession(): |
84 | 90 | # print(headers) |
85 | 91 | print(data) |
86 | 92 | log.info("{0:=^86}".format('')) |
87 | - log.info("{}\n{}\n{}\n".format(url,data,kwargs)) | |
93 | + log.info("{}\n{}\n{}\n".format(url, data, kwargs)) | |
88 | 94 | if "gateway" in url: |
89 | 95 | # 判断接口路径,通过接口路径钟是否包含gateway来判定接口是否是由客户端进行访问,来判定headers使用 |
90 | - self.clientHeaders = dict(self.clientHeaders,**headers) | |
96 | + self.clientHeaders = dict(self.clientHeaders, **headers) | |
91 | 97 | res = requests.request(method=method, url=url, data=data, headers=self.clientHeaders, **kwargs) |
92 | 98 | else: |
93 | 99 | self.webHeaders = dict(self.webHeaders, **headers) |
94 | 100 | res = requests.request(method=method, url=url, data=data, headers=self.webHeaders, **kwargs) |
95 | 101 | return res |
96 | 102 | |
97 | - | |
98 | - | |
99 | 103 | def get(self, url, **kwargs): |
100 | 104 | """Sends a GET request. Returns :class:`Response` object. |
101 | 105 | |
... | ... | @@ -103,13 +107,13 @@ class mysession(): |
103 | 107 | :param \*\*kwargs: Optional arguments that ``request`` takes. |
104 | 108 | :rtype: requests.Response |
105 | 109 | """ |
106 | - #记录日志 | |
110 | + # 记录日志 | |
107 | 111 | log.info("{0:=^86}".format('')) |
108 | - log.info("{}\n{}\n".format(url,kwargs)) | |
109 | - #进行请求 | |
110 | - re=self.se.get(url,**kwargs,proxies=self.proxies,timeout=self.timeout) | |
112 | + log.info("{}\n{}\n".format(url, kwargs)) | |
113 | + # 进行请求 | |
114 | + re = self.se.get(url, **kwargs, proxies=self.proxies, timeout=self.timeout) | |
111 | 115 | return re |
112 | - | |
116 | + | |
113 | 117 | def post(self, url, data=None, json=None, **kwargs): |
114 | 118 | """Sends a POST request. Returns :class:`Response` object. |
115 | 119 | :param url: URL for the new :class:`Request` object. |
... | ... | @@ -119,11 +123,11 @@ class mysession(): |
119 | 123 | :param \*\*kwargs: Optional arguments that ``request`` takes. |
120 | 124 | :rtype: requests.Response |
121 | 125 | """ |
122 | - #记录日志 | |
126 | + # 记录日志 | |
123 | 127 | log.info("{0:=^86}".format('')) |
124 | - log.info("{}\n{}\n{}\n{}".format(url,data,json,kwargs)) | |
125 | - #进行请求 | |
126 | - re=self.se.post(url, data=data, json=json,proxies=self.proxies, **kwargs,timeout=self.timeout) | |
128 | + log.info("{}\n{}\n{}\n{}".format(url, data, json, kwargs)) | |
129 | + # 进行请求 | |
130 | + re = self.se.post(url, data=data, json=json, proxies=self.proxies, **kwargs, timeout=self.timeout) | |
127 | 131 | return re |
128 | 132 | |
129 | 133 | def options(self, url, **kwargs): |
... | ... | @@ -132,10 +136,10 @@ class mysession(): |
132 | 136 | :param \*\*kwargs: Optional arguments that ``request`` takes. |
133 | 137 | :rtype: requests.Response |
134 | 138 | """ |
135 | - #记录日志 | |
136 | - log.info(url,kwargs) | |
137 | - #进行请求 | |
138 | - re=self.se.options(url,**kwargs) | |
139 | + # 记录日志 | |
140 | + log.info(url, kwargs) | |
141 | + # 进行请求 | |
142 | + re = self.se.options(url, **kwargs) | |
139 | 143 | return re |
140 | 144 | |
141 | 145 | def head(self, url, **kwargs): |
... | ... | @@ -145,12 +149,12 @@ class mysession(): |
145 | 149 | :param \*\*kwargs: Optional arguments that ``request`` takes. |
146 | 150 | :rtype: requests.Response |
147 | 151 | """ |
148 | - #记录日志 | |
149 | - log.info(url,kwargs) | |
150 | - #进行请求 | |
151 | - re=self.se.head(url,**kwargs) | |
152 | + # 记录日志 | |
153 | + log.info(url, kwargs) | |
154 | + # 进行请求 | |
155 | + re = self.se.head(url, **kwargs) | |
152 | 156 | return re |
153 | - | |
157 | + | |
154 | 158 | def put(self, url, data=None, **kwargs): |
155 | 159 | """Sends a PUT request. Returns :class:`Response` object. |
156 | 160 | :param url: URL for the new :class:`Request` object. |
... | ... | @@ -159,54 +163,54 @@ class mysession(): |
159 | 163 | :param \*\*kwargs: Optional arguments that ``request`` takes. |
160 | 164 | :rtype: requests.Response |
161 | 165 | """ |
162 | - #记录日志 | |
163 | - log.info(url,data,kwargs) | |
164 | - #进行请求 | |
165 | - re=self.se.put(url, data,**kwargs) | |
166 | + # 记录日志 | |
167 | + log.info(url, data, kwargs) | |
168 | + # 进行请求 | |
169 | + re = self.se.put(url, data, **kwargs) | |
166 | 170 | return re |
167 | - | |
171 | + | |
168 | 172 | def delete(self, url, **kwargs): |
169 | 173 | """Sends a DELETE request. Returns :class:`Response` object. |
170 | 174 | :param url: URL for the new :class:`Request` object. |
171 | 175 | :param \*\*kwargs: Optional arguments that ``request`` takes. |
172 | 176 | :rtype: requests.Response |
173 | 177 | """ |
174 | - #记录日志 | |
175 | - log.info(url,kwargs) | |
176 | - #进行请求 | |
177 | - re=self.se.delete(url,**kwargs) | |
178 | + # 记录日志 | |
179 | + log.info(url, kwargs) | |
180 | + # 进行请求 | |
181 | + re = self.se.delete(url, **kwargs) | |
178 | 182 | return re |
179 | - | |
183 | + | |
180 | 184 | def set_mark(self): |
181 | 185 | "用户自定义优先级方法" |
182 | - mark_list=eval(com.get_global_config("global_data","mark","list")) | |
183 | - print("预设运行标记:",mark_list) | |
186 | + mark_list = eval(com.get_global_config("global_data", "mark", "list")) | |
187 | + print("预设运行标记:", mark_list) | |
184 | 188 | global _global_mark |
185 | - if type(mark_list)==type([]) and len(mark_list)!=0 : | |
186 | - _global_mark=mark_list | |
189 | + if type(mark_list) == type([]) and len(mark_list) != 0: | |
190 | + _global_mark = mark_list | |
187 | 191 | return _global_mark |
188 | - elif type(mark_list)==type([]) and len(mark_list)==0: | |
189 | - _global_mark=False | |
192 | + elif type(mark_list) == type([]) and len(mark_list) == 0: | |
193 | + _global_mark = False | |
190 | 194 | return _global_mark |
191 | 195 | else: |
192 | 196 | raise Exception("error,pls check mark data") |
193 | 197 | |
194 | - def mark(self,m=None): | |
198 | + def mark(self, m=None): | |
195 | 199 | try: |
196 | - #没有输入任何预设值,默认跑全部 | |
197 | - if _global_mark == False : | |
200 | + # 没有输入任何预设值,默认跑全部 | |
201 | + if _global_mark == False: | |
198 | 202 | return True |
199 | - #输入预设值且未标记用例,默认跑全部 | |
203 | + # 输入预设值且未标记用例,默认跑全部 | |
200 | 204 | elif _global_mark != False and (m in _global_mark): |
201 | - return True | |
205 | + return True | |
202 | 206 | else: |
203 | 207 | return False |
204 | 208 | except Exception as e: |
205 | 209 | return False |
206 | 210 | |
207 | 211 | |
208 | - | |
209 | -my=mysession() | |
212 | +my = mysession() | |
210 | 213 | my.set_mark() |
211 | -s1=my.get_session("user01") | |
214 | +s1 = my.get_session("user01") | |
212 | 215 | # s2=my.get_session("user02") |
216 | +my.cliLogin() | ... | ... |
report/test.log