Monkey的使用
adb shell monkey -p com.lqr.wechat -v 500 > monkey.log adb shell monkey -p com.lqr.wechat -vvv 5000 > monkey2.log adb shell monkey -p com.lqr.wechat -vvv 5000 --send=1556166765229 --throttle 500 > monkey3.log adb shell monkey -p com.lqr.wechat -vvv 5000 --send=1556166765229 --ignore-crashes > monkey4.log
參數:android
- -p
- -v
- -s
- --throttle
- --ignore-crashes: 忽略崩潰
- --ignore-timeouts: 忽略ANR
- --ignore-security-exceptions: 忽略證書問題
- --kill-process-after-error: Monkey出錯後結束運行
- --monitor-native-crashes: 監控本地崩潰
可經過命令行指定web
- count=5000: 計劃執行5000次
- AllowPackage: 包名
- Event percentages: 事件百分比, 可經過命令行修改
- Switch: Activity(頁面)跳轉
- Sending Touch: 發送動做指令
- Injection Failed #注入(執行)指令失敗, App無反應或不支持
- CRASH: 崩潰
指定事件百分比shell
- –pct-touch:觸摸事件(點擊)
- –pct-motion:動做事件(拖動)
- –pct-trackball:軌跡球事件(移動)
- –pct-nav:基本導航事件:上下左右操做
- –pct-majornav:主導航事件: 確認,菜單,返回鍵
- –pct-syskeys:系統按鍵事件: 如HOME鍵,BACK鍵,撥號鍵,掛斷鍵,音量鍵等
- –pct-appswitch:應用啓動事件
- –pct-anyevent: 任意事件
手動中止Monkey瀏覽器
adb shell ps | grep monkey (查詢進程號) kill 進程號
Appium inspector的使用
- 啓動和配置
- 定位和操做元素
- 錄製
元素定位
- 經過resource_id: find_element_by_id:
- 經過content-desc: find_element_by__accessibility_id
- 經過xpath: 結合屬性/上下級節點/索引定位
- 重複元素+索引定位: l = find_elements_by_... l[1].click()
- 逐級定位: driver.find_element_by_id(...).find_element_by_class_name(...)
使用By及元素定位器 from appium.webdriver.common.mobileby import By微信
driver.find_element(By.ID, "....")網絡
使用uiautomator的原始定位方式
1.經過id定位app
driver.find_element_by_android_uiautomator('new UiSelector().resourceId(「id」)').click()
- 經過text定位
# Text定位 driver.find_element_by_android_uiautomator('new UiSelector().text("textstr")') # 文字包含 driver.find_element_by_android_uiautomator('new UiSelector().textContains(textStr)') # 以哪一個字符開始 driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith(textStr)')
- 經過class屬性定位
driver.find_element_by_android_uiautomator('new UiSelector().className("class屬性")')
- 多屬性定位
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("Custom View")')
可做爲過濾條件的有:ui
- UISelector.text
- UISelector.textContains
- UISelector.textStartsWith
- UiSelector.resourceId
- UISelector.className
- UISelector.textMatches
- UISelector.classNameMatches
- UiSelector.fromParent
- UiSelector.childSelector
- UiSelector.resourceIdMatches
- UiSelector.description
- UiSelector.descriptionStartWith
- UiSelector.descriptionMatches
元素操做
- click()
- send_keys()
- clear()
- is_displayed()/is_enabled()/is_checked()
- get_attribute(): 獲取屬性 文本/狀態/是否可操做等
- get_attribute("text")
- get_attribute("resourceId")
- get_attribute("className")
- set_value(): 設置text值
- text: 元素的文本
- location: 獲取元素座標值
- size: 獲取元素大小
練習spa
noReset: True 1. 打開微信應用 ,判斷是否存在 登陸按鈕 2. 若是已經登陸,登陸按鈕不存在 ,就直接進入登陸頁,打印 通信錄的這些屬性值, resourceid,text,className,enabled,clickable,selected,Displayed c = driver.find...() print(c.get_attribute("clickable")) print(c.is_enabled()/c.is_displayed()/c.is_selected()) 若是登陸按鈕存在 ,打印這個登陸按鈕的上面的屬性值resourceid,text,className,enabled,clickable,selected,Displayed, 並點擊登陸按鈕, 使用set_value())輸入用戶名密碼 判斷登陸按鈕是否可用, 可用的話打印可用,並點擊登陸,判斷是否登陸成功 不可用打印不可用 3. 退出應用
練習提示.net
try: # 嘗試定位登陸按鈕 l = driver.find_element_by_id("") except: # 未定位到 print(「沒有登陸按鈕」) c = driver.find_....() # 定位 通信錄 標籤 print(c.get_attribute('resource-id') # 打印各類屬性 print(c.text) print(c.get_attribute(「class」) print(c.is_enabled()) .... else: # 若是沒有異常(定位到了登陸按鈕) print(l.get_attribute("resource-id") # 打印登陸按鈕的各類屬性 print(l.text) print(l.get_attribute(「class」) print(l.is_enabled()) ... l.click() # 點擊登陸按鈕 driver.find…().set_value("18010181267") # 使用set_value輸入文字 driver.find…().set_value("123456") # 使用set_value輸入密碼 l2 = driver.find..() # 定位 登陸 按鈕 if l2.is_enabled() is True: # 判斷 登陸按鈕 是否可用 print(「可用」) l2.click() # 點擊登陸按鈕 try: # 判斷是否登陸成功, 登陸成功後會有 "通信錄" 這個標籤 c = driver.find_....() # 嘗試定位通信錄這個標籤 except: # 定位不到打印失敗 print(「登陸失敗」) else: # 沒有異常(定位到) 打印成功 print(「登陸成功」) else: print("不可用") # 上面的l2登陸按鈕不可用打印不可用
按鍵操做
- drvier.keyevent()
- driver.press_keycode()
練習
啓動 微信,登陸用戶名和密碼,並點擊登陸 進入 「我」 ,點擊用戶名, 點擊二維碼名片, 截圖並保存到d:\\screenshot\\VCODE.png 導入截圖到手機/sdcard/images/這個路徑下 返回到微信聊天列表頁,並將音量放大兩個分貝 點擊菜單回到手機主屏 退出
屏幕操做
- tap: 觸控(點擊指定座標),支持多點觸控
- swipe: 滑動
- flick: 快滑
- pinch:
- zoom:
- get_screenshot_as_file: 截圖
獲取屏幕寬度
- width = driver.get_window_size()['width']
- height = driver.get_window_size()['height']
練習
封裝四個方法, 向下滑動, 向上滑動, 向左滑動, 向右滑動,
練習2
打開手機設置 滑動查找頁面上含有「關於「文字的 元素 若是找到了點擊,若是沒找到繼續找, 進入關於頁面,查看手機的版本信息 退出設置頁面
文件操做
- driver.pull()
- driver.push()
app操做
autoLaunch=False
- driver.is_app_installed()
- driver.install_app()
- driver.remove_app()
- driver.launch_app()
- driver.start_activity()
- driver.current_activity()
- driver.background_app(3)
- driver.close_app()
網絡狀態
- driver.network_connection
查找已安裝app(無apk包)Package/MainActiviy方法
adb shell logcat | grep cmp=
而後在設備上操做一次app
練習
1.鏈接appium server,不啓動任何app ,(autoLaunch =False) 2.啓動微信的首頁activity( launch_app() ) 3.打開通知欄,而後關閉通知欄(要先打開一個activity) driver.press_keyevent(「4」) 4.用start_activity()啓動本地瀏覽器,判斷當前相機activity是否正確( 獲取current_activity 對比當前的activity print driver.current_activity - .Camera) 5. 截屏screenshot.png 並保存到 d:\tmp\目錄下 6.把當前app放在後臺運行5秒鐘 driver.backGround_app—進入 入到app首頁 7.查看當前的網絡狀態,而且輸出 8. 判斷微信app是否已安裝,已安裝卸載,未安裝執行安裝 driver.is_app_install(「com.sankuai.meituan」) Driver.remove_app(「包名」) Driver.install_app(「路徑/.apk」)