1. 程式人生 > >使用 Appium 測試微信小程序 Webview

使用 Appium 測試微信小程序 Webview

page pytho 調試 runtime med ppp active devices lee

打開調試功能

通過微信打開debugx5.qq.com,或者直接掃下面二維碼

技術分享圖片


勾選【打開TBS內核Inspector調試功能】

技術分享圖片

Chrome查看頁面元素

手機連接電腦,查看是否連接成功。如下展示設備號則為連接成功

技術分享圖片

進入任意小程序,以【X東】為例,在Chrome瀏覽器訪問chrome://inspect/#devices

技術分享圖片


點擊inspact,即可查看頁面元素

技術分享圖片

小程序的進程

微信有很多的進程,每一個小程序都運行在不同的進程中。
進入【X東】後,看下當前運行在哪個進程中

技術分享圖片


我們可以看到,當前的小程序運行在com.tencent.mm:appbrand0中,記住這個進程,記住啦

Appium編寫測試用例

最關鍵的一點,需要添加androidProcess這一項,也就是我們上面的com.tencent.mm:appbrand0
我用python寫一個簡單的demo

desired_caps = {
            ‘platformName‘: ‘Android‘,
            ‘fastReset‘: ‘false‘,
            ‘deviceName‘: ‘XXXXXX‘,
            ‘appPackage‘: ‘com.tencent.mm‘,
            ‘appActivity‘: ‘.ui.LauncherUI‘,
            ‘fullReset‘: ‘false‘,
            ‘unicodeKeyboard‘: ‘True‘,
            ‘resetKeyboard‘: ‘True‘,
            ‘chromeOptions‘: {
                ‘androidProcess‘: ‘com.tencent.mm:appbrand0‘
                }
            }
driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps)
driver.find_element_by_name("發現").click()
driver.find_element_by_name("小程序").click()
driver.find_element_by_name("X東購物").click()
driver.switch_to.context(‘WEBVIEW_com.tencent.mm:appbrand0‘)
time.sleep(5)
print(driver.page_source)

切換Webview失敗

我遇到過一種切換Webview失敗的情況,下面貼上appium的報錯日誌。

> info: Chromedriver: Changed state to ‘stopped‘
> error: Chromedriver: Chromedriver exited unexpectedly with code null, signal SIGTERM
> warn: Chromedriver for context WEBVIEW_com.tencent.mm:appbrand1 stopped unexpectedly
> warn: Chromedriver quit unexpectedly, but it wasn‘t the active context, ignoring
> error: Chromedriver: Error: A new session could not be created. (Original error: session not created exception
> from unknown error: Runtime.executionContextCreated has invalid ‘context‘: {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"}
>   (Session info: chrome=57.0.2987.132)
>   (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64))
>     at JWProxy.command$ (lib/proxy.js:133:15)
>     at tryCatch (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:67:40)
>     at GeneratorFunctionPrototype.invoke [as _invoke] (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:315:22)
>     at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:100:21)
>     at GeneratorFunctionPrototype.invoke (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\regenerator\runtime.js:136:37)
>     at bound (domain.js:284:14)
>     at GeneratorFunctionPrototype.runBound (domain.js:297:12)
>     at run (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js:89:39)
>     at D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\es6.promise.js:100:28
>     at flush (D:\Appium\node_modules\appium\node_modules\appium-chromedriver\node_modules\appium-jsonwp-proxy\node_modules\babel-runtime\node_modules\core-js\library\modules\$.microtask.js:17:13)
>     at process._tickDomainCallback (node.js:381:11)
>  { [Error: A new session could not be created. (Original error: session not created exception
> from unknown error: Runtime.executionContextCreated has invalid ‘context‘: {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"}
>   (Session info: chrome=57.0.2987.132)
>   (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64))]
>   status: 33,
>   value: { message: ‘session not created exception\nfrom unknown error: Runtime.executionContextCreated has invalid \‘context\‘: {"auxData":{"frameId":"32496.2","isDefault":true},"id":2,"name":"","origin":"https://servicewechat.com"}\n  (Session info: chrome=57.0.2987.132)\n  (Driver info: chromedriver=2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a),platform=Windows NT 6.1 SP1 x86_64)‘ },

註意最後那一段的chrome和chromedriver的版本

技術分享圖片


查看對應表可知,兩者版本不對應,需要替換chromedriver

技術分享圖片


將Appium的chromedriver替換為相應版本後,即可成功切換。

使用 Appium 測試微信小程序 Webview