Python Robot

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值