| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- # -*- coding: utf-8 -*-
- import os
- import unittest
- 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
- #去掉list中的重复元素,并保持元素位置不变
- def listQD(onelist):
- newlist = []
- for i in onelist:
- if not i in newlist:
- newlist.append(i)
- for i in newlist:
- print i
- return newlist
- def changeListTree(listTree):
- listTreeName = []
- if len(listTree) > 0 :
- for i in listTree:
- listTreeName.append(i.text)
- return listTreeName
- # newListName中有而oldListName中没有的
- # def listIsDif(newListName,oldListName):
- # # newListName = changeListTree(newList)
- # # oldListName = changeListTree(oldList)
- # difListName = list(set(newListName).difference(set(oldListName)))
- # # print difListName
- # print '不同路径的长度',len(difListName)
- # return difListName
- def listDif(newList,oldList):
- for iold in oldList:
- for inew in newList:
- if iold == inew:
- newList.remove(inew)
- # for i in newList:
- # print i
- return newList
- #找人
- def findOne(self, lookfor, topTree, lastTree,clickTree):
- findflag = False
- # copyTreeName = topTree
- # lastTreeName = changeListTree(lastTree)
- if len(lastTree) > 0 and len(topTree) > 0 and len(clickTree) >0:
- for i in topTree:
- lastTree.append(i)
- tempTree = listQD(lastTree)
- if len(tempTree) > 0:
- topTree = listDif(tempTree,clickTree)
- # print '目前路径如下:', topTree
- toptreeCount = len(topTree)
- for onetree in topTree:
- num = topTree.index(onetree)
- currenttreeName = topTree[num]
- if elementIsExit(self, currenttreeName):
- self.driver.find_element_by_name(currenttreeName).click()
- clickTree.append(currenttreeName)
- print clickTree
- sleep(1)
- print '程序点击了该路径:', currenttreeName
- while True:
- otherTree = self.driver.find_elements_by_id('cn.wswin.moa:id/tv_item_tree_parent')
- if elementIsExit(self, lookfor):
- print '程序在该路径下找人:', lookfor
- findflag = True
- # self.driver.find_element_by_name(lookfor).click()
- break
- elif len(listDif(changeListTree(otherTree), topTree)) > 0:
- for i in otherTree:
- print '发现新的路径:', i.text
- for i in clickTree:
- print clickTree
- findOne(self, lookfor, topTree, changeListTree(otherTree),clickTree)
- elif num < (toptreeCount - 1):
- nexttreeName = topTree[num + 1]
- print '要查看的下一个路径名称:', nexttreeName
- if elementIsExit(self, nexttreeName):
- break
- else:
- print '查询不到要找的人,以及下一级节点,需要滑动'
- self.driver.swipe(710, 1000, 710, 200) # 往下滑动当前页
- else:
- self.driver.swipe(710, 1000, 710, 200) # 往下滑动当前页
- print '找不到任何规定的内容'
- if findflag == True:
- break
- 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替换
- otherTreeName = []
- topTreeNmae = changeListTree(topTree)
- clickTree = []
- if findOne(opentree, '成向阳', topTreeNmae, otherTreeName, clickTree):
- 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()
|