最近在学习使用 Appium 测试 android webview, 官网的 Python sample code 在 4.4 模拟器上测试通过,但是尝试测试 Browser 时,遇到如下问题。

以下是 appium server Log

> Starting Node Server
> info: Welcome to Appium v1.3.4 (REV c8c79a85fbd6870cd6fc3d66d038a115ebe22efe)
> info: Appium REST http interface listener started on 127.0.0.1:4723
> info: [debug] Non-default server args: {"address":"127.0.0.1","logNoColors":true,"deviceName":"10.37.130.208:5555","platformName":"Android","platformVersion":"17","automationName":"Appium"}
> info: Console LogLevel: debug
> info: --> POST /wd/hub/session {"desiredCapabilities":{"platformVersion":"4.4","appPackage":"com.android.browser","platformName":"Android","appActivity":".BrowserActivity","deviceName":"emulator-5554"}}
> info: Client User-Agent string: Python-urllib/2.7
> info: Set mode: Proxying straight through to Chromedriver
> info: [debug] Looks like we want chrome on android
> info: [debug] Creating new appium session d63580ea-1e03-4287-acc9-508e9c5f1357
> info: [debug] Using fast reset? true
> info: [debug] Preparing device for session
> info: [debug] Not checking whether app is present since we are assuming it's already on the device
> info: [debug] Checking whether adb is present
> info: [debug] Using adb from D:\Android\android-sdk\platform-tools\adb.exe
> info: Retrieving device
> info: [debug] Trying to find a connected android device
> info: [debug] Getting connected devices...
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe devices
> info: [debug] 1 device(s) connected
> info: Found device emulator-5554
> info: [debug] Setting device id to emulator-5554
> info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 wait-for-device
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 shell "echo 'ready'"
> info: [debug] Starting logcat capture
> info: [debug] Pushing unlock helper app to device...
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 install "D:\Chrome_Downloads\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 shell "dumpsys window"
> info: [debug] Screen already unlocked, continuing.
> info: [debug] Forwarding system:4724 to device:4724
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 forward tcp:4724 tcp:4724
> info: [debug] Pushing appium bootstrap to device...
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 push "D:\\Chrome_Downloads\\Appium\\node_modules\\appium\\build\\android_bootstrap\\AppiumBootstrap.jar" /data/local/tmp/
> info: Starting App
> info: [debug] Attempting to kill all 'uiautomator' processes
> info: [debug] Getting all processes with 'uiautomator'
> info: [debug] executing cmd: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 shell "ps 'uiautomator'"
> info: [debug] No matching processes found
> info: [debug] Running bootstrap
> info: [debug] spawning: D:\Android\android-sdk\platform-tools\adb.exe -s emulator-5554 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] WARNING: linker:
> info: [debug] [UIAUTOMATOR STDOUT] libdvm.so has text relocations. This is wasting memory and is a security risk. Please fix.
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
> info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
> info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
> info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
> info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
> info: [debug] [BOOTSTRAP] [debug] Loading json...
> info: [debug] Pushing command to appium work queue: ["getDataDir",{}]
> info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
> info: [debug] [BOOTSTRAP] [debug] Client connected
> info: [debug] dataDir set to: /data
> info: [debug] Creating Chrome session
> info: [debug] Set chromedriver binary as: D:\Chrome_Downloads\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
> info: [debug] Ensuring Chromedriver exists
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"getDataDir","params":{}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: getDataDir
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"\/data","status":0}
> info: [debug] 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 %b))
> info: [debug] No old chromedrivers seemed to exist
> info: [debug] Spawning chromedriver with: D:\Chrome_Downloads\Appium\node_modules\appium\build\chromedriver\windows\chromedriver.exe
> info: [debug] [CHROMEDRIVER] Starting ChromeDriver 2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf) on port 9515
> Only local connections are allowed.
> info: [debug] Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"com.android.chrome","androidDeviceSerial":"emulator-5554"}}}}
> error: Chromedriver create session did not work. Status was 200 and body was {"sessionId":"aa1e43bb36455e7a8d2bc9b50aea07b5","status":13,"value":{"message":"unknown error: com.android.chrome is not installed on device emulator-5554\n  (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Windows NT 6.1 SP1 x86)"}}
> info: [debug] Error: Did not get session redirect from Chromedriver
>     at null.<anonymous> (D:\Chrome_Downloads\Appium\node_modules\appium\lib\devices\android\chromedriver.js:222:12)
>     at Request._callback (D:\Chrome_Downloads\Appium\node_modules\appium\lib\devices\common.js:121:5)
>     at Request.self.callback (D:\Chrome_Downloads\Appium\node_modules\appium\node_modules\request\request.js:121:22)
>     at Request.EventEmitter.emit (events.js:98:17)
>     at Request.<anonymous> (D:\Chrome_Downloads\Appium\node_modules\appium\node_modules\request\request.js:985:14)
>     at Request.EventEmitter.emit (events.js:117:20)
>     at IncomingMessage.<anonymous> (D:\Chrome_Downloads\Appium\node_modules\appium\node_modules\request\request.js:936:12)
>     at IncomingMessage.EventEmitter.emit (events.js:117:20)
>     at _stream_readable.js:920:16
>     at process._tickDomainCallback (node.js:459:13)
> info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Did not get session redirect from Chromedriver)","origValue":"Did not get session redirect from Chromedriver"},"sessionId":null}
> info: <-- POST /wd/hub/session 500 5359.333 ms - 214 
> error: Failed to start an Appium session, err was: Error: Did not get session redirect from Chromedriver

修改后的 Python 代码如下:

import os
import glob
import unittest
from time import sleep

from appium import webdriver


PLATFORM_VERSION = '4.4'


class AndroidWebViewTests(unittest.TestCase):

    def setUp(self):
        app = os.path.abspath(
                os.path.join(os.path.dirname(__file__),
                             '../../apps/selendroid-test-app.apk'))
        desired_caps = {
            #'app': app,
            #'appPackage': 'io.selendroid.testapp',
            #'appActivity': '.HomeScreenActivity',
            'appPackage': 'com.android.browser',
            'appActivity': '.BrowserActivity',
            'platformName': 'Android',
            'platformVersion': PLATFORM_VERSION,
            'deviceName': 'emulator-5554'  
        }

        if (PLATFORM_VERSION != '4.4'):
            desired_caps['automationName'] = 'selendroid'

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


    def test_webview(self):
        print self.driver.contexts
        fh = file('1.xml','w')
        fh.write(self.driver.page_source)
        fh.close()
        #if (PLATFORM_VERSION == '4.4'):
        #    button = self.driver.find_element_by_accessibility_id('buttonStartWebviewCD')
        #else:
        #    button = self.driver.find_element_by_name('buttonStartWebviewCD')
        #print button
        #button.click()

        #self.driver.switch_to.context('WEBVIEW_io.selendroid.testapp')
        self.driver.switch_to.context('WEBVIEW_com.android.browser')

        fh = file('3.xml','w')
        fh.write(self.driver.page_source)
        fh.close()
        #input_field = self.driver.find_element_by_id('name_input')
        #input_field.clear()
        #input_field.send_keys('Appium User')
        #input_field.submit()

        # test that everything is a-ok
        #source = self.driver.page_source
        #self.assertNotEqual(-1, source.find('This is my way of saying hello'))
        #self.assertNotEqual(-1, source.find('"Appium User"'))

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


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


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