• APP自动化_操作微信小程序实现自动化_多终端并行


    APP自动化_混合App自动化理论相关

    原理:本质是混合app,应该用切入webview的方法去自动化web页面。
    现状:目前由于技术原因无法切到webview中做自动化。
    方案:采用原生app自动方法来自动化小程序。
    
    • 1
    • 2
    • 3

    1、mini_config.py 配置文件内容

    desired_caps = {
        # 平台
        "platformName": "Android",
        "platformVersion": "11",
        "deviceName": "Redmi Note 11 Pro",
        # 目标app信息--微信--从微信打开小程序
        'appActivity': '.ui.LauncherUI',
        'appPackage': 'com.tencent.mm',
        # 设置命令超时时间
        'newCommandTimeout': 6000,
        # 确保自动化之后不重置app
        'noReset': True,
        # 底层驱动
        'automationName': 'UiAutomator2',
        # 如果不想每次都安装UI2驱动,可以这么设置
        # 'skipServerInstallation':True,
        # 修改手机的输入法,UI2不需要设置
        # 'unicodeKeyboard':True,
        # 自动化结束之后将输入法还原-概率性
        # 'resetKeyboard':True
    }
    
    xiaomi_caps = desired_caps.copy()
    huawei_caps = desired_caps.copy()
    
    xiaomi_caps['platformVersion'] = '11'
    xiaomi_caps['deviceName'] = 'iruov8yt8hifcyjn'
    xiaomi_caps['systemPort'] = '4723'
    
    huawei_caps['platformVersion'] = '11'
    huawei_caps['deviceName'] = 'iruov8yt8hifcyjn'
    huawei_caps['systemPort'] = '8246'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    2、操作微信小程序实现自动化_多终端并行 演示

    import os
    import time
    import pytest
    from appium import webdriver
    from day6_小程序.mini_config import *
    
    
    def open_session(port, caps):
        """
        初始化应用
        :param port:
        :param caps:
        :return:
        """
        global driver
        # 初始化driver对象-用于控制手机-启动被测应用(微信)
        # IP-appium-server所在机器的网络ip,port-监听的端口号,path固定/wd/hub
        driver = webdriver.Remote(f'http://localhost:{port}/wd/hub', caps)
        driver.implicitly_wait(15)
    
    
    def close_session():
        """
        清理应用
        :return:
        """
        global driver
        driver.quit()
    
    
    def swipe_screen(distance):
        """
        滑动屏幕
        向上滑动:滑动距离为负数,反之为正数,起点为屏幕中心点
        :param distance:
        :return:
        """
        size = driver.get_window_size()
        pos_x = size['width'] / 2
        pos_y = size['height'] / 2
        driver.swipe(pos_x, pos_y, pos_x, pos_y + distance)
    
    
    def open_miniprogram(name):
        """
        打开小程序
        :param name:
        :return:
        """
        global driver
        # 先等待目标页面出现
        driver.find_element_by_xpath('//*[@text="微信"]')
        # 下拉
        size = driver.get_window_size()
        distance = size['height'] / 2-10
        # 向下距离为正数
        swipe_screen(distance)
        time.sleep(1)
        # 点击搜索框
        driver.find_element_by_id('com.tencent.mm:id/r8').click()
        # 输入关键字name
        driver.find_element_by_id('com.tencent.mm:id/bhn').send_keys(name)
        # 点击搜索结果
        driver.find_element_by_xpath('//*[@resource-id="app"]//android.view.View[@text="拼多多"]').click()
        time.sleep(1.5)
        # # 点击 ENTER
        # driver.keyevent(66)
        # # 选择第一个结果
        # res = driver.find_elements_by_xpath(
        #     '//*[@resource-id="app"]/android.view.View[2]/android.view.View[2]/android.view.View[1]/android.widget.Button[1]')
        # if res:
        #     res[0].click()
    
    
    def select_items(item):
        """
        挑选商品+提交订单
        :param item:
        :return:
        """
        # 点击 限时秒杀
        driver.find_element_by_xpath('//*[@text="xxx"]').click()
        # 等待进入商品购买页
        element = driver.find_element_by_xpath('//*[@text="限时秒杀"]')
        assert element.text == '限时秒杀'
        # 滑动页面
        driver.implicitly_wait(0.5)
        size = driver.get_window_size()
        distance2 = -(size['height'] / 8)
        while 1:
            # 寻找1个商品
            eles = driver.find_elements_by_xpath(f'//*[@text="{item}"]')
            if eles:
                print('找到目标商品')
                # 点击加号
                driver.find_element_by_xpath(f'//*[@text="{item}"]/../../following-sibling::android.view.View').click()
                break
            swipe_screen(distance2)
        driver.implicitly_wait(15)
        # 检查商品对应购物栏数字
        goods_num = driver.find_element_by_xpath(
            f'//*[@text="{item}"]/../../following-sibling::android.view.View/android.view.View/*/*').text
    
        print('当前商品数量: %s' % goods_num)
    
        # 点击购买
        driver.find_element_by_xpath('//*[@text="购买礼物"]').click()
    
        # 检查进入选择支付方式
        driver.find_element_by_xpath('//*[@text="选择支付方式"]')
    
        # 支付金额查看
        res = driver.find_element_by_xpath('//*[contains(@text,"洗碗布")]').text
        print(res)
    
    
    # # 单设备
    # def setup():
    #     """
    #     给初始化加上启动参数
    #     :return:
    #     """
    #     # 单设备
    #     open_session('4723', xiaomi_caps)
    
    # 多设备
    @pytest.fixture(params=[('4723', xiaomi_caps), ('4725', huawei_caps)])
    def setup(request):
        """
        给初始化加上启动参数
        :return:
        """
        port = request.param[0]
        cpas = request.param[1]
        open_session(port, cpas)
        yield
        teardown()
    
    def teardown():
        """
        清除
        :return:
        """
        close_session()
    
    def test_miniprogram():
        open_miniprogram('拼多多')
        select_items('洗碗布')
    
    
    if __name__ == '__main__':
        # 提前装好pytest-xdist插件: pip install pytest-xdist
        # -n 2表示用两个进程启动测试脚本,因为该测试有两组参数。
        # 单设备
        # pytest.main(['1_手机WEB页面自动化_操作微信小程序实现自动化_多终端并行.py', '-s'])
        # 多设备
        pytest.main(['1_手机WEB页面自动化_操作微信小程序实现自动化_多终端并行.py', '-s', '-n 2', '--alluredir=tmp/my_allure_results'])
        # os.system(f'allure serve tmp/my_allure_results')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
  • 相关阅读:
    Apache Doris (五十四): Doris Join类型 - Bucket Shuffle Join
    OVIS数据集代码解析
    【MySQL】增删查改CURD(基础版)
    实验篇:三层网络,12个节点询问式通信
    寻找二叉树一个节点的后继节点
    使用内网穿透工具实现远程访问本地部署的Odoo企业管理系统
    vuex 中使用了modules,如何在页面中调用actions里面的方法
    wish测评自养号,卖家怎么快速出单?
    java项目 基于springboot儿童福利院申请管理系统
    ArcGIS 10.3安装教程!
  • 原文地址:https://blog.csdn.net/weixin_44801980/article/details/125471222