Appium进行APP自动化测试

本文介绍了如何使用Appium进行安卓应用自动化测试,包括步骤1. 安装Appium, 2. 元素识别与Inspector使用, 3. Python代码示例。重点展示了通过XPath和Class Name定位元素并实现登录流程的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   之前用过uiautomator2对安卓App进行自动化测试,本次简单介绍Appium进行App自动化测试。

    Appium具体功能不再介绍,可自行百度。

1、 安装Appium

    首先官网下载对应版本:

    Release v1.22.3-4 · appium/appium-desktop · GitHub

    注意:其他Node.js\JDK\Andoird SDK\Python\Selenium自行安装

2、 元素识别

    元素识别可用Android自带的uiautomatorview,也可用第三方工具weditor,也可用appium的Inspector。随着Appium Desktop升级到1.22.0版本,服务和元素查看器已经分开了,查看元素信息就需要下载Appium Inspector。下载地址:Releases · appium/appium-inspector · GitHub

2.1  首先运行Appium,配置相关信息

    

2.2 启动appium inspector,配置相关信息

     2.3 点击 Start Session打开界面,可实时刷新。

3、 编写代码实现

import time
import unittest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy as By

class InspectTest(unittest.TestCase):
    caps = {}
    caps["platformName"] = "Android"
    caps["appium:platformVers"] = "12"
    caps["appium:deviceName"] = "7d9p45zhytuson6l"
    caps["appium:ensureWebviewsHavePages"] = True
    caps["appium:nativeWebScreenshot"] = True
    caps["appium:newCommandTimeout"] = 3600
    caps["appium:connectHardwareKeyboard"] = True
    caps["appium:appPackage"] = "com.xxxxx.xxxxx"
    caps["appium:appActivity"] = "crc64ad4cc14999bdba0b.MainActivity"

    # 当前Python为3.10,其find_element方法有所改变
    def start_app(self):
        d = webdriver.Remote("http://127.0.0.1:4723/wd/hub", self.caps)
        acc = d.find_element(By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[1]/android.widget.EditText')
        acc.send_keys('username')
        time.sleep(1)
        pw = d.find_element(By.XPATH, '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.widget.ScrollView/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]/android.widget.EditText')
        pw.send_keys('password')
        time.sleep(1)
        loginBtn = d.find_element(by=By.CLASS_NAME, value='android.widget.Button')
        loginBtn.click()
        time.sleep(5)

        backB = d.find_element(By.CLASS_NAME, 'android.widget.Button')
        backB.click()
        time.sleep(1)

        report = d.find_element(By.ANDROID_UIAUTOMATOR, 'new UiSelector().text("Inspection Reports")')
        report.click()
        time.sleep(5)

        d.quit()

    def test01_starts(self):
        self.start_app()


if __name__ == '__main__':
    unittest.main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zljun8210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值