sudo kill -9 $(lsof -i:8889 -t)
mitmweb -p 8889 -s addons.py
mitmdump -q -p 8889 -s addons.py
http://appium.io/docs/en/writing-running-appium/caps/
adb logcat -v time | grep `adb shell ps | grep com.tencent.mm.appbrand0 | cut -c10-15` >/home/androidlog.txt
uiautomatorviewer
!!!! WARNING: You are on OS X 10.11 El Capitan or greater, you may need to add the
!!!! WARNING: `--unsafe-perm=true` flag when running `npm install`
!!!! WARNING: or else it will fail.
===
安裝macna問題
npm i -g macaca-ios --unsafe-perm=true
appium
appium+python 微信小程序的自動化
前言
最近微信的小程序越來越多了,隨之帶來的問題是:小程序如何做自動化測試?
今天寫一篇小程序該如何做自動化測試,如何定位,具體以膜拜為例子
webview進程
1.小程序和微信公眾號還不太一樣,基本思路差不多,先配置:chromeOptions
'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand0'}
2.androidProcess進程可以通過adb shell去查看,先點開摩拜小程序,然后進adb shell
進入shell后輸入:dumpsys activity top | grep ACTIVITY
-
C:\Users\admin>adb shell
-
HWBND- H:/ $ dumpsys activity top | grep ACTIVITY
-
ACTIVITY com.tencent.mm/.plugin.appbrand.ui.AppBrandUI d0f2ff4 pid=7332
此時可以看到pid
然后輸入:ps 7332
-
HWBND-H:/ $ ps 7332
-
USER PID PPID VSIZE RSS WCHAN PC NAME
-
u0_a119 7332 495 2706272 283720 0 0000000000 S com.tencent.mm:appbrand0
-
HWBND-H:/ $
3.com.tencent.mm:appbrand0 這個就是我們要找的東西
正式開始:
1. 下拉為微信聊天頁面,出現摩拜小程序(顯示最近使用的哦,你的可能是別的小程序)
怎樣實現下拉滑動呢?使用driver的swipe方法,我這里用的是我封裝好的滑動方法
-
#向下滑動
-
def swipe_down(driver,t=500,n=1):
-
s = driver.get_window_size()
-
x1 = s[ 'width'] * 0.5 # x坐標
-
y1 = s[ 'height'] * 0.25 # 起點y坐標
-
y2 = s[ 'height'] * 0.75 # 終點y坐標
-
for i in range(n):
-
driver.swipe(x1,y1,x1,y2,t)
2. 點擊摩拜小程序圖標
-
# 點擊膜拜單車
-
driver.find_element_by_id( 'com.tencent.mm:id/ij').click()
-
time.sleep( 4)
-
print(driver.contexts)
3.點擊右下角的小頭像
由於這個頁面是view.view屬性所以不好用常用定位方法定位,只好用坐標來定位了!
-
# tap觸摸右下角那個人頭坐標
-
driver.tap([( 972, 1613), (1034, 1622)], 1000) #tap的點必須是tuple類型,一個點是一個tuple
-
time.sleep( 5)
-
print( '進入我的頁面')
4.點擊錢包,這里也是利用坐標定位
-
# 點擊我的錢包
-
driver.tap([( 267, 907)], 500)
-
time.sleep( 4)
-
print( '進入錢包')
5.點擊余額,這里利用xpath定位
-
# 點擊余額
-
driver.find_element_by_xpath( '//*[@text="余額"]').click()
-
time.sleep( 4)
6.點擊充值,利用xpath定位
-
# 點擊充值
-
driver.find_element_by_xpath( '//*[@text="充值"]').click()
-
time.sleep( 2)
我就不充值了,因為太窮,這里基本就是一個進入小程序完成充值過程的一個自動化操作了,下面是完整的代碼:
-
#coding:utf-8
-
from appium import webdriver
-
import time,os
-
from common.My_swipe import swipe_down
-
-
-
desired_caps = {
-
'platformName': 'Android',
-
'platformVersion': '8.0',
-
'deviceName': '740dc3d1',
-
'appPackage': 'com.tencent.mm',
-
'appActivity': '.ui.LauncherUI',
-
'automationName': 'Uiautomator2',
-
# 'unicodeKeyboard': True,
-
# 'resetKeyboard': True,
-
'noReset': True,
-
'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand0'}
-
}
-
driver = webdriver.Remote( r'http://127.0.0.1:4723/wd/hub', desired_caps)
-
time.sleep( 3)
-
# 向下滑動
-
swipe_down(driver)
-
time.sleep( 2)
-
-
# 點擊膜拜單車
-
driver.find_element_by_id( 'com.tencent.mm:id/ij').click()
-
time.sleep( 4)
-
print(driver.contexts)
-
-
# tap觸摸右下角那個人頭坐標
-
driver.tap([( 972, 1613), (1034, 1622)], 1000) #tap的點必須是tuple類型,一個點是一個tuple
-
time.sleep( 5)
-
print( '進入我的頁面')
-
# 點擊我的錢包
-
driver.tap([( 267, 907)], 500)
-
time.sleep( 4)
-
print( '進入錢包')
-
# 點擊余額
-
driver.find_element_by_xpath( '//*[@text="余額"]').click()
-
time.sleep( 4)
-
-
# 點擊充值
-
driver.find_element_by_xpath( '//*[@text="充值"]').click()
-
time.sleep( 2)
-
#接下來就是摩拜的充值頁面了,由於我太窮充不起,所以就介紹到這里
總結:其實這里只是介紹一個小程序自動化的大概思路吧,有疑問的可以加我qq 970185127 ,未完待續.....