123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- # -*- coding:utf-8 -*-
- import platform
- import os,sys
- reload(sys)
- sys.setdefaultencoding('utf-8')
- import shutil
- import string
- import subprocess
- class Environment():
- # 测试结果相关参数
- resultFolder = "result"
- logFolder = "log"
- detailFolder = "detail"
- screenFolder = "screen"
- scriptFolder = "scripts"
- resultDir = ""
- resultDetailDir = ""
- logDir = ""
- screenDir = ""
- scriptDir = ""
- allResultName = "all-result.xml"
- # 测试平台相关配置参数
- windowsCfg = "Environment_windows.cfg"
- linuxCfg = "Environment_linux.cfg"
- configFolder = "config"
- taskFile = ""
- tv_platform = ""
- projectID = ""
- tvIP = ""
- testType = ""
- taskName=""
- changeProjectIDTaskFile = ""
- changeProjectIDTaskName = "task_change_projectid"
- taskRootDir=""
- HOME_DIR=".."
- ANDROID_TV_SCRIPTS_DIR=""
- ANDROID_TV_TEST_DIR=""
- LINUX_TV_SCRIPTS_DIR=""
- LINUX_TV_TEST_DIR = ""
- tvServerSW = "SmokingTest.jar"
- SDK_Relative_Path = "/Lib/site-packages/ssat_sdk/"
- def getTvSererSWPath():
- return Environment.HOME_DIR + "/../../" + Environment.tvServerSW
- def getTVScriptDir():
- if (Environment.tv_platform == "android"):
- return Environment.ANDROID_TV_SCRIPTS_DIR
- elif (Environment.tv_platform == "linux"):
- return Environment.LINUX_TV_SCRIPTS_DIR
- def getTVTestDir():
- if (Environment.tv_platform == "android"):
- return Environment.ANDROID_TV_TEST_DIR
- elif (Environment.tv_platform == "linux"):
- return Environment.LINUX_TV_SCRIPTS_DIR
- def getPlatformType():
- return platform.system()
- def getFileSeprator():
- sys_str = getPlatformType()
- if (sys_str == "Windows"):
- return "/"
- elif (sys_str == "Linux"):
- return "/"
- else:
- return "/"
- def makeResultDir(inDir):
- sys_str = getPlatformType()
- Environment.resultDir = inDir + getFileSeprator() + Environment.resultFolder
- Environment.logDir = Environment.resultDir + getFileSeprator() + Environment.logFolder
- Environment.resultDetailDir = Environment.resultDir + getFileSeprator() + Environment.detailFolder
- Environment.screenDir = Environment.resultDir + getFileSeprator() + Environment.screenFolder
- Environment.scriptDir = Environment.resultDir + getFileSeprator() + Environment.scriptFolder
- if (os.path.isdir(Environment.resultDir)
- and os.path.isdir(Environment.logDir)
- and os.path.isdir(Environment.resultDetailDir)
- and os.path.isdir(Environment.screenDir)
- and os.path.isdir(Environment.scriptDir)):
- return Environment.resultDir
- os.mkdir(Environment.resultDir)
- os.mkdir(Environment.logDir)
- os.mkdir(Environment.screenDir)
- os.mkdir(Environment.scriptDir)
- os.mkdir(Environment.resultDetailDir)
- return Environment.resultDir
- def getFileFromAndroidPath(filepath):
- strarr = string.split(filepath, '/')
- filename = strarr[strarr.__len__() - 1]
- return filename
- def getFileFromLocalPath(filepath):
- strarr = string.split(filepath, getFileSeprator())
- filename = strarr[strarr.__len__() - 1]
- return filename
- def getTaskNameFromFile(taskfile):
- filename = getFileFromLocalPath(taskfile)
- strarr2 = string.split(filename, '.')
- taskname = strarr2[0]
- return taskname
- def appendFilePath(dir, filepath):
- filename = getFileFromAndroidPath(filepath)
- sys_str = getPlatformType()
- return dir + getFileSeprator() + filename
- def getFileSuffix(filepath):
- filename = getFileFromAndroidPath(filepath)
- strarr2 = string.split(filename, '.')
- suffix = strarr2[strarr2.__len__() - 1]
- print "Current file suffix:", suffix
- return suffix
- def getDirFromLocalFilePath(filepath):
- print "getDirFromLocalFilePath,filepath=", filepath
- strarr = string.split(filepath, getFileSeprator())
- fileDir = ""
- for index in range(0, strarr.__len__() - 1):
- if (index == 0):
- fileDir = strarr[0]
- else:
- fileDir = fileDir + getFileSeprator() + strarr[index]
- return fileDir
- def copyFile(filepath, destdir):
- sys_str = getPlatformType()
- filepath = Environment.HOME_DIR + getFileSeprator() + Environment.configFolder + getFileSeprator() + filepath
- destFile = destdir + getFileSeprator() + getFileFromLocalPath(filepath)
- print "copyFile,destFile=",destFile
- shutil.copyfile(filepath, destFile)
- def setEnvironment(info):
- strarr = info.split("=")
- print "setEnvironment,", strarr
- if (strarr.__len__() == 2):
- if (strarr[0] == "resultDir"):
- Environment.resultDir = strarr[1]
- elif (strarr[0] == "taskFile"):
- Environment.taskFile = strarr[1]
- Environment.taskName = getTaskNameFromFile(Environment.taskFile)
- Environment.taskRootDir = getDirFromLocalFilePath(Environment.taskFile)
- Environment.changeProjectIDTaskFile = Environment.taskRootDir + getFileSeprator() + Environment.changeProjectIDTaskName + ".xml"
- elif (strarr[0] == "tv_platform"):
- Environment.tv_platform = strarr[1]
- elif (strarr[0] == "projectID"):
- Environment.projectID = strarr[1]
- elif (strarr[0] == "tvIP"):
- Environment.tvIP = strarr[1]
- elif (strarr[0] == "testType"):
- Environment.testType = strarr[1]
- elif (strarr[0] == "ANDROID_TV_SCRIPTS_DIR"):
- Environment.ANDROID_TV_SCRIPTS_DIR = strarr[1]
- elif (strarr[0] == "ANDROID_TV_TEST_DIR"):
- Environment.ANDROID_TV_TEST_DIR = strarr[1]
- elif (strarr[0] == "LINUX_TV_SCRIPTS_DIR"):
- Environment.LINUX_TV_SCRIPTS_DIR = strarr[1]
- elif (strarr[0] == "HOME_DIR"):
- Environment.HOME_DIR = strarr[1]
- def getConfigFilePath(config):
- return Environment.HOME_DIR + getFileSeprator() + Environment.configFolder + getFileSeprator() + config
- def parseConfig(config):
- configpath = getConfigFilePath(config)
- print "parseConfig,configpath=",configpath
- cfgFile = open(configpath,"r",-1)
- strline = "init"
- while (strline <> None and strline <> ""):
- strline = cfgFile.readline().strip("\n").strip("\r")
- print "position=", cfgFile.tell(), ";line:", strline
- info = strline.decode("utf-8", "error")
- #防止文本读取结束时,“#”判断出错
- if (info.__len__() > 1 and info[0] <> "#"):
- #print "position=", cfgFile.tell(), ";line:", strline
- setEnvironment(strline)
- cfgFile.close()
- def getScriptsConfig(config):
- print "getScriptsConfig,config=", config
- conDic = {}
- cfgFile = open(config, "r", -1)
- strline = "init"
- while (strline <> None and strline <> ""):
- strline = cfgFile.readline().strip("\n").strip("\r")
- info = strline.decode("utf-8", "error")
- # 防止文本读取结束时,“#”判断出错
- if (info.__len__() > 1 and info[0] <> "#"):
- print "position=", cfgFile.tell(), ";line:", info.encode("utf-8")
- strarr = info.split("=")
- print "getConfig,", strarr
- if (strarr.__len__() > 1):
- conDic[strarr[0]] = strarr[1]
- cfgFile.close()
- return conDic
- def loadConfig():
- sys_str = getPlatformType()
- if (sys_str == "Windows"):
- parseConfig(Environment.windowsCfg)
- elif (sys_str == "Linux"):
- parseConfig(Environment.linuxCfg)
- else:
- parseConfig(Environment.linuxCfg)
- #在一行长文本中,用空格分开字符串,寻找第findNumber个 数字字符串,
- def findNumber(findNumber, strinfo):
- print strinfo
- strarr = string.split(strinfo, " ")
- print strarr
- findCount = 0
- for index in range(0, strarr.__len__()):
- if (strarr[index].__len__() > 0):
- flag = False
- for char in strarr[index]:
- if (char >= '0' and char <= '9'):
- flag = True
- continue
- else:
- flag = False
- break
- if (flag == True):
- findCount = findCount + 1
- print "findNumber=",findNumber, ";;findCount=", findCount, ",str=", strarr[index]
- if (findCount == findNumber):
- return strarr[index]
- return ""
- def checkTVTestProcessAndClose():
- sys_str = getPlatformType()
- if (Environment.tv_platform == "android" and sys_str == "Windows"):
- cmd = "adb -s " + Environment.tvIP + ":5555 shell ps| find \"uiautomator\""
- print cmd
- cprocess = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
- resstr = cprocess.stdout.read()
- uaPid = findNumber(1, resstr)
- cmd = "adb -s " + Environment.tvIP + ":5555 shell kill " + uaPid
- print cmd
- krocess = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
- resstr = krocess.stdout.read()
- '''
- 根据传入的进程id,检测进程是否存在
- '''
- def checkProcess(pid):
- cmd = "tasklist | findstr " + str(pid)
- cprocess = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
- psInfo = cprocess.stdout.readline()
- if psInfo.__len__() > 0:
- return True
- else:
- return False
|