moaTest.py 6.9 KB

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