之前用过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()