【接口自动化测试实战1】企业微信接口自动化测试实战

  • restful相关资料

Roy_Fielding:https://en.wikipedia.org/wiki/Roy_Fielding
论文:https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
http://www.ruanyifeng.com/blog/2014/05/restful_api.html

  • requests官方文档

https://requests.readthedocs.io/en/master/

  • 企业微信api文档

https://work.weixin.qq.com/api/doc/90000/90135/90664

  • 企业微信接口逻辑
    image

  • 企业微信错误码查询工具

https://open.work.weixin.qq.com/devtool/query

实战部分

通讯录权限

课堂练习

课堂练习:

import requests
class TestToken:

    def test_token1(self):
        corpid = "ww24dc354fa0a345cf"
        corpsecret = "T3fYAsORyoOPMNBjJvXq_ELcXF_7inR5gNnfa-zbM-4"
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
        r = requests.get(url=url)
        print(r.json())

    def test_token2(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid": "ww24dc354fa0a345cf",
            "corpsecret": "T3fYAsORyoOPMNBjJvXq_ELcXF_7inR5gNnfa-zbM-4"
        }
        r = requests.get(url=url, params=params)
        print(r.json())

获取企业微信token

代码

import requests

class TestToken:

    def test_token1(self):
        params={
            'corpid':'wwaa5e12b39afbafe3',
            'corpsecret':'2_YAsegpEFFziREvf3GB5Vr2bIgAP7cBIWr35DN_b-s'
        }
        url='https://qyapi.weixin.qq.com/cgi-bin/gettoken'
        r=requests.get(url,params=params)
        print(r.json())
        print("*"*20)
    def test_token2(self):
        corpid = 'wwaa5e12b39afbafe3'
        corpsecret = '2_YAsegpEFFziREvf3GB5Vr2bIgAP7cBIWr35DN_b-s'
        url = f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}'
        r = requests.get(url)
        print(r.json())

执行结果

import requests


class TestToken:
    def test_token(self):
        corpid = 'ww170e13c876c74789'
        corpsecret = 'N5-mHIhmaAYP1-qcMaccISC0Ktw5L8YY93GDjUy7pjs'
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
        r =requests.get(url=url)
        print(r.json())

    def test_token_param(self):
        # corpid = 'ww170e13c876c74789'
        # corpsecret = 'N5-mHIhmaAYP1-qcMaccISC0Ktw5L8YY93GDjUy7pjs'
        param = {
            'corpid':'ww170e13c876c74789',
            'corpsecret':'N5-mHIhmaAYP1-qcMaccISC0Ktw5L8YY93GDjUy7pjs'
        }
        url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
        r =requests.get(url=url,params=param)
        print(r.json())
import requests

class TestToken:
    def test_token(self):
        corpid = "ww441338b28c377647"
        corpsecret = "7-Dbm-lumcWei3dpn-1tof-JmxC1sApIIKAnhkhYVi0"
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
        r = requests.get(url=url)
        print(r.json())

    def test_token2(self):
        corpid = "ww441338b28c377647"
        corpsecret = "7-Dbm-lumcWei3dpn-1tof-JmxC1sApIIKAnhkhYVi0"
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid":"ww441338b28c377647",
            "corpsecret" : "7-Dbm-lumcWei3dpn-1tof-JmxC1sApIIKAnhkhYVi0"
        }
        r = requests.get(url=url)

第二种方式,param都没传进去的

import requests

class TestDemo():
    def test_token(self):
        corpid = "wwf909985f20e7bb66"
        corpsecret = "ZxzplF-X8jacr_C5Hda0WGnexMwoKzDHH2X3BZpIZt4"
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        param = {
            "corpid" : corpid,
            "corpsecret" : corpsecret
        }
        r = requests.get(url=url,params=param)
        print(r.json())

import requests

class TestToken:

def test_token(self):
    corpid = "MtOofZxnsWNN7DSwkxYtY6lReC1lr"
    corpsecret = "ktHhof0Fg8SvCWtkAoGDfLasXM9ukI2ddbLXfBNON39"
    url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid = {corpid}&corpsecret = {corpsecret}"
    r = requests.get(url=url)
    print(r.json())

def test_token1(self):
    corpid = "MtOofZxnsWNN7DSwkxYtY6lReC1lr"
    corpsecret = "ktHhof0Fg8SvCWtkAoGDfLasXM9ukI2ddbLXfBNON39"
    url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    params = {
        "corpid": corpid,
        "corpsecret": corpsecret
    }
    r = requests.get(url=url, params=params)
    print(r.json())

注意格式化

每次发的文本怎么跟他们的格式不太一样呢,发布的时候哪里需要格式化一下吗

课堂练习
import requests
class TestToken:

    def test_token(self):
        corpid = "ww2e51e84799d1f6f7"
        corpsecret = "xMZ8h3PCTOGFp9TtjvWWfqtX--uwTdCPVcSmzRqRMn8"
        # 使用f可以直接使用{}引用参数
        url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"
        r = requests.get(url=url)
        # 使用json获取它的响应体
        print(r.json())

    def test_token_param(self):
        corpid = "ww2e51e84799d1f6f7"
        corpsecret = "xMZ8h3PCTOGFp9TtjvWWfqtX--uwTdCPVcSmzRqRMn8"
        param = {
            "corpid": corpid,
            "corpsecret": corpsecret
        }
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        r = requests.get(url=url, params=param)
        print(r.json())
执行结果
PASSED                              [ 50%]{'errcode': 0, 'errmsg': 'ok', 'access_token': 'v8YoWTM3JvzpCFqO3k-yuEagDlWzhOp0IuJBYR8F9nXHBQgpJ0Wzua4N6RWnpO8YXeib9D_FvMSHWaskg2LK_IonA-bj4gqEpeMUTB5Y3rmhfmRQmPW23GZIDrulVnmHjJUfXQpxT0Mm2z4v4vGyS80axBWkBhn-1RsbiVDeGi8wkJk0BfjObI7SIxIcyiar2mclVGrq3wHEjlYu2ezusQ', 'expires_in': 7200}
PASSED                        [100%]{'errcode': 0, 'errmsg': 'ok', 'access_token': 'v8YoWTM3JvzpCFqO3k-yuEagDlWzhOp0IuJBYR8F9nXHBQgpJ0Wzua4N6RWnpO8YXeib9D_FvMSHWaskg2LK_IonA-bj4gqEpeMUTB5Y3rmhfmRQmPW23GZIDrulVnmHjJUfXQpxT0Mm2z4v4vGyS80axBWkBhn-1RsbiVDeGi8wkJk0BfjObI7SIxIcyiar2mclVGrq3wHEjlYu2ezusQ', 'expires_in': 7200}

import requests

class TestToken:

    def test_token1(self):
        corpid ="wwa00084331d6037a9"
        corpsecret ="Oo7HNzj6u60jPosjy7Lj-gVpetM4C9Pl-COpcaqnkzY"
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid" : corpid,
            "corpsecret" : corpsecret
        }
        r = requests.get(url=url, params=params)
        print(r.json())

    def test_token2(self):
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = {
            "corpid" : "wwa00084331d6037a9",
            "corpsecret" : "Oo7HNzj6u60jPosjy7Lj-gVpetM4C9Pl-COpcaqnkzY"
        }
        r = requests.get(url=url, params=params)
        print(r.json())

执行结果

关闭