Repository navigation
Expand file tree
/
Copy pathBaseIosLog.py
More file actions
180 lines (151 loc) · 5.5 KB
/
Copy pathBaseIosLog.py
File metadata and controls
180 lines (151 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import logging
import time
import threading
from Base.BaseIosPhone import get_ios_PhoneInfo
from Base.BaseRunner import *
import os
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
class iOSLog:
def __init__(self, duid):
get_phone = get_ios_PhoneInfo(duid)
phone_name = get_phone["device"] + "_" + get_phone["release"] + "_" + "iOS" + "_" + get_phone["duid"]
global logger, resultPath, logPath
resultPath = PATH("../Log/")
logPath = os.path.join(resultPath, (phone_name + "_" + time.strftime('%Y%m%d%H%M%S', time.localtime())))
if not os.path.exists(logPath):
os.makedirs(logPath)
self.checkNo = 0
self.logger = logging.getLogger()
self.logger.setLevel(logging.INFO)
# create handler,write log
fh = logging.FileHandler(os.path.join(logPath, "outPut.log"))
# Define the output format of formatter handler
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
self.logger.addHandler(fh)
# 获取系统日志,过滤当前app的log,如不需要获取系统日志,注掉即可
crashPath = os.path.join(PATH("../Log/CrashInfo/iOS/"))
if not os.path.exists(crashPath):
os.makedirs(crashPath)
syslog_path = os.path.join(crashPath, "syslog.log")
sys_cmd = 'idevicesyslog -u ' + get_phone["duid"] + " |grep 'XiaoYing' > %s" % (syslog_path)
os.popen(sys_cmd)
def getMyLogger(self):
"""get the logger
:return:logger
"""
return self.logger
def buildStartLine(self, caseNo):
"""build the start log
:param caseNo:
:return:
"""
startLine = "---- " + caseNo + " " + " " + \
" ----"
# startLine = "---- " + caseNo + " " + "START" + " " + \
# " ----"
self.logger.info(startLine)
def buildEndLine(self, caseNo):
"""build the end log
:param caseNo:
:return:
"""
endLine = "---- " + caseNo + " " + "END" + " " + \
" ----"
self.logger.info(endLine)
self.checkNo = 0
def writeResult(self, result):
"""write the case result(OK or NG)
:param result:
:return:
"""
reportPath = os.path.join(logPath, "report.txt")
flogging = open(reportPath, "a")
try:
flogging.write(result + "\n")
finally:
flogging.close()
pass
def resultOK(self, caseNo):
self.writeResult(caseNo + ": OK")
def resultNG(self, caseNo, reason):
self.writeResult(caseNo + ": NG--" + reason)
def checkPointOK(self, driver, caseName, checkPoint):
"""write the case's checkPoint(OK)
:param driver:
:param caseName:
:param checkPoint:
:return:
"""
self.checkNo += 1
self.logger.info("[CheckPoint_" + str(self.checkNo) + "]: " + checkPoint + ": OK")
print("==用例_%s检查点成功==" % caseName)
# take shot 默认去掉成功截图
# self.screenshotOK(driver, caseName)
def checkPointNG(self, driver, caseName, checkPoint):
"""write the case's checkPoint(NG)
:param driver:
:param caseName:
:param checkPoint:
:return:
"""
self.checkNo += 1
self.logger.info("[CheckPoint_" + str(self.checkNo) + "]: " + checkPoint + ": NG")
# take shot
return self.screenshotNG(driver, caseName)
def screenshotOK(self, driver, caseName):
"""screen shot
:param driver:
:param caseName:
:return:
"""
screenshotPath = os.path.join(logPath, caseName)
screenshotName = "CheckPoint_" + str(self.checkNo) + "_OK.png"
# wait for animations to complete before taking screenshot
time.sleep(1)
# driver.get_screenshot_as_file(os.path.join(screenshotPath, screenshotName))
driver.get_screenshot_as_file(os.path.join(screenshotPath + screenshotName))
def screenshotNG(self, driver, caseName):
"""screen shot
:param driver:
:param caseName:
:return:
"""
screenshotPath = os.path.join(logPath, caseName)
screenshotName = "CheckPoint_" + str(self.checkNo) + "_NG.png"
# wait for animations to complete before taking screenshot
time.sleep(1)
driver.get_screenshot_as_file(os.path.join(screenshotPath + screenshotName))
return os.path.join(screenshotPath + screenshotName)
def screenshotERROR(self, driver, caseName):
"""screen shot
:param driver:
:param caseName:
:return:
"""
screenshotPath = os.path.join(logPath, caseName)
screenshotName = "ERROR.png"
# wait for animations to complete before taking screenshot
time.sleep(1)
driver.get_screenshot_as_file(os.path.join(screenshotPath, screenshotName))
class myIosLog:
"""
This class is used to get log
"""
log = None
mutex = threading.Lock()
def __init__(self):
pass
@staticmethod
def getLog(duid):
if myIosLog.log is None:
myIosLog.mutex.acquire()
myIosLog.log = iOSLog(duid)
myIosLog.mutex.release()
return myIosLog.log
if __name__ == "__main__":
pass
# logTest = myIosLog.getLog("5214866ccb9342f87f4c2aab093c25f7e252fd85")
# logger = logTest.getMyLogger()