test.py 8.2 KB

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