Mac上运行appium的python脚本

2015-02-15  籽藤 

为了进行移动App的功能测试自动化,我玩起了appium。有了selenium webdriver基础,理解appium就很容易。运行脚本不是什么难事儿,但对于刚接触移动测试的新手,有一些基础知识必须了解。

检查appium的安装

appium的安装方式有很多,npm啦,pip啦,brew... 条条大路通罗马。关键在于,我们能确认appium已经成功安装了。我上一篇博文已经提到了“appium-doctor”命令,除此此外,还可以用“appium”启动appium server,在浏览器中访问“http://localhost:4723/wd/hub”,如果页面显示“That URL did not map to a valid JSONWP resource”,说明没问题:)

安装appium GUI (非必需)

有人喜欢用客户端,有人是命令控。点此下载客户端

检查python

如我在之前博文中提到的,Mac自带Python运行环境。在终端输入“python”就可以看到python版本信息。

检查Genymotion 设备

进入Genymotion shell界面,用“devices show” 命令显示设备详细信息

运用UIAutomatorViewer

UIAutomatorViewer存在于Android SDK的tools目录中,用于查看App各个控件的属性。

编写一个基于appium的python脚本 import os from time import sleep import unittest from appium import webdriver # Returns abs path relative to this file and not cwd PATH = lambda p: os.path.abspath( os.path.join(os.path.dirname(__file__), p) ) class SimpleAndroidTests(unittest.TestCase): def setUp(self): desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.4' desired_caps['deviceName'] = 'Custom Phone - 4.2.2 - API 17 - 768x1280' desired_caps['app'] = PATH( '/Users/applewu/Documents/iWork/workspace/test-android.apk' ) self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) #def tearDown(self): # end the session #self.driver.quit() def test_find_elements(self): text = self.driver.find_elements_by_class_name('android.widget.EditText')[0] text.send_keys('1') el = self.driver.find_elements_by_class_name('android.widget.Button')[1] self.assertIsNotNone(el) el.click() if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(SimpleAndroidTests) unittest.TextTestRunner(verbosity=2).run(suite)

千万别问我为啥上面的python脚本在你机器上直接运行不起来,因为它是基于我的测试app(test-android.apk)的,你要先理解self.driver的方法,作出调整。

运行脚本

理解了上面每一步,运行脚本就是水到渠成的事儿。一行“python simple.py”命令搞定。

465°/4654 人阅读/0 条评论 发表评论

登录 后发表评论