测试对象是微信公众号
贴上脚本代码:

#coding=utf-8
import unittest
from appium import webdriver
#By定义了若干查找控件的方法
from appium.webdriver.mobilecommand import MobileCommand
from selenium.webdriver.common.by import By
from appium.webdriver.common.touch_action import TouchAction
from time import sleep
from appium import webdriver
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class Testsuit(unittest.TestCase):
    def setUp(self):
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '5.0.1'
        desired_caps['deviceName'] = 'note4'
        desired_caps['appPackage'] = 'com.tencent.mm'
        desired_caps['appActivity'] = '.ui.LauncherUI'
        desired_caps['newCommandTimeout'] = '15'
        desired_caps['unicodeKeyboard'] = 'True'
        desired_caps['resetKeyboard'] = 'True'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(time_to_wait="15")

    def tearDown(self):
        self.driver.quit()

    def test_OpenYoWe(self):
        self.driver.find_element(by=By.NAME, value="通讯录").click()
        self.driver.find_element(by=By.NAME, value="公众号").click()
        self.driver.find_element(by=By.NAME, value="测试").click()
        self.driver.find_element(by=By.NAME, value="页面").click()#点击后进入公众号的页面
        print self.driver.contexts#打印WebView页面
        sleep(5)
        self.driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {"name": "WEBVIEW_com.tencent.mm:tools"})#切换到对应的WebView页面
        self.driver.find_element_by_xpath("html/body/div/div[1]/div/ul[2]/li[1]/div[2]/div[2]/div/button").click()#查找WebView元素

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(Testsuit)
    unittest.TextTestRunner(verbosity=2).run(suite)

这是运行后的 Log

> info: [debug] Getting a list of available webviews
> info: [debug] executing cmd: D:\sdk\platform-tools\adb.exe -s 127.0.0.1:62001 shell "cat /proc/net/unix"
> info: [debug] WEBVIEW_2949 mapped to pid 2949
> info: [debug] Getting process name for webview
> info: [debug] executing cmd: D:\sdk\platform-tools\adb.exe -s 127.0.0.1:62001 shell "ps"
> info: [debug] Parsed pid: 2949 pkg: com.tencent.mm:tools
> info: [debug] from: u0_a47,2949,148,1688764,158084,ffffffff,b7724251,R,com.tencent.mm:tools
> info: [debug] returning process name: com.tencent.mm:tools
> info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.tencent.mm:tools
> info: [debug] ["WEBVIEW_com.tencent.mm:tools"]
> info: [debug] Available contexts: NATIVE_APP,WEBVIEW_com.tencent.mm:tools
> info: [debug] Connecting to chrome-backed webview
> info: Chromedriver: Changed state to 'starting'
> info: Chromedriver: Set chromedriver binary as: D:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
> info: Chromedriver: Killing any old chromedrivers, running: FOR /F "usebackq tokens=5" %a in (`netstat -nao ^| findstr /R /C:"9515 "`) do (FOR /F "usebackq" %b in (`TASKLIST /FI "PID eq %a" ^| findstr /I chromedriver.exe`) do (IF NOT %b=="" TASKKILL /F /PID %a))
> info: Chromedriver: Successfully cleaned up old chromedrivers
> info: Chromedriver: Spawning chromedriver with: D:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=9515
> info: Chromedriver: [STDOUT] Starting ChromeDriver 2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067) on port 9515
> Only local connections are allowed.
> info: JSONWP Proxy: Proxying [GET /status] to [GET http://127.0.0.1:9515/wd/hub/status] with no body
> info: JSONWP Proxy: Got response with status 200: {"sessionId":"","status":0,"value":{"build":{"version":"alpha"},"os":{"arch":"x86_64","name":"Windows NT","version":"6.1 SP1"}}}
> info: JSONWP Proxy: Proxying [POST /session] to [POST http://127.0.0.1:9515/wd/hub/session] with body: {"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.tencent.mm","androidUseRunningApp":true,"androidDeviceSerial":"127.0.0.1:62001"}}}
> info: [debug] Didn't get a new command in 15 secs, shutting down...
> info: Shutting down appium session
> info: [debug] Pressing the HOME button

提问:

info: Chromedriver: Changed state to 'starting'可以看出已经切换到 WebView 页面去了
但是后面没有执行 self.driver.find_element_by_xpath("html/body/div/div[1]/div/ul[2]/li[1]/div[2]/div[2]/div/button").click()# 查找 WebView 元素
想请问下 Appium 切换到 WebView 后的运行机制是什么?


↙↙↙阅读原文可查看相关链接,并与作者交流