環境配置
- 下載最新的Appium , . 我用的是appium-desktop-setup-1.8.0.exe;
- 下載Android Studio ;
- 安裝java jdk 1.8;
- 安裝Python client library ,我使用的是python2.7
- 開啟Android Virtual Device(AVD)Manager,創建一個虛擬器;
- 啟動Appium Server console;
第一個測試實例
- 獲取要測試的.apk ,將apk 放在某個文件夾下,e.g.($Directory_Of_My_Choice\apps\Chess Free.apk );
-
獲取AndroidManifest.xml的信息 ,用Android Studio/Build/Analyze APK 選擇你要測試的apk,查看AndroidManifest.xml的內容;** 獲取AndroidManifest.xml的信息**,用Android Studio/Build/Analyze APK 選擇你要測試的apk,查看AndroidManifest.xml的內容;
- 編寫.py測試腳本,腳本文件放在apk的上級目錄 ,e.g.(e.g.($Directory_Of_My_Choice\apps)
"""
Qxf2: Example script to run one test against the Chess Free app using Appium
The test will:
- launch the app
- click the 'PLAY!' button
"""
import os
import unittest
from appium import webdriver
from time import sleep
class ChessAndroidTests(unittest.TestCase):
"Class to run tests against the Chess Free app"
def setUp(self):
"Setup for the test"
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '8.0'
desired_caps['deviceName'] = 'Pixel'
# Returns abs path relative to this file and not cwd
desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'apps/Chess Free.apk'))
desired_caps['appPackage'] = 'uk.co.aifactory.chessfree'
desired_caps['appActivity'] = '.ChessFreeActivity'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
def tearDown(self):
"Tear down the test"
self.driver.quit()
def test_single_player_mode(self):
"Test the Chess app launches correctly and click on Play button"
element = self.driver.find_element_by_id("uk.co.aifactory.chessfree:id/ButtonPlay")
element.click()
sleep(5)
#---START OF SCRIPT
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(ChessAndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
- 執行腳本
- 查看測試結果
總結:
在編寫腳本的時候,需要用到元素定位。可以使用appium自帶的inspector
參數配置如下圖