# -*- coding: utf-8 -*- import os,unittest,copy from appium import webdriver from time import sleep PATH = lambda p: os.path.abspath( os.path.join(os.path.dirname(__file__), p) ) # desired_caps = {} # desired_caps['platformName'] = 'Android' # desired_caps['platformVersion'] = '4.2.3' # desired_caps['deviceName'] = '7bbe99db' # # desired_caps['app']=PATH('D:\AutoTest\ppp\winsoft.apk') # desired_caps['appPackage'] = 'cn.wswin.moa' # desired_caps['appActivity'] = '.ui.activity.SplashActivity' # desired_caps['unicodeKeyboard'] = True # desired_caps['resetKeyboard'] = True # # driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) #捕获异常 def elementIsExit(self,element): try: self.driver.find_element_by_name(element) except BaseException,e: print e return False return True def changeListTree(listTree): listTreeName = [] for i in listTree: listTreeName.append(i.text) return listTreeName def listIsDifent(newList, oldList): # newListName = changeListTree(newList) # oldListName = changeListTree(oldList) # newListName中有而oldListName中没有的 difListName = list(set(newList).difference(set(oldList))) print '不同路径的长度',len(difListName) return difListName #找人 def findOne(self, lookfor, topTree): findflag = False copyTreeName = changeListTree(topTree) toptreeCount = topTree.__len__() # for onetree in copyTreeName: # num = copyTreeName.index(onetree) num = 0 currenttreeName = copyTreeName[num] if elementIsExit(self, currenttreeName): self.driver.find_element_by_name(currenttreeName).click() print '程序点击了该路径:', currenttreeName while True: otherTree = self.driver.find_elements_by_id('cn.wswin.moa:id/tv_item_tree_parent') num = num + 1 if elementIsExit(self, lookfor): print '程序在该路径下找人:', lookfor findflag = True # self.driver.find_element_by_name(lookfor).click() break elif len(listIsDifent(changeListTree(otherTree), copyTreeName)) > 0: print '发现新的路径:', otherTree findOne(self, lookfor, otherTree) elif num <= (toptreeCount - 1): nexttreeName = copyTreeName[num] print '要查看的下一个路径名称:', nexttreeName if elementIsExit(self, nexttreeName): self.driver.find_element_by_name(nexttreeName).click() else: print '查询不到下一个路径,需要滑动' self.driver.swipe(710, 1000, 710, 100) # 往下滑动当前页 else: print '结束查找' break if findflag == True: print '出结果了要' return findflag class PYtest(unittest.TestCase): @classmethod def setUpClass(self): print('--------------start App-------------') desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.3' desired_caps['deviceName'] = '7bbe99db' # desired_caps['app']=PATH('D:\AutoTest\ppp\winsoft.apk') desired_caps['appPackage'] = 'cn.wswin.moa' desired_caps['appActivity'] = '.ui.activity.SplashActivity' # desired_caps['appActivity'] = '.ui.activity.MainActivity' desired_caps['unicodeKeyboard'] = True desired_caps['resetKeyboard'] = True self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) self.driver.implicitly_wait(5)#全局方法最长超时时间 @classmethod def py_login(login):#登录测试用例 #忽略引导页存在,手动去掉引导页之后再进行以下所有测试用例 sleep(2) current_Act = login.driver.current_activity print current_Act if current_Act == '.ui.activity.LoginActivity': print ('login App') account = login.driver.find_element_by_id('cn.wswin.moa:id/e_user_name') password = login.driver.find_element_by_id('cn.wswin.moa:id/e_user_password') btn_login = login.driver.find_element_by_name('登录') account.clear() account.send_keys('hgl') password.clear() password.send_keys('hegl123456') btn_login.click() print ('login App passed') elif current_Act =='.ui.activity.MainActivity': print ('app is login') else: print ('不知道你在哪个页面') def py_quitApp(quit):#退出APP sleep(2) current_Act= quit.driver.current_activity print current_Act if current_Act == '.ui.activity.MainActivity': print('quit App') btn_left = quit.driver.find_element_by_class_name('android.widget.ImageButton')#最好不要在判断之前提前定义,会因为找不到对象而报错 if btn_left.is_enabled(): btn_left.click() btn_quit = quit.driver.find_element_by_name('退出') if btn_quit.is_enabled(): btn_quit.click() btn_sure = quit.driver.find_element_by_name('确认') if btn_sure.is_enabled(): btn_sure.click() print('quit App passed') def py_openTree2(opentree): current_Act = opentree.driver.current_activity print current_Act if current_Act == '.ui.activity.MainActivity': sleep(2) opentree.driver.tap([(333, 1224)]) # 此处定位在联系人界面 if elementIsExit(opentree,'联系人'): print ('当前页面位置:联系人界面') topTree = opentree.driver.find_elements_by_id('cn.wswin.moa:id/tv_item_tree_parent') # 后期用target替换 if findOne(opentree, '陈明干', topTree): print '找到人了' def finishDriver(self): print('finish Driver') self.driver.quit() print('finish Driver passed') def py_Clicktap1(self):#用例点击事件 self.driver.find_element_by_name('在吗').click() print('Click Passed') def py_Swiap(self): self.driver.swipe(720,700,0,700) print ('Swip Passed') if __name__ == '__main__': suite = unittest.TestSuite() #需要测试的用例就addTest,不加的就不会运行 # suite.addTest(PYtest('py_Swiap')) suite.addTest(PYtest('py_login'))#APP登录 suite.addTest(PYtest('py_openTree2')) # suite.addTest(PYtest('py_quitApp'))#APP登出 suite.addTest(PYtest('finishDriver'))#结束测试 unittest.TextTestRunner(verbosity=1).run(suite) # timestr = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) # filename = "D:\\AutoTest\\report\\report_" + timestr + ".html" # fp = file(filename, "wb") # runner =HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'测试用例详情') # runner.run(suite) # fp.close()