上回我們說到,手機和appium已經連接成功。
下面我們開始寫代碼來進行測試。
運行代碼前,先要安裝python 庫
pip install Appium-Python-Client
然後獲取apk信息、工具在sdk目錄下,build-tools對應版本下面。
命令:aapt.exe dump badging (APK的路徑)
獲取到包名,等信息之後,將包名填寫到appium桌面版當中去。appium桌面版我稍後將資源上傳。
大致內容如下:
直接點start server 啓動
啓動後,點擊放大鏡,填寫鏈接內容,大致界面如下圖。添加好APP信息
{ "platformName": "Android", #固定 "platformVersion": "9", #android版本 "deviceName": "xxxxxx你自己的手機唯一識別碼,adb可以看", "appPackage": "bin.mt.plus", #package 上面獲取到的app信息裏面有 "appActivity": "bin.mt.plus.Main", # 上面獲取到的app 第二個內容 "automationName": "uiautomator2", #默認 "unicodeKeyboard": true, #用unicode編碼發送字符串 "resetKeyboard": true, # 取消鍵盤 "noReset": true # 應用信息不重置 }
點擊Start Session啓動
啓動後會打卡app,然後獲取到app對應頁面上的信息。
剩下就是寫腳本的事情了。獲取到Xpath。寫腳本
#coding=utf-8
from appium import webdriver
import time
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '9'
desired_caps['deviceName'] = 'xxxxxxxxx'
desired_caps['appPackage'] = 'bin.mt.plus'
desired_caps['appActivity'] = 'bin.mt.plus.Main'
desired_caps['automationName'] = 'uiautomator2'
desired_caps['unicodeKeyboard'] = 'True'
desired_caps['resetKeyboard'] = 'True'
desired_caps['noReset'] = 'True'
# 啓動app
drivers = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 定位到功能按鍵,並點擊
driver.find_element_by_xpath(".//*[@text='功能']").click()
# 關閉退出
drivers.quit()
上面只是一個簡單的例子。如何運行以及使用。
今天就講到這裏。感謝各位閱讀。