appium+python自動化42-微信公眾號


前言

本篇介紹如何在微信公眾號上自動化測試,以操作我的個人公眾號:yoyoketang為例,沒關注的,先微信關注了,再跟着操作

環境准備:
python 3.6
appium 1.7以上版本
微信6.6.6
微信里面webview 57.0
電腦上chrome版本 66.0
appium-chromedriver路徑下chromedriver 2.28

開啟微信debug模式

1.以微信為例,webview的版本號可以用chrome瀏覽器查看到,由於微信用的是x5內核,跟其他app不太一樣,這里需要先開啟微信的debug模式

開啟微信debug模式:在微信聊天界面輸入:debugx5.qq.com,如何勾選"打開TBS內核Inspector調試功能

2.查看微信里面webview版本,直接在電腦chrome瀏覽器輸入:chrome://inspect/#devices
再打開微信的公眾號頁面,刷新瀏覽器頁面,就會出現webview版本號57.0

ChromeOptions

1.在appium中context的切換時,識別webview的時候, 把com.tencent.mm:tools的webview識別成com.tencent.mm的webview. 從而導致context切換失敗。
所有這里必須加上這個參數ChromeOptions,這個是關鍵。

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

2.appium1.7以后的版本支持Uiautomator2了,為了保險一點,最好加上這個,用Uiautomator2執行

'automationName': 'Uiautomator2'

from appium import webdriver
import time

# 作者:上海-悠悠 QQ交流群:512200893

desired_caps = {
                'platformName': 'Android',
                'platformVersion': '7.0',
                'deviceName': 'A5RNW18316011440',
                'appPackage': 'com.tencent.mm',
                'appActivity': '.ui.LauncherUI',
                'automationName': 'Uiautomator2',
                'unicodeKeyboard': True,
                'resetKeyboard': True,
                'noReset': True,
                'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
                }

操作公眾號

1.操作步驟如下(前提要先關注微信公眾號:yoyoketang):

  • 點微信首頁搜索按鈕
  • 輸入yoyoketang內容搜索
  • 點開公眾號
  • 點公眾號菜單-精品分類

2.代碼實現

切換webview

# 切換到webview
time.sleep(2)
print(driver.contexts)
driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')

打印結果:

['NATIVE_APP', 'WEBVIEW_com.tencent.mm:tools']

參考代碼:

from appium import webdriver
import time

# 作者:上海-悠悠 QQ交流群:512200893

desired_caps = {
                'platformName': 'Android',
                'platformVersion': '7.0',
                'deviceName': 'A5RNW18316011440',
                'appPackage': 'com.tencent.mm',
                'appActivity': '.ui.LauncherUI',
                'automationName': 'Uiautomator2',
                'unicodeKeyboard': True,
                'resetKeyboard': True,
                'noReset': True,
                'chromeOptions': {'androidProcess': 'com.tencent.mm:tools'}
                }

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(10)

# 作者:上海-悠悠 QQ交流群:512200893

# 點微信首頁搜索按鈕
driver.find_element_by_accessibility_id("搜索").click()
# 輸入內容搜索
time.sleep(3)
driver.find_element_by_id('com.tencent.mm:id/hx').send_keys("yoyoketang")
# 點開公眾號
time.sleep(3)
driver.find_element_by_id('com.tencent.mm:id/l7').click()

# 點公眾號菜單-精品分類
time.sleep(3)
driver.find_elements_by_id('com.tencent.mm:id/aaq')[0].click()


# 切換到webview
time.sleep(2)
print(driver.contexts)
driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')

# 點webview上元素 目前有個問題會報找不到元素
driver.find_element_by_xpath(".//*[@id='namespace_1']/div[1]/div/div[2]").click()
time.sleep(2)
driver.quit()

在學習過程中有遇到疑問的,可以appium+python QQ群交流:330467341


免責聲明!

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



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