import requests
from cfg import url
import hashlib
from robot.libraries.BuiltIn import BuiltIn
import yaml
class LibLogin:
def login(self,phone,pwd,cookiesName=None):
#输入密码,转换成md5
m = hashlib.md5()
m.update(pwd.encode('utf-8'))
mpwd=m.hexdigest()
dict = {'Content-Type': 'application/json'}
payload=f'''{{
"phone": "{phone}",
"pwd":"{mpwd}"
}}'''
r=requests.post(f'{url}/user/login',data=payload,headers=dict)
if cookiesName:
BuiltIn().set_global_variable('${%s}' % cookiesName, r.cookies)
return r.cookies
if __name__ == '__main__':
LL=LibLogin()
cookies=LL.login('15868147325','11111111')
print(cookies)
import requests
from cfg import url
from robot.libraries.BuiltIn import BuiltIn
class LibMattype():
#物料类型列表
def mattype_list(self,cookies):
r=requests.post(f'{url}/material/type/list',cookies=cookies)
# print(r.json())
return r.json()
#物料类型添加和修改
def mattype_add(self,id,name,cookies,idSavedName=None):
#添加前列表
beforelist=self.mattype_list(cookies)['data']
dict = {'Content-Type': 'application/json'}
payload=f'''{{
"id":"{id}",
"name":"{name}"
}}'''
r = requests.post(f'{url}/material/type/addOrUpd',data=payload.encode('utf-8'), headers=dict,cookies=cookies)
#添加后的列表
afterlist=self.mattype_list(cookies)['data']
rlist=[one for one in afterlist if one not in beforelist]
#设置id到全局变量中
if idSavedName:
BuiltIn().set_global_variable('${%s}'%idSavedName,rlist[0]['id'])
print(f"global var set: ${idSavedName}:{rlist[0]['id']}")
print(r.json())
return r.json()
#物料类型删除
def mattype_del(self,id,cookies):
dict = {'Content-Type': 'application/json'}
payload = f'''{{
"id":{id}
}}'''
r = requests.post(f'{url}/material/type/del', data=payload.encode('utf-8'), headers=dict,cookies=cookies)
# print(r.json())
return r.json()
# 增加修改前后列表比较,返回新增和修改后列表
def mattype_add_differ(self,beforer,afterr):
list = [one for one in afterr['data'] if one not in beforer['data']]
return list
#删除前后列表比较,核对删除的id和name
def mattype_del_differ(self, beforer, afterr):
list = [one for one in beforer['data'] if one not in afterr['data']]
return list
# def mattype_delall(self):
# rd=self.mattyp_list()
# if rd['code']!=0:
# raise Exception('列出所有设备类型失败')
# for one in rd['data']:
# self.mattype_del(one['id'])
# rd=self.mattyp_list()
# if rd['data']!=[]:
# raise Exception('删除所有设备类型失败')
if __name__ == '__main__':
from lib.LibLogin import LibLogin
LL=LibLogin()
cookies=LL.login('15325719761','12345678')
LMT=LibMattype()
LMT.mattype_add("","请填写物料类型",cookies)
init
登录
*** Settings ***
Library lib.LibLogin
Suite Setup login 15868147325 11111111 cookies
物料类型init
*** Settings ***
Library lib.LibMattype
Suite Setup mattype add ${empty} 智能开关 ${cookies} mattype_id_init
Suite Teardown mattype del ${mattype_id_init} ${cookies}
物料类型管理
*** Settings ***
Library lib.LibMattype
*** Test Cases ***
添加物料类型 - mt001001
#列出物料类型
${listRetBefore}= mattype list ${cookies}
#添加物料类型
${addRet}= mattype add ${empty} 智能灯 ${cookies} mattype_id
should be true $addRet['code']==0
#列出添加后物料类型
${listRetAfter}= mattype list ${cookies}
#比较列表得到添加内容
${listDiffer}= mattype add differ ${listRetBefore} ${listRetAfter}
should be true $listDiffer[0]['name']=='智能灯'
[Teardown] mattype del ${mattype_id} ${cookies}
修改物料类型 - mt002001
#添加物料类型
${addRet}= mattype add ${empty} 智能灯 ${cookies} mattype_id
should be true $addRet['code']==0
#列出物料类型
${listRetBefore}= mattype list ${cookies}
#修改
${modifyRet}= mattype add ${mattype_id} 吸顶智能灯 ${cookies}
should be true $modifyRet['code']==0
#列出添加后物料类型
${listRetAfter}= mattype list ${cookies}
#比较列表得到添加内容
${listDiffer}= mattype add differ ${listRetBefore} ${listRetAfter}
should be true $listDiffer[0]['id']== ${mattype_id}
should be true $listDiffer[0]['name']=='吸顶智能灯'
[Teardown] mattype del ${mattype_id} ${cookies}
删除物料类型 - mt003001
#添加物料类型
${addRet}= mattype add ${empty} 智能灯 ${cookies} mattype_id
should be true $addRet['code']==0
#列出物料类型
${listRetBefore}= mattype list ${cookies}
#删除
${delRet}= mattype del ${mattype_id} ${cookies}
should be true $delRet['code']==0
#列出删除后物料类型
${listRetAfter}= mattype list ${cookies}
${listDiffer}= mattype del differ ${listRetBefore} ${listRetAfter}
should be true $listDiffer[0]['id']== ${mattype_id}
should be true $listDiffer[0]['name']=='智能灯'
执行
robot -P . tc