测试分享之模拟用户助力

本文为博主原创,未经授权,严禁转载及使用。
本文链接:https://blog.csdn.net/zyooooxie/article/details/118603676

之前做的需求 经常有 做活动助力的功能;要测这部分,要多去找同事帮忙助力下,但我不喜欢这样麻烦别人,想着自己能不能捯饬捯饬。

个人博客:https://blog.csdn.net/zyooooxie

【以下所有内容仅为个人项目经历,如有不同,纯属正常】

需求

在需求中,我们习惯 把主动分享的用户称为 主态,助力的用户称为 客态;

实际主态去邀请好友,客态去助力的过程:

1,主态登录活动页时,会访问一个share接口(返回当天此活动此用户的uuid);
2,主态使用邀请功能,分享 助力链接;
3,客态去访问活动链接、给主态助力;实际会请求help接口(Referer会带 前面的uuid和客态用户的信息);
4,后台 会去校验实际客态、主态、此活动、uuid,校验通过-返回:助力成功;

脚本思路

  1. 清理助力表中 用户A的全部记录;
  2. 在分享表 查 用户A某活动当天的分享uuid(表里没有时 要主动请求share接口 拿到分享uuid);
  3. 读取某txt文件,随机使用 用户BCD;
  4. 用户BCD登录活动页,访问help接口,给A进行助力;

代码

(代码有删改)

第二步:获取主态某活动当天的分享uuid


def test_get_share_uuid(mobile: int = 17777777777, ac_type: str = 'zy', user_id: str = 'https://blog.csdn.net/zyooooxie'):
    """
    获取分享的uuid
    :param mobile:
    :param ac_type:
    :param user_id:
    :return:
    """

    scene = return_scene(ac_type)

    today_str = time.strftime('%Y-%m-%d')
    sql ="""SELECT uuid FROM t_share WHERE mobile = {} and scene = {} AND user_id = '{}' AND share_date = '{}';""".format(mobile, scene, user_id, today_str)

    db, cur = connect_db()
    sql_data = fetchone_data(sql, db, cur)
    
    if sql_data is None:
        Log.info('用户-{}、当前活动-{}、当天-{} 没有生成 分享的uuid'.format(user_id, ac_type, today_str))

        return test_share_url(ac_type)
    
    Log.info('查询表 mobile为{},scene为{},user_id为{},share_date为{}的记录'.format(mobile, scene, user_id, today_str))
    Log.info('获取uuid:{}'.format(sql_data))

    return sql_data[0], scene


def test_share_url(ac_type: str, session: str = None, mobile: int = 17777777777):
    """
    获取此用户、此活动、当天的分享uuid
    :param session:
    :param ac_type:
    :return:
    """

    share_scene = return_scene(ac_type)

    if session is None:
        Log.info('登录,获取sessionId')
        session = get_session(str(mobile))[0]

    data_dict = {
    
    "shareScene":"{}".format(share_scene)}
    header = {
    
    'Cookie': 'sessionId={}'.format(session)}
    url = 'https://blog.csdn.net/zyooooxie'
    res = requests.post(url, json=data_dict, headers=header, verify=False)

    re_str = r'https://blog.csdn.net/zyooooxie@(.+?)@https://blog.csdn.net/zyooooxie'.format(share_scene)
    result = re.search(re_str, res.json().get('obj'))
    Log.info('生成的uuid是{}'.format(result.group(1)))

    return result.group(1), share_scene

第四步:客态登录、给主态助力


def test_help(phone_ke: str, user_ke: str, uuid_zhu: str, user_zhu: str, scene: int = 6666,
              req_url: str = 'https://blog.csdn.net/zyooooxie~help',
              ref_url: str = 'https://blog.csdn.net/zyooooxie'):
    """
    助力方法
    :param phone_ke:
    :param user_ke:
    :param uuid_zhu:
    :param user_zhu:
    :param scene:
    :param req_url:
    :param ref_url:
    :return:
    """
    Log.info('{}-{}'.format(phone_ke, user_ke))

    json_dict = {
    
    "mobile": phone_ke, "scene": scene, "uuid": uuid_zhu}
    res = requests.post('https://blog.csdn.net/zyooooxie/genSession', json=json_dict, verify=False)
    res_dict = res.json()

    session, user = res_dict['obj']['sessionId'], res_dict['obj']['userId']
    assert user_ke == user
    Log.info('用户 {} 登录活动页'.format(user))
    phone = ''.join([phone_ke[:3], '****', phone_ke[-4:]])

    ref_u = ref_url.split('/')[-1]
    ref = ''.join([ref_url, '?scene={0}?redirectUri=/{1}Friends&mobile='.format(scene, ref_u),
                   '&userId={0}&scene={3}&token={1}&uuid={2}'.format(user, session, uuid_zhu, scene)])

    header = {
    
    'Cookie': 'sessionId={}'.format(session), 'Referer': ref}

	cx = 'WEIXIN-SHARE'

    res = requests.post(req_url, json={
    
    "Type": cx}, headers=header, verify=False)
    assert res.json()['success'] is True
    Log.info('助力help - {}'.format(res.json()))

本文链接:https://blog.csdn.net/zyooooxie/article/details/118603676

交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie

おすすめ

転載: blog.csdn.net/zyooooxie/article/details/118603676
おすすめ