• APP自动化测试-9.Appium设备交互与模拟器控制


    APP自动化测试-9.Appium设备交互与模拟器控制



    前言


    一、Appium设备交互常用API

    1. 模拟测试过程中来电和短信

    • 电话模拟:make_gsm_call
    • 短信模拟:send_sms

    代码示例:

    from time import sleep
    
    import pytest
    from appium import webdriver
    from appium.webdriver.common.appiumby import AppiumBy
    from appium.webdriver.extensions.android.gsm import GsmCallActions
    from selenium.webdriver.common.by import By
    
    
    class TestHybrid:
    
        def setup(self):
        	# 路径中test为用户名
            desired_caps = {
                "platformName": "android",
                "appium:deviceName": "emulator-5554",
                "browserName": "chrome",
                "chromedriverExecutableDir": "/Users/test/tools/chromedriverdir",
                "chromedriverChromeMappingFile": "/Users/test/PycharmProjects/test_appium/testcase/mapping.json"
            }
    
            self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    
            # 设置隐式等待
            self.driver.implicitly_wait(10)
    
        def teardown(self):
            self.driver.quit()
    
        def test_devices(self):
            self.driver.get("http://m.baidu.com")
            self.driver.make_gsm_call('13020997158', GsmCallActions.CALL)
            self.driver.send_sms('13120993123', 'hello appium test message')
    
            if __name__ == 'main':
                pytest.main()
    
    • 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

    2. 模拟网络切换

    网络状态切换:driver.set_network_connection

    参数:0表示网络断开,1表示飞行模式,2表示仅wifi,4表示仅数据连接,6表示wifi和数据都开启

    代码示例:

    self.driver.set_network_connection(1)
    sleep(3)
    self.driver.set_network_connection(4)
    sleep(3)
    
    • 1
    • 2
    • 3
    • 4

    3. 测试过程中截图

    截图方法:get_screenshot_as_file

    参数中传入文件的路径及文件名即可,示例如下:

    self.driver.get_screenshot_as_file('./screenshot/img.png')
    
    • 1

    完整代码如下:

    
    from time import sleep
    
    import pytest
    from appium import webdriver
    from appium.webdriver.common.appiumby import AppiumBy
    from appium.webdriver.extensions.android.gsm import GsmCallActions
    from selenium.webdriver.common.by import By
    
    
    class TestHybrid:
    
        def setup(self):
            desired_caps = {
                "platformName": "android",
                "appium:deviceName": "emulator-5554",
                "browserName": "chrome",
                "chromedriverExecutableDir": "/Users/gaozeyu/tools/chromedriverdir",
                "chromedriverChromeMappingFile": "/Users/gaozeyu/PycharmProjects/test_appium/testcase/mapping.json"
            }
    
            self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    
            # 设置隐式等待
            self.driver.implicitly_wait(10)
    
        def teardown(self):
            self.driver.quit()
    
        def test_devices(self):
    
            self.driver.get("http://m.baidu.com")
    
            self.driver.make_gsm_call('13020997158', GsmCallActions.CALL)
            self.driver.send_sms('13120993123', 'hello appium test message')
    
            self.driver.get_screenshot_as_file('./screenshot/img.png')
    
            self.driver.set_network_connection(1)
            sleep(3)
            self.driver.set_network_connection(4)
            sleep(3)
    
            if __name__ == 'main':
                pytest.main()
    
    • 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

    二、Android studio模拟器控制

    mac上面使用emulator直接启动创建的模拟器

    • Android studio创建模拟器
    • mac的terminal窗口使用命令查看创建的模拟器:emulator -list-avds
    • 使用emulator命令启动模拟器:emulator @Pixel_5_API_30

    appium脚本中启动参数disired_caps中添加模拟器

    avd": "Pixel_5_API_30"
    
    • 1

    注:该启动项仅适用于emulator创建的模拟器,其他第三方创建的模拟器不使用。


  • 相关阅读:
    MySQL数据库中text类型的数据使用Mybatis自动生成之后,返回值为空
    关于ABAP选择屏幕部分,定义框架与不定义框架两种写法
    XML 外部实体 (XXE) 漏洞及其修复方法
    英文PDF怎么翻译成中文?两分钟让你学会翻译PDF
    【k8s】kubeadm安装k8s集群
    Maven依赖引入的优先机制
    2023-09-23 Windows系统rust开发环境配置真经
    获取keystore中的公钥模数及md5
    【新书推荐】MongoDB Performance Tuning
    电脑如何打开软键盘,教大家Win10如何打开软键盘的方法
  • 原文地址:https://blog.csdn.net/weixin_42517691/article/details/126268945