login.py 3.01 KB
#!/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,allow_redirects=False,**kwargs)
        #返回请求对象,供断言使用
        return re

    def get_session(self,account,**kwargs):
        #如下代码,可以通过配置文件来控制登录的账户
        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.headers)

# 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)