test.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import unittest
  4. from appium import webdriver
  5. from time import sleep
  6. PATH = lambda p: os.path.abspath(
  7. os.path.join(os.path.dirname(__file__), p)
  8. )
  9. # desired_caps = {}
  10. # desired_caps['platformName'] = 'Android'
  11. # desired_caps['platformVersion'] = '4.2.3'
  12. # desired_caps['deviceName'] = '7bbe99db'
  13. # # desired_caps['app']=PATH('D:\AutoTest\ppp\winsoft.apk')
  14. # desired_caps['appPackage'] = 'cn.wswin.moa'
  15. # desired_caps['appActivity'] = '.ui.activity.SplashActivity'
  16. # desired_caps['unicodeKeyboard'] = True
  17. # desired_caps['resetKeyboard'] = True
  18. #
  19. # driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
  20. #捕获异常
  21. #检查当前页面是否存在查找的元素
  22. def elementIsExit(self,element):
  23. try:
  24. self.driver.find_element_by_name(element)
  25. except BaseException,e:
  26. # print e
  27. return False
  28. return True
  29. #去掉list中的重复元素,并保持元素位置不变
  30. def listQD(onelist):
  31. newlist = []
  32. for i in onelist:
  33. if not i in newlist:
  34. newlist.append(i)
  35. return newlist
  36. def printList(onelist,tittle):
  37. pathName = ''
  38. for i in onelist:
  39. pathName = pathName + i + ','
  40. print tittle,':',pathName,'长度:',len(onelist)
  41. def changeListTree(listTree):
  42. listTreeName = []
  43. if len(listTree) > 0 :
  44. for i in listTree:
  45. listTreeName.append(i.text)
  46. return listTreeName
  47. # newListName中有而oldListName中没有的
  48. # def listIsDif(newListName,oldListName):
  49. # # newListName = changeListTree(newList)
  50. # # oldListName = changeListTree(oldList)
  51. # difListName = list(set(newListName).difference(set(oldListName)))
  52. # # print difListName
  53. # print '不同路径的长度',len(difListName)
  54. # return difListName
  55. def listDif(newList,oldList):
  56. for iold in oldList:
  57. for inew in newList:
  58. if iold == inew:
  59. newList.remove(inew)
  60. return newList
  61. #找人
  62. def findOne(self, lookfor, topTree, lastTree,clickTree):
  63. findflag = False
  64. # copyTreeName = topTree
  65. # lastTreeName = changeListTree(lastTree)
  66. if len(lastTree) > 0 and len(topTree) > 0 and len(clickTree) >0:
  67. for i in topTree:
  68. lastTree.append(i)
  69. tempTree = listQD(lastTree)
  70. if len(tempTree) > 0:
  71. topTree = listDif(tempTree,clickTree)
  72. # print '目前路径如下:', topTree
  73. toptreeCount = len(topTree)
  74. for onetree in topTree:
  75. num = topTree.index(onetree)
  76. currenttreeName = topTree[num]
  77. if elementIsExit(self, currenttreeName):
  78. self.driver.find_element_by_name(currenttreeName).click()
  79. clickTree.append(currenttreeName)
  80. printList(clickTree,'已经点击过的路径')
  81. sleep(1)
  82. # print '程序点击了该路径:', currenttreeName
  83. while True:
  84. otherTree = self.driver.find_elements_by_id('cn.wswin.moa:id/tv_item_tree_parent')
  85. if elementIsExit(self, lookfor):
  86. print '程序在该路径下找人:', lookfor
  87. findflag = True
  88. # self.driver.find_element_by_name(lookfor).click()
  89. break
  90. elif len(listDif(changeListTree(otherTree), topTree)) > 0:
  91. # 如果 新找到的otherTree 都在 clickTree里面,则滑动
  92. if len(listDif(changeListTree(otherTree), clickTree)) == 0:
  93. self.driver.swipe(710, 600, 710, 200) # 往下滑动当前页
  94. printList(changeListTree(otherTree),'发现新路径')
  95. findOne(self, lookfor, topTree, changeListTree(otherTree),clickTree)
  96. elif num < (toptreeCount - 1):
  97. nexttreeName = topTree[num + 1]
  98. print '要查看的下一个路径名称:', nexttreeName
  99. if elementIsExit(self, nexttreeName):
  100. break
  101. else:
  102. print '查询不到要找的人,以及下一级节点,需要滑动'
  103. self.driver.swipe(710, 1000, 710, 200) # 往下滑动当前页
  104. else:
  105. self.driver.swipe(710, 1000, 710, 200) # 往下滑动当前页
  106. print '找不到任何规定的内容'
  107. if findflag == True:
  108. break
  109. print '出结果了要'
  110. return findflag
  111. class PYtest(unittest.TestCase):
  112. @classmethod
  113. def setUpClass(self):
  114. print('--------------start App-------------')
  115. desired_caps = {}
  116. desired_caps['platformName'] = 'Android'
  117. desired_caps['platformVersion'] = '4.3'
  118. desired_caps['deviceName'] = '7bbe99db'
  119. # desired_caps['app']=PATH('D:\AutoTest\ppp\winsoft.apk')
  120. desired_caps['appPackage'] = 'cn.wswin.moa'
  121. desired_caps['appActivity'] = '.ui.activity.SplashActivity'
  122. # desired_caps['appActivity'] = '.ui.activity.MainActivity'
  123. desired_caps['unicodeKeyboard'] = True
  124. desired_caps['resetKeyboard'] = True
  125. self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
  126. self.driver.implicitly_wait(5)#全局方法最长超时时间
  127. @classmethod
  128. def py_login(login):#登录测试用例
  129. #忽略引导页存在,手动去掉引导页之后再进行以下所有测试用例
  130. sleep(2)
  131. current_Act = login.driver.current_activity
  132. print current_Act
  133. if current_Act == '.ui.activity.LoginActivity':
  134. print ('login App')
  135. account = login.driver.find_element_by_id('cn.wswin.moa:id/e_user_name')
  136. password = login.driver.find_element_by_id('cn.wswin.moa:id/e_user_password')
  137. btn_login = login.driver.find_element_by_name('登录')
  138. account.clear()
  139. account.send_keys('hgl')
  140. password.clear()
  141. password.send_keys('hegl123456')
  142. btn_login.click()
  143. print ('login App passed')
  144. elif current_Act =='.ui.activity.MainActivity':
  145. print ('app is login')
  146. else:
  147. print ('不知道你在哪个页面')
  148. def py_quitApp(quit):#退出APP
  149. sleep(2)
  150. current_Act= quit.driver.current_activity
  151. print current_Act
  152. if current_Act == '.ui.activity.MainActivity':
  153. print('quit App')
  154. btn_left = quit.driver.find_element_by_class_name('android.widget.ImageButton')#最好不要在判断之前提前定义,会因为找不到对象而报错
  155. if btn_left.is_enabled():
  156. btn_left.click()
  157. btn_quit = quit.driver.find_element_by_name('退出')
  158. if btn_quit.is_enabled():
  159. btn_quit.click()
  160. btn_sure = quit.driver.find_element_by_name('确认')
  161. if btn_sure.is_enabled():
  162. btn_sure.click()
  163. print('quit App passed')
  164. def py_openTree2(opentree):
  165. current_Act = opentree.driver.current_activity
  166. print current_Act
  167. if current_Act == '.ui.activity.MainActivity':
  168. sleep(2)
  169. opentree.driver.tap([(333, 1224)]) # 此处定位在联系人界面
  170. if elementIsExit(opentree,'联系人'):
  171. print ('当前页面位置:联系人界面')
  172. topTree = opentree.driver.find_elements_by_id('cn.wswin.moa:id/tv_item_tree_parent') # 后期用target替换
  173. otherTreeName = []
  174. topTreeNmae = changeListTree(topTree)
  175. clickTree = []
  176. if findOne(opentree, '成向阳', topTreeNmae, otherTreeName, clickTree):
  177. print '找到人了'
  178. def finishDriver(self):
  179. print('finish Driver')
  180. self.driver.quit()
  181. print('finish Driver passed')
  182. def py_Clicktap1(self):#用例点击事件
  183. self.driver.find_element_by_name('在吗').click()
  184. print('Click Passed')
  185. def py_Swiap(self):
  186. self.driver.swipe(720,700,0,700)
  187. print ('Swip Passed')
  188. if __name__ == '__main__':
  189. suite = unittest.TestSuite()
  190. #需要测试的用例就addTest,不加的就不会运行
  191. # suite.addTest(PYtest('py_Swiap'))
  192. suite.addTest(PYtest('py_login'))#APP登录
  193. suite.addTest(PYtest('py_openTree2'))
  194. # suite.addTest(PYtest('py_quitApp'))#APP登出
  195. suite.addTest(PYtest('finishDriver'))#结束测试
  196. unittest.TextTestRunner(verbosity=1).run(suite)
  197. # timestr = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
  198. # filename = "D:\\AutoTest\\report\\report_" + timestr + ".html"
  199. # fp = file(filename, "wb")
  200. # runner =HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'测试用例详情')
  201. # runner.run(suite)
  202. # fp.close()