#写入数据
import xlrd
import xlwt
from xlutils.copy import copy
dir = os.path.abspath('.').split('src')[0]
'''主要逻辑实现'''
oldWb = xlrd.open_workbook(dir+"/data/考勤系统/考勤系统.xlsx");#先打开已存在的表
newWb = copy(oldWb)#复制
newWs = newWb.get_sheet(2);#取sheet表
newWs.write(2, 4, "pass");#写入 2行4列写入pass
newWb.save(dir+"/result/考勤系统.xls"); #保存至result路径
不copy直接写入原文件的话,会把原来的数据清空。所以要先复制原来的数据下来。
读取数据
for curr_row in range(num_rows):
row = worksheet1.row_values(curr_row)
row = str(row).strip().lstrip('[').rstrip(']')
print (row)
row2 = row.split(sep=",")
print (row2)
# 获取第一列的数据
address = row2[0].strip().lstrip('\'').rstrip(' \'')
print("row%s address is %s" % (curr_row, address))
num1 = curr_row + 1
写入当前时间
now_time = datetime.datetime.now()
# 写入当前时间
currenttime = now_time.strftime('%Y-%m-%d %H:%M:%S')
newWs.write(curr_row, 12, currenttime)
xlutils 2.0.0和Python3.4有些语法不适配
print ()
xrange 改为range
unicode 改为 str
+++++++++++++++++++
对比页面元素是否包含截图
from devicetest.log.variables import VAR
file_path = os.path.join(os.path.abspath(VAR.ResourcePath), "hwBrowser\\image\\dedao.png")
HwBrowser.checkIfImageByViewNode(self.DUT, file1_path=file_path, myCondition='id/file_name', myPath=[-1, 0], FillImageBg=True)(自动填充背景,需要选上,否则对比不出来)
对比图片:
def checkIfImageExist(ad, file1_path, similar=0.9):
# 构造对比图片的绝对路径
myTemplate = os.path.join(os.path.abspath(VAR.ResourcePath), file1_path)
ad.log.error(myTemplate)
# 对当前屏幕进行截图,并保存到report目录
img_path = getScreenHot(ad)
# 开始进行图片对比
result = find_template(ad, img_file=img_path , template_file=myTemplate , threshold=similar)
if result == "notfind":
ad.log.error("当前屏幕中没有找到图片" + myTemplate)
return False
else:
ad.log.error("当前屏幕中找到图片" + myTemplate)
return True
def find_template(ad, img_file , template_file , threshold=0.9):
img = cv2.imread(img_file)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread(template_file)
# print(template.shape)
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
w, h = template_gray.shape[::-1]
res = cv2.matchTemplate(img_gray, template_gray, cv2.TM_CCOEFF_NORMED)
if cv2.minMaxLoc(res)[1] > threshold:
# print( cv2.minMaxLoc(res)[1] )
pass
else:
# print(img_file + "与" + template_file +"找不到匹配的")
# print(res)
return "notfind"
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img, top_left, bottom_right, 255, 2)
return (top_left[0] , top_left[1] , w, h)
def checkIfImageByViewNode(ad, file1_path, myCondition, myPath=None, similar=0.99, ahash=False, FillImageBg=False):
#====================================================================================
#====================================================================================
# 构造对比图片的绝对路径
myTemplate = os.path.join(os.path.abspath(VAR.ResourcePath), file1_path)
ad.log.error(myTemplate)
# 对控件进行截图
if myPath == None:
viewnode = Common.getViewNode(ad, condition=myCondition)
else:
viewnode = Common.getViewNode(ad, condition=myCondition, path=myPath)
# thisdate = ad.droid.Settings.getCurrentSystemTime()
if FillImageBg == False:
img_path = ad.hierarchyviewer.captureViewNode(viewnode=viewnode, name="Node", ext=".png", refresh=False)
else:
img_path = ad.hierarchyviewer.captureViewNode(viewnode=viewnode, name="Node", ext=".png", refresh=False)
img_path = fillImageBackground(ad, img_path)
ad.log.error("控件截图文件:" + img_path)
if ahash == False:
result = find_template(ad, img_file=img_path , template_file=myTemplate , threshold=similar)
else:
result = find_template_ahash(ad, img_file=img_path , template_file=myTemplate , hanming_dis=similar)
if result == "notfind":
ad.log.error("待检测控件截图没有找到图片" + myTemplate)
return False
else:
ad.log.error("待检测控件截图找到图片" + myTemplate)
return True
=====2018.11.26======
共同学习,写下你的评论
评论加载中...
作者其他优质文章