环境准备

为了避免走弯路,我们先要确保三点:

否则的话,你会遇到诸如此类的问题:

2.x 的真机

info: "/Applications/adt-bundle-mac-x86_64-20130729/sdk/platform-tools/adb" -s S5830f63efdb6 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
info: [ADB STDOUT] uiautomator: permission denied

4.0.x 的真机

info: "/Applications/adt-bundle-mac-x86_64-20130729/sdk/platform-tools/adb" -s HT1B4V803001 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
info: [ADB STDOUT] /system/bin/sh: uiautomator: not found

如果使用的是 Appium.dmg 安装的 Appium,那要设置 custom android sdk, 如图

这样 Appium 启动的时候可以使用 adb android 等命令。

这三点都做到的情况下,我们要看下 devices 的情况,因为运行 appium 测试脚本前要确保,devices 连接上了。所以要做的是:启动模拟器或者连上手机

然后我们运行 adb devices -l,

➜  ~  adb devices -l
List of devices attached
emulator-5554          device product:sdk model:sdk device:generic
e912s                  device usb:24100000

P.S Appium 支不支持多个 device 还没有定论。可以参考 https://groups.google.com/forum/#! topic/appium-discuss/xKCt4HrerrU

我们关掉模拟器,因为模拟器实在太慢了。(在模拟器上运行过 sample code, 都是能正常运行的。)

写个脚本玩玩

其实 sample code 里面的代码已经非常丰富了。就照搬一个吧。


# -*- coding: utf-8 -*-
import os
import glob
import unittest
from selenium import webdriver


class TestMyGame(unittest.TestCase):

    def setUp(self):
        app = os.path.abspath(glob.glob(os.path.dirname(__file__)
                                        + './*.apk')[0])
        desired_caps = {
            'device': 'appium',
            'app': app,
            'app-package': 'com.example.android.contactmanager',
            'app-activity': '.ContactManager'
        }

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

    def test(self):
        driver = self.driver
        el = driver.find_element_by_name("Add Contact")
        el.click()
        textfields = driver.find_elements_by_tag_name("textfield")
        textfields[0].send_keys("My Name")
        textfields[2].send_keys("someone@somewhere.com")
        driver.find_element_by_name("Save").click()

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

if __name__ == '__main__':
    unittest.main()


ContactManager 是 android sample 里面的代码。 大家在安装 adt tool 的时候,可以获取。
我把 build 出来的 apk 和下面这个脚本放在一个目录下,然后运行。

在使用模拟器的时候,除了慢,工作的非常好。但是在海信的真机上就悲剧了。 因为在聚焦输入框的时候,弹出了搜狐输入法,干扰了 send_key 方法。github 上有 issue 记录,可惜没有好的解决方法。(https://github.com/appium/appium/issues/173

至此,整个安卓的流程都跑过来了。总的来说, Appium 目前还不是很成熟。不过应该是未来的方向。学起来总归是好的!


以上所有步骤,都在 mac 10.9 xcode 5.0.1 上运行通过。


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