appium+python 微信小程序的自動化


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 微信小程序的自動化

版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接: https://blog.csdn.net/xgh1951/article/details/86076582

前言

最近微信的小程序越來越多了,隨之帶來的問題是:小程序如何做自動化測試?
今天寫一篇小程序該如何做自動化測試,如何定位,具體以膜拜為例子

webview進程

1.小程序和微信公眾號還不太一樣,基本思路差不多,先配置:chromeOptions

'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand0'}

2.androidProcess進程可以通過adb shell去查看,先點開摩拜小程序,然后進adb shell

  進入shell后輸入:dumpsys activity top | grep ACTIVITY

  1.  
    C:\Users\admin>adb shell
  2.  
    HWBND- H:/ $ dumpsys activity top | grep ACTIVITY
  3.  
    ACTIVITY com.tencent.mm/.plugin.appbrand.ui.AppBrandUI d0f2ff4 pid=7332

此時可以看到pid

然后輸入:ps 7332

  1.  
    HWBND-H:/ $ ps 7332
  2.  
    USER PID PPID VSIZE RSS WCHAN PC NAME
  3.  
    u0_a119 7332 495 2706272 283720 0 0000000000 S com.tencent.mm:appbrand0
  4.  
    HWBND-H:/ $

3.com.tencent.mm:appbrand0 這個就是我們要找的東西

 

正式開始:

1. 下拉為微信聊天頁面,出現摩拜小程序(顯示最近使用的哦,你的可能是別的小程序)

怎樣實現下拉滑動呢?使用driver的swipe方法,我這里用的是我封裝好的滑動方法

  1.  
    #向下滑動
  2.  
    def swipe_down(driver,t=500,n=1):
  3.  
    s = driver.get_window_size()
  4.  
    x1 = s[ 'width'] * 0.5 # x坐標
  5.  
    y1 = s[ 'height'] * 0.25 # 起點y坐標
  6.  
    y2 = s[ 'height'] * 0.75 # 終點y坐標
  7.  
    for i in range(n):
  8.  
    driver.swipe(x1,y1,x1,y2,t)

 2. 點擊摩拜小程序圖標

  1.  
    # 點擊膜拜單車
  2.  
    driver.find_element_by_id( 'com.tencent.mm:id/ij').click()
  3.  
    time.sleep( 4)
  4.  
    print(driver.contexts)

3.點擊右下角的小頭像

   由於這個頁面是view.view屬性所以不好用常用定位方法定位,只好用坐標來定位了!

  1.  
    # tap觸摸右下角那個人頭坐標
  2.  
    driver.tap([( 972, 1613), (1034, 1622)], 1000) #tap的點必須是tuple類型,一個點是一個tuple
  3.  
    time.sleep( 5)
  4.  
    print( '進入我的頁面')

4.點擊錢包,這里也是利用坐標定位

  1.  
    # 點擊我的錢包
  2.  
    driver.tap([( 267, 907)], 500)
  3.  
    time.sleep( 4)
  4.  
    print( '進入錢包')

5.點擊余額,這里利用xpath定位

  1.  
    # 點擊余額
  2.  
    driver.find_element_by_xpath( '//*[@text="余額"]').click()
  3.  
    time.sleep( 4)

 6.點擊充值,利用xpath定位

  1.  
    # 點擊充值
  2.  
    driver.find_element_by_xpath( '//*[@text="充值"]').click()
  3.  
    time.sleep( 2)

 

我就不充值了,因為太窮,這里基本就是一個進入小程序完成充值過程的一個自動化操作了,下面是完整的代碼: 

  1.  
    #coding:utf-8
  2.  
    from appium import webdriver
  3.  
    import time,os
  4.  
    from common.My_swipe import swipe_down
  5.  
     
  6.  
     
  7.  
    desired_caps = {
  8.  
    'platformName': 'Android',
  9.  
    'platformVersion': '8.0',
  10.  
    'deviceName': '740dc3d1',
  11.  
    'appPackage': 'com.tencent.mm',
  12.  
    'appActivity': '.ui.LauncherUI',
  13.  
    'automationName': 'Uiautomator2',
  14.  
    # 'unicodeKeyboard': True,
  15.  
    # 'resetKeyboard': True,
  16.  
    'noReset': True,
  17.  
    'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand0'}
  18.  
    }
  19.  
    driver = webdriver.Remote( r'http://127.0.0.1:4723/wd/hub', desired_caps)
  20.  
    time.sleep( 3)
  21.  
    # 向下滑動
  22.  
    swipe_down(driver)
  23.  
    time.sleep( 2)
  24.  
     
  25.  
    # 點擊膜拜單車
  26.  
    driver.find_element_by_id( 'com.tencent.mm:id/ij').click()
  27.  
    time.sleep( 4)
  28.  
    print(driver.contexts)
  29.  
     
  30.  
    # tap觸摸右下角那個人頭坐標
  31.  
    driver.tap([( 972, 1613), (1034, 1622)], 1000) #tap的點必須是tuple類型,一個點是一個tuple
  32.  
    time.sleep( 5)
  33.  
    print( '進入我的頁面')
  34.  
    # 點擊我的錢包
  35.  
    driver.tap([( 267, 907)], 500)
  36.  
    time.sleep( 4)
  37.  
    print( '進入錢包')
  38.  
    # 點擊余額
  39.  
    driver.find_element_by_xpath( '//*[@text="余額"]').click()
  40.  
    time.sleep( 4)
  41.  
     
  42.  
    # 點擊充值
  43.  
    driver.find_element_by_xpath( '//*[@text="充值"]').click()
  44.  
    time.sleep( 2)
  45.  
    #接下來就是摩拜的充值頁面了,由於我太窮充不起,所以就介紹到這里

 

總結:其實這里只是介紹一個小程序自動化的大概思路吧,有疑問的可以加我qq 970185127   ,未完待續.....

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM