Python Appium 开启Android测试之路
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Appium 开启Android测试之路相关的知识,希望对你有一定的参考价值。
1、获取 Android app的Activity
打开终端cmd,先cd进入到刚才下载的“新浪.apk”目录下,然后使用aapt dump badging xxx.apk命令获取包内信息。注意,启动类名称一个字母都不能错。
2、启动微博
#-*-coding:utf-8-*- #Time:2017/7/20 18:06 #Author:YangYangJun import time from appium import webdriver desired_caps = {} desired_caps[‘platformName‘] = ‘Android‘ desired_caps[‘platformVersion‘] = ‘6.0‘ desired_caps[‘deviceName‘] = ‘N79SIV5PVCSODAQC‘ desired_caps[‘appPackage‘] = ‘com.sina.weibo‘ desired_caps[‘appActivity‘] = ‘com.sina.weibo.SplashActivity‘ driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, desired_caps) driver.implicitly_wait(5) time.sleep(3) driver.quit()
3、元素定位
driver.find_element_by_accessibility_id()
driver.find_element_by_android_uiautomator()
driver.find_element_by_ios_uiautomation()
(第三个是ios的专用的)
3.2、下面一一举例进行 介绍
3.2.1、元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作.
uiautomatorviewer是android-sdk自带的一个元素定位工具,非常简单好用,使用uiautomatorviewer,你可以检查一个应用的UI来查看应用的布局和组件以及相关的属性。
启动uiautomatorviewer.bat
打开目录D:\\SProgram\\Sdk\\tools
双击启动,启动之后出现如下界面
如果不喜欢双击启动的话,也可以在cmd里面通过指令启动
先cd到tools目录下,然后输入uiautomatorviewer.bat回车后启动服务
3.2.1、find_element_by_accessibility_id
driver.find_element_by_accessibility_id(u"我的").click()
resrouce-id属性是id:
driver.find_element_by_id(‘username‘)
3.2.3、通过name定位元素
text属性是name
driver.find_element_by_name(‘password‘)
#-*-coding:utf-8-*- #Time:2017/7/21 16:32 #Author:YangYangJun import time import unittest from appium import webdriver import sys reload(sys) sys.setdefaultencoding(‘utf8‘) class Login(unittest.TestCase): def setUp(self): self.desired_caps = {} self.desired_caps[‘platformName‘] = ‘Android‘ self.desired_caps[‘platformVersion‘] = ‘6.0‘ self.desired_caps[‘deviceName‘] = ‘N79SIV5PVCSODAQC‘ self.desired_caps[‘appPackage‘] = ‘com.xxxx.mrg‘ self.desired_caps[‘appActivity‘] = ‘com.uzmap.pkg.EntranceActivity‘ #隐藏键盘 self.desired_caps[‘unicodeKeyboard‘] = True self.desired_caps[‘resetKeyboard‘] = True self.driver = webdriver.Remote(‘http://localhost:4723/wd/hub‘, self.desired_caps) #self.driver.implicitly_wait(15) #测试异常登录-用户不存在 def test_aNoUserLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登录/注册").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer1‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘111111‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登录").click() time.sleep(18) get_alertMessage = driver.find_elements_by_android_uiautomator(‘new Uiselector().resourceId("android:id/message")‘).get_attribute(‘name‘) time.sleep(2) if get_alertMessage == u‘用户不存在‘: print u"用户不存在,用例执行成功!" else: print u"用例执行失败!" def test_bNoPassLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登录/注册").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘1111112‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登录").click() time.sleep(18) get_alertMessage = driver.find_elements_by_android_uiautomator(‘new Uiselector().resourceId("android:id/message")‘).get_attribute(‘name‘) time.sleep(2) print get_alertMessage[0:5] # if get_alertMessage == u‘用户不存在‘: # print u"用户不存在,用例执行成功!" # else: # print u"用例执行失败!" # # 测试正常登录 def test_cLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登录/注册").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘111111‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登录").click() time.sleep(2) # 判断是否登录成功 print driver.find_element_by_accessibility_id(u"西藏医药销售有限公司").get_attribute(‘name‘) nickName = driver.find_element_by_accessibility_id(u"西藏医药销售有限公司").get_attribute(‘name‘) temp = nickName[0:2] print temp if nickName == u‘西藏医药销售有限公司‘: print ‘登录成功!‘ else: print ‘登录失败!‘ def tearDown(self): self.driver.close_app() self.driver.quit() if __name__ == "__main__": unittest.main()
以上是关于Python Appium 开启Android测试之路的主要内容,如果未能解决你的问题,请参考以下文章
Python + Robotframework + Appium 之APP自动化测试小试牛刀(Android)