希望给点意见和建议,毕竟周围没有人可以交流。。。
嘿嘿,第一次发帖有点小激动。
接触 appium 也有一个多月了,自己根据以前做 selenium 的经验(其实只有一年不到!!!)搭建了框架,希望大家给点意见啊!!!毕竟我身边没有可以和我交流的!!!万分感谢
1.打开 appium server
2.获取当前手机的 device Name 和 安卓版本号,打开 driver
3.运行 case
4.生成报告
5.关闭 driver
6.关闭 appium server
整个程序是从这个模块开始运行的,也是花了我最长时间的地方。下面上代码
# ========================================================
# Summary :run
# Author :tong shan
# Create Date :2015-10-09
# Amend History :
# Amended by :
# ========================================================
import readConfig
readConfigLocal = readConfig.ReadConfig()
import unittest
from testSet.common.DRIVER import myDriver
import testSet.common.Log as Log
import os
from time import sleep
from selenium.common.exceptions import WebDriverException
import threading
mylock = threading.RLock()
log = Log.myLog.getLog()
# ========================================================
# Summary :myServer
# Author :tong shan
# Create Date :2015-10-10
# Amend History :
# Amended by :
# ========================================================
class myServer(threading.Thread):
def __init__(self):
global appiumPath
threading.Thread.__init__(self)
self.appiumPath = readConfigLocal.getConfigValue("appiumPath")
def run(self):
log.outputLogFile("start appium server")
rootDirectory = self.appiumPath[:2]
startCMD = "node node_modules\\appium\\bin\\appium.js"
#cd root directory ;cd appiuu path; start server
os.system(rootDirectory+"&"+"cd "+self.appiumPath+"&"+startCMD)
# ========================================================
# Summary :Alltest
# Author :tong shan
# Create Date :2015-10-10
# Amend History :
# Amended by :
# ========================================================
class Alltest(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global casePath, caseListLpath, caseList, suiteList, appiumPath
self.caseListPath = readConfig.logDir+"\\caseList.txt"
self.casePath = readConfig.logDir+"\\testSet\\"
self.caseList = []
self.suiteList = []
self.appiumPath = readConfigLocal.getConfigValue("appiumPath")
# =================================================================
# Function Name : driverOn
# Function : open the driver
# Input Parameters: -
# Return Value : -
# =================================================================
def driverOn(self):
myDriver.GetDriver()
# =================================================================
# Function Name : driverOff
# Function : colse the driver
# Input Parameters: -
# Return Value : -
# =================================================================
def driverOff(self):
myDriver.GetDriver().quit()
# =================================================================
# Function Name : setCaseList
# Function : read caseList.txt and set caseList
# Input Parameters: -
# Return Value : -
# =================================================================
def setCaseList(self):
print(self.caseListPath)
fp = open(self.caseListPath)
for data in fp.readlines():
sData = str(data)
if sData != '' and not sData.startswith("#"):
self.caseList.append(sData)
# =================================================================
# Function Name : createSuite
# Function : get testCase in caseList
# Input Parameters: -
# Return Value : testSuite
# =================================================================
def createSuite(self):
self.setCaseList()
testSuite = unittest.TestSuite()
if len(self.caseList) > 0:
for caseName in self.caseList:
discover = unittest.defaultTestLoader.discover(self.casePath, pattern=caseName+'.py', top_level_dir=None)
self.suiteList.append(discover)
if len(self.suiteList) > 0:
for test_suite in self.suiteList:
for casename in test_suite:
testSuite.addTest(casename)
else:
return None
return testSuite
# =================================================================
# Function Name : runTest
# Function : run test
# Input Parameters: -
# Return Value : -
# =================================================================
def run(self):
try:
while not isStartServer():
mylock.acquire()
sleep(1)
log.outputLogFile("wait 1s to start appium server")
mylock.release()
else:
log.outputLogFile("start appium server success")
suit = self.createSuite()
if suit != None:
log.outputLogFile("open Driver")
self.driverOn()
log.outputLogFile("Start to test")
unittest.TextTestRunner(verbosity=2).run(suit)
log.outputLogFile("end to test")
log.outputLogFile("close to Driver")
self.driverOff()
else:
log.outputLogFile("Have no test to run")
except Exception as ex:
log.outputError(myDriver.GetDriver(), str(ex))
def isStartServer():
try:
driver = myDriver.GetDriver()
if driver == None:
return False
else:
return True
except WebDriverException:
raise
if __name__ == '__main__':
thread1 = myServer()
thread2 = Alltest()
thread2.start()
thread1.start()
while thread2.is_alive():
sleep(10)#"allTest is alive,sleep10"
else:
#kill myServer
os.system('taskkill /f /im node.exe')
log.outputLogFile("stop appium server")
刚接触的时候发现每次都要手动打开 appium 服务,然后再运行代码,想着是不是太麻烦了?就想试着可不可做成一步?
这一想,就费了我好多功夫。
1.打开 appium server
开始的时候,连如何用命令行打开服务都不会,在群里问了一圈(感谢回答问题的大大们!!!),然后自己又琢磨了一下,搞定了,可是!!!用 os.system(),居然阻塞进程,最后还是问了群里的大大解决(再次感谢!!!!)。
2.如何判断 server 是否被成功开启?
这个问题我想了很久,现在我解决了,但是解决方法我不太满意,希望有大大可以给我一个好点的方法或者思路(跪谢!!)
目前做法:判断是否可以返回一个正常的 driver 对象
def isStartServer():
try:
driver = myDriver.GetDriver()
if driver == None:
return False
else:
return True
except WebDriverException:
raise
3.关闭 appium server
目前的做法是强制杀死,还是不太满意,不知道以后会不会有什么不可预见的问题,希望有大大可以给我一个好点的方法或者思路(跪谢!!)
if __name__ == '__main__':
thread1 = myServer()
thread2 = Alltest()
thread2.start()
thread1.start()
while thread2.is_alive():
sleep(10)#"allTest is alive,sleep10"
else:
#kill myServer
os.system('taskkill /f /im node.exe')
log.outputLogFile("stop appium server")
1.common.py 共同方法,主要指封装了一些方法,供其他模块使用。
2.DRIVER.py 获取 driver,这里是做成了一个单例模式,run.py 中打开,关闭,其他模块调用。
3.Log.py 中有 2 个类 log 和 myLog,同样也把 myLog 做成了一个单例模式
4.myPhone.py 主要使用了 adb 命令来识别和获取手机参数
5.readConfig.py 是读取配置文件
1.目前异常机制还不够完善,也怪我学艺不精
2.线程是先学的,线程安全之类也没有考虑,目前做到的仅仅是可以用了
3.测试数据参数化还没有实现(这个还要再研究)
4.重要的话说 3 遍:没有人交流!!!
没有人交流!!!
没有人交流!!!
希望大家在看完后,多多题意见
最后附上源码地址:https://github.com/tongshan1/appium_python