appium之基于Python实现自动化测试实战(Android)
2018-01-18 14:38
1151 查看
#!/usr/bin/python # encoding:utf-8 import os import time import unittest from appium import webdriver #添加这个可以自动生成测试报告 # import HTMLTestRunner desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['deviceName'] = '127.0.0.1:62001' desired_caps['appPackage'] = '包名' desired_caps['appActivity'] = 'activity名,可以通过appium的inspection找到该名称' #超时时间 desired_caps['newCommandTimeout'] = '100' desired_caps['app'] = 'apk包存放地址' driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) class tjTeacher(unittest.TestCase): #登陆 def loginTest(self): time.sleep(3) pwlogin_btn = driver.find_element_by_id('bt_pwdlogin').click() phone_number = driver.find_element_by_id('phone_number_edit_text') phone_number.click() phone_number.send_keys('15102001212') password = driver.find_element_by_id('pwd_edit_text') password.click() password.send_keys('1111aaa') driver.find_element_by_id('login_btn').click() #设置隐式等待时间 driver.implicitly_wait(3) if self.isElement('id', 'close_btn_iv') is True: driver.find_element_by_id('close_btn_iv').click() driver.find_element_by_id('close_btn_iv').click() driver.find_element_by_id('tab_message_tv').click() #发消息 def messageTest(self): driver.find_element_by_id('tab_message_tv').click() driver.find_element_by_id('group_chat_lyt').click() driver.find_element_by_id('circle').click() #元素不好定位时,可以通过此方式定位到 driver.find_element_by_android_uiautomator('new UiSelector().text(\"小明\")').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"小红\")').click() driver.find_element_by_id('recive_sel_ok').click() txt = driver.find_element_by_id('msg_send_edit') txt.click() txt.send_keys('123') driver.find_element_by_id('msg_send_btn').click() #显式等待 time.sleep(2) driver.find_element_by_id('msg_action_btn').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"相册\")').click() # driver.find_element_by_android_uiautomator('new UiSelector().text(\"最近\")').click() time.sleep(2) driver.find_element_by_xpath('//android.widget.FrameLayout[3]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ImageView[1]').click() time.sleep(2) driver.find_element_by_id('msg_switch_btn').click() action = TouchAction(driver) el = driver.find_element_by_id('msg_record_btn') action.long_press(el).wait(10000).perform() driver.find_element_by_id('audio_record_preview_right_btn').click() driver.find_element_by_id('left_btn').click() driver.find_element_by_id('left_btn').click() driver.find_element_by_id('btn_ok').click() driver.find_element_by_id('left_btn').click() time.sleep(3) def informTest(self): driver.find_element_by_id('send_notification_lyt').click() driver.find_element_by_id('right_btn').click() time.sleep(2) driver.find_element_by_id('school_name').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"全校老师(0/50)\")').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"小明老师\")').click() time.sleep(1) #模拟手机中,上下滑动操作 driver.swipe(700,1275,700,160,300) driver.swipe(700,1275,700,160,300) driver.find_element_by_android_uiautomator('new UiSelector().text(\"班级(0/11)\")').click() time.sleep(1) driver.swipe(700,1275,700,160,250) driver.find_element_by_android_uiautomator('new UiSelector().text(\"一年级测试一班(0/1)\")').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"小丽家长测试一号\")').click() driver.find_element_by_id('tip_message_tv').click() txt = driver.find_element_by_id('msg_content_edittext') txt.click() txt.send_keys('123') driver.find_element_by_id('thumbnailIv').click() #控件无论通过元素还是uiselector都定位不到时,可以使用此方法 x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] driver.tap([(x*0.2,y*0.9)]) time.sleep(2) driver.find_element_by_xpath('//android.widget.ListView/android.widget.LinearLayout[contains(@index,0)]/android.widget.ImageView[contains(@index,0)]').click() time.sleep(2) driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[1]').click() driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[3]').click() driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[5]').click() time.sleep(2) driver.find_element_by_id('photo_sel_ok').click() driver.find_element_by_id('right_btn').click() time.sleep(2) driver.find_element_by_id('left_btn').click() time.sleep(3) def homewrokTest(self): driver.find_element_by_id('iv_homework_icon').click() driver.find_element_by_id('right_btn').click() driver.find_element_by_id('school_name').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"一年级测试一班(0/1)\")').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"家长测试一号\")').click() driver.find_element_by_android_uiautomator('new UiSelector().text(\"一年级一班(0/10)\")').click() driver.swipe(700,1275,700,160,300) driver.find_element_by_android_uiautomator('new UiSelector().text(\"小红\")').click() driver.find_element_by_id('tip_message_tv').click() time.sleep(2) driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.TextView[3]').click() driver.find_element_by_id('thumbnailIv').click() x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] driver.tap([(x*0.2,y*0.9)]) time.sleep(2) driver.find_element_by_xpath('//android.widget.ListView/android.widget.LinearLayout[contains(@index,0)]/android.widget.ImageView[contains(@index,0)]').click() time.sleep(2) driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[1]').click() driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[5]').click() driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[7]').click() time.sleep(2) driver.find_element_by_id('photo_sel_ok').click() txt = driver.find_element_by_id('msg_content_edittext') txt.click() txt.send_keys('123') driver.find_element_by_id('right_btn').click() time.sleep(2) driver.find_element_by_id('left_btn').click() def homeTest(self): self.driver.find_element_by_id('tab_home_tv').click() def foundTest(self): #这里也可以使用while循环来实现,while比for感觉更合适 for i in xrange(1,100): time.sleep(10*i) tb = driver.find_element_by_id('tab_found_tv') if tb is not None: tb.click() break for i in xrange(1,1000): driver.implicitly_wait(10*i) tmp = driver.find_element_by_id('school_icon') if tmp is not None: tmp.click() break driver.find_element_by_id('right_btn').click() txt = driver.find_element_by_id('et_content') txt.click() txt.send_keys('123') driver.find_element_by_id('thumbnailIv').click() x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] driver.tap([(x*0.2,y*0.9)]) driver.find_element_by_xpath('//android.widget.ListView/android.widget.LinearLayout[contains(@index,0)]/android.widget.ImageView[contains(@index,0)]').click() time.sleep(2) driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[2]').click() driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[4]').click() driver.find_element_by_xpath('//android.widget.GridView[1]/android.widget.FrameLayout[9]').click() time.sleep(2) driver.find_element_by_id('photo_sel_ok').click() time.sleep(2) driver.find_element_by_id('right_btn').click() tmp_click = driver.find_element_by_id('rl_like') tmp_click.click() tmp_attribute = tmp_click.get_attribute('clickable') print 'tmp_attribute %s' % tmp_attribute driver.find_element_by_id('tv_comment').click() tmp_comment = driver.find_element_by_id('input_ed') tmp_comment.click() tmp_comment.send_keys('hhh') driver.find_element_by_id('tv_send').click() driver.implicitly_wait(3) driver.find_element_by_id('left_btn').click() driver.find_element_by_id('tv_delete').click() driver.find_element_by_id('btn_ok').click() driver.find_element_by_id('left_btn').click() print '22222222' # driver.find_element_by_id('class_icon').click() def classTest(self): for i in xrange(1,1000): driver.implicitly_wait(i*10) tmp_classbtn = driver.find_element_by_id('tab_class_tv') if tmp_classbtn is not None: tmp_classbtn.click() break driver.find_element_by_android_uiautomator('new UiSelector().descriptionContains(\"妈妈帮手\")').click() driver.find_element_by_id('left_btn').click() def loginOutTest(self): time.sleep(3) driver.find_element_by_id('tab_config_tv').click() driver.find_element_by_id('right_btn').click() driver.find_element_by_id('setting_exit_btn').click() driver.find_element_by_id('btn_ok').click() # 网上找的,在判断一个元素是否找到时会报错的解决方案 def isElement(self,identifyBy,c): ''' Determine whether elements exist Usage: isElement(By.XPATH,"//a") ''' time.sleep(1) flag=None try: if identifyBy == "id": #self.driver.implicitly_wait(60) driver.find_element_by_id(c) elif identifyBy == "xpath": #self.driver.implicitly_wait(60) driver.find_element_by_xpath(c) elif identifyBy == "class": driver.find_element_by_class_name(c) elif identifyBy == "link text": driver.find_element_by_link_text(c) elif identifyBy == "partial link text": driver.find_element_by_partial_link_text(c) elif identifyBy == "name": driver.find_element_by_name(c) elif identifyBy == "tag name": driver.find_element_by_tag_name(c) elif identifyBy == "css selector": driver.find_element_by_css_selector(c) flag = True except NoSuchElementException,e: flag = False finally: return flag # def teardown(): # self.driver.quit() if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(tjTeacher('loginTest')) # suite.addTest(tjTeacher('messageTest')) # suite.addTest(tjTeacher('informTest')) # suite.addTest(tjTeacher('homewrokTest')) # suite.addTest(tjTeacher('foundTest')) # suite.addTest(tjTeacher('classTest')) suite.addTest(tjTeacher('loginOutTest')) runner = unittest.TextTestRunner() runner.run(suite)
相关文章推荐
- APP自动化(1)——搭建Appium自动化测试平台环境(基于python&android)
- MAC 搭建自动化测试环境(基于Appium+python+ios/android+unnitest+HTMLTestRunner)(一)
- SVN+Jenkins+Maven+Appium+TestNG+ReportNG 实战 Android 自动化测试
- Appium 新手贴:Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android (完整版)
- Appium 新手贴:Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android (完整版)
- appium+Python+Android 自动化测试实例
- Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android(完整版)
- SVN+Jenkins+Maven+Appium+TestNG+ReportNG 实战 Android 自动化测试
- 《Selenium2自动化测试实战--基于Python语言》 --即将面市
- Android monkeyrunner录制 && python脚本&& adb shell input 实现自动化测试
- Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android(完整版)
- 基于Appium+Pythom的Android自动化测试
- Appium 新手贴:Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android (完整版)
- 解决android自动化测试中锁屏问题-简单的实现
- DalekJS – 基于 JavaScript 实现跨浏览器的自动化测试
- 基于APPIUM的移动自动化测试
- 基于XML的API自动化测试工具设计与实现
- 【Python】基于PyUnit的自动化单元测试框架,PyUnitGUI
- 【金阳光测试】基于控件核心技术探讨---Android自动化系列(2)---2013年5月