1、打開微信在任意窗口輸入一下內容
針對微信版本在7.0以下,可以只需要在任意聊天窗口輸入debugx5.qq.com即可打開。
針對微信版本在7.0+,微信有對H5開關做了調整,需要在聊天窗口輸入如下:
2、手機通過usb連接到電腦,打開USB調試模式,通過adb devices命令檢測到設備
3、進入到小程序中
這點相信大家都知道如何進入小程序。
4、使用uc - devtools檢測webview頁面
顯示的webview版本是57.xxx,這里就是微信X5內核的版本,不是android System webview版本,同樣也可以點擊inspect查看頁面元素
可以看到當前就是一個html頁面,我們可以采取常規web元素定位方式來定位元素
注意事項:
(1)頁面空白加載不出來,這是因為Google的inspect工具需要訪問到牆外的網站,所以需要有FQ工具或者VPN方式;
(2)微信在新版本中對小程序調試入口加上了限制:在微信主窗口下來打開小程序,在chrome中通過inspect工具是檢測不到小程序對應的url入口的;
解決方案:
在微信->發現->搜一搜搜索小程序,即可發現在inspect工具中可以將對應url顯示出來。
(3)還有一點需要注意,如果點擊右上角關閉了小程序之后,一定要記得從后台清理下對應的小程序進程(關閉之后小程序還在后台運行),再次點擊重啟小程序。
5、默認appium-desktop安裝之后里面自帶的chromedriver不是2.26的,需要手動去官網下載對應版本的chromedriver:
http://chromedriver.storage.googleapis.com/index.html
將其放到appium的chromedriver對應目錄中,我的是:
C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\appium-chromedriver
6、微信/QQ有很多進程,我們要確定當前web頁面是位於哪個進程中,使用adb命令:
- adb shell dumpsys activity top | findstr ACTIVITY
- adb shell ps 進程號
確定我們當前微信的頁面運行在com.tencent.mm:toolsmp中
7、在desiredCapabilities中指定:
desired_caps={}
// 支持X5內核應用自動化配置
desired_caps["recreateChromeDriverSessions"]= True
desired_caps["platformName"]="Android"
desired_caps["platformversion"]="5.1"
desired_caps["deviceName"]="Android Emulator"
desired_caps["appPackage"]="com.tencent.mm"
desired_caps["appActivity"]="com.tencent.mm.ui.LauncherUI"
desired_caps["noReset"]=True
// ChromeOptions使用來定制啟動選項,因為在appium中切換context識別webview的時候,
// 把com.tencent.mm:toolsmp的webview識別成com.tencent.mm的webview.
// 所以為了避免這個問題,加上androidProcess: com.tencent.mm:toolsmp
desired_caps["chromeOptions"] = {"androidProcess":"com.tencent.mm:tools"}
// 初始化會默認將chrome瀏覽器打開,需要將Browser置為空
desired_caps["browserName"]=""
driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
8、根據chrome的inspect工具得到web元素的信息
測試流程如下:
選擇測試小程序->點擊老師->滑動老師列表找到“歪歪”
"老師"對應的xpath://*[@id="js-tab-bar"]/li[3]
"歪歪"對應的xpath://em[text()='歪歪']
測試代碼對應如下:
time.sleep(5) #點擊微信中底部“發現”按鈕 driver.find_element_by_android_uiautomator('new UiSelector().text(\"發現\")').click() #點擊發現里面搜一搜 driver.find_element_by_android_uiautomator("new UiSelector().text(\"搜一搜\")").click() time.sleep(5) #點擊搜索框 driver.find_element_by_id("com.tencent.mm:id/jd").click() driver.find_element_by_id("com.tencent.mm:id/jd").sendKeys("檸檬班軟件測試") time.sleep(5) // 點擊搜索結果中的檸檬班軟件測試(采用adb命令坐標點擊的方式) os.system("adb shell input tap 281 205") time.sleep(5) // 點擊檸檬班軟件測試小程序 os.system("adb shell input tap 364 470") // 等待小程序加載完成 time.sleep(10) // 獲取到所有的contexts print("所有的contexts:" + driver.contexts); // 切換到小程序webview對應的context中 driver.switch_to.context("WEBVIEW_com.tencent.mm:toolsmp") time.sleep(5) // 獲取到所有的handles windowHandles = driver.window_handles print("所有的windowsHandles" + windowHandles) // 遍歷所有的handles,找到當前頁面所在的handle:如果pageSource有包含你想要的元素,就是所要找的handle // 小程序的頁面來回切換也需要:遍歷所有的handles,切換到元素所在的handle for handle in windowHandles: print("切換到對應的windowHandle:" + windowHandle) driver.switch_to.window(handle) time.sleep(3) if driver.page_source.find("檸檬班")!=-1: break // 點擊老師 WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.xpath("//*[@id=\"js-tab-bar\"]/li[3]")))).click() WebDriverWait(driver,20).until(EC.presence_of_element_located((By.xpath("//em[text()='歪歪']")))) // 通過js滾動到指定的元素 (這個元素已經在文檔中間已經存在,但是還是不可見的) ele=driver.find_element_by_xpath("//em[text()='歪歪']") time.sleep(3) // 滑動到上面定位到的元素的位置 driver.execute_script("arguments[0].scrollIntoViewIfNeeded(true);", ele); time.sleep(3)
到目前為止,微信小程序自動化代碼實現就完成了,后續按照自己的需求編寫代碼完成。
參考文章:https://blog.csdn.net/qq_38741986/article/details/99702537