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

前言

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

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

開啓微信debug模式

1.以微信爲例,webview的版本號能夠用chrome瀏覽器查看到,因爲微信用的是x5內核,跟其餘app不太同樣,這裏須要先開啓微信的debug模式web

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

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'}app

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):ui

  • 點微信首頁搜索按鈕
  • 輸入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

相關文章
相關標籤/搜索