appium自动化测试环境搭建和demo测试(python版本)

来源:互联网 发布:java变量的主要类型 编辑:程序博客网 时间:2024/06/10 01:40

一、环境准备:

1、python2.7.10安装,可以下载ActivePython ,已集成setuptool和pip、virtualenv等包。

下载地址:http://www.activestate.com/activepython/downloads

2、appium客户端安装,直接pip install appium-python-client

3、nodejs安装

下载地址:https://nodejs.org/en/download/

4、安装java
详情参考http://blog.csdn.net/hqzxsc2006/article/details/47700453
5、安装Android sdk 
详情参考http://blog.csdn.net/hqzxsc2006/article/details/47700537

6、appium服务端安装

官方下载地址:https://bitbucket.org/appium/appium.app/downloads/

百度云盘下载地址:http://pan.baidu.com/s/1jGvAISu

二、开启appium服务

运行已安装好的appium服务

三、编写脚本

pycharm里新建一个文件,拷贝以下demo测试代码

官方其他测试例子:https://github.com/appium/sample-code/tree/master/sample-code/examples/python

import osimport unittestfrom appium import webdriverfrom time import sleep# Returns abs path relative to this file and not cwdPATH = lambda p: os.path.abspath(    os.path.join(os.path.dirname(__file__), p))class ContactsAndroidTests(unittest.TestCase):    def setUp(self):        desired_caps = {}        desired_caps['platformName'] = 'Android'        desired_caps['platformVersion'] = '4.2'        desired_caps['deviceName'] = 'Android'        desired_caps['app'] = PATH(            '../../../sample-code/apps/ContactManager/ContactManager.apk'        )        desired_caps['appPackage'] = 'com.example.android.contactmanager'        desired_caps['appActivity'] = '.ContactManager'        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)    def tearDown(self):        self.driver.quit()    def test_add_contacts(self):        el = self.driver.find_element_by_accessibility_id("Add Contact")        el.click()        textfields = self.driver.find_elements_by_class_name("android.widget.EditText")        textfields[0].send_keys("Appium User")        textfields[2].send_keys("someone@appium.io")        self.assertEqual('Appium User', textfields[0].text)        self.assertEqual('someone@appium.io', textfields[2].text)        self.driver.find_element_by_accessibility_id("Save").click()        # for some reason "save" breaks things        alert = self.driver.switch_to_alert()        # no way to handle alerts in Android        self.driver.find_element_by_android_uiautomator('new UiSelector().clickable(true)').click()        self.driver.press_keycode(3)if __name__ == '__main__':    suite = unittest.TestLoader().loadTestsFromTestCase(ContactsAndroidTests)    unittest.TextTestRunner(verbosity=2).run(suite)

元素定位使用uiautomatorviewer查看,在命令行输入uiautomatorviewer即可打开界面查看

四、运行脚本

电脑连接好运行手机,然后运行脚本即可。


参考中文文档:https://github.com/appium/appium/tree/master/docs/cn

0 0
原创粉丝点击