Skip to content

Commit

Permalink
feat(更新图片Diff算法,采用差异哈希算法计算图片相似度):
Browse files Browse the repository at this point in the history
  • Loading branch information
uct8086 committed Apr 16, 2021
1 parent 2ddcd5b commit 4da2d2b
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/wxa-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* 暂无
* 待优化&计划:
* 截屏用的图片比对条件过于苛刻,稍微有些偏移就报错;理想情况:整体页面稍微有些偏移不应报错,因小程序rpx单位渲染,有小数点的时候,每次渲染不能保证位置一致,导致整体页面有位移,需调整图片比对参数,试下能不能忽略偏移。
* -r录制模式也自动用开发者工具打开当前项目,每次录制完生成用例后,自动回到小程序首页
* ~~-r录制模式也自动用开发者工具打开当前项目,每次录制完生成用例后,自动回到小程序首页~~
* 不带-r参数时,即回放模式,仅添加元素id(回放测试用例时能找到对应元素),不侵入过多代码(现在会劫持各种tap等事件,植入全局按钮组件)
* 高大上网页版测试报告
* 【用例复用】:支持用例复用-公共用例不用重复录制(如:登陆模块只需要录制一次,其他用例复用,当登录模块更改时,只需要重新录制一次登录,不需要每个用例都重新录制)
Expand Down
5 changes: 5 additions & 0 deletions packages/wxa-cli/scripts/buildLib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ rm -f ./dist/tester/wxa-e2eTest/e2eTpl.ejs
rm -rf ./lib-dist/wxa-e2eTest
mkdir -p ./lib-dist/wxa-e2eTest
mkdir -p ./dist/tester/wxa-e2eTest
mkdir -p ./dist/tester/imageSimilarity
ln ./src/tester/wxa-e2eTest/state.js ./lib-dist/wxa-e2eTest/state.js
ln ./src/tester/wxa-e2eTest/e2eTestSuite.js ./lib-dist/wxa-e2eTest/e2eTestSuite.js
ln ./src/tester/wxa-e2eTest/e2eMockWxMethod.js ./lib-dist/wxa-e2eTest/e2eMockWxMethod.js
ln ./src/tester/wxa-e2eTest/e2eRecordBtn.wxa ./lib-dist/wxa-e2eTest/e2eRecordBtn.wxa
ln ./src/tester/wxa-e2eTest/e2eTestCaseTpl.ejs ./dist/tester/wxa-e2eTest/e2eTestCaseTpl.ejs
ln ./src/tester/wxa-e2eTest/e2eTpl.ejs ./dist/tester/wxa-e2eTest/e2eTpl.ejs

ln ./src/tester/imageSimilarity/dHash.py ./dist/tester/imageSimilarity/dHash.py
ln ./src/tester/imageSimilarity/init.py ./dist/tester/imageSimilarity/init.py

echo $MODE;
if test "$MODE" = 'dev'
then
Expand Down
53 changes: 53 additions & 0 deletions packages/wxa-cli/src/tester/imageSimilarity/dHash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from PIL import Image
# This module can classfy the image by dHash
class dHash():
def getCode(self,img,size):

result = []
x_size = size[0]-1#width
y_size = size[1] #high
for x in range(0,x_size):
for y in range(0,y_size):
now_value = img.getpixel((x,y))
next_value = img.getpixel((x+1,y))

if next_value < now_value:
result.append(1)
else:
result.append(0)


return result



def compCode(self,code1,code2):
num = 0
for index in range(0,len(code1)):
if code1[index] != code2[index]:
num+=1
return num

def classfiy_dHash(self,image1,image2,size=(9,8)):
''' 'image1' and 'image2' is a Image Object.
You can build it by 'Image.open(path)'.
'Size' is parameter what the image will resize to it and then image will be compared to another image by the dHash.
It's 9 * 8 when it default.
The function will return the hamming code,less is correct.
'''
image1 = image1.resize(size).convert('L')
code1 = self.getCode(image1, size)


image2 = image2.resize(size).convert('L')
code2 = self.getCode(image2, size)

assert len(code1) == len(code2),"error"
print(self.compCode(code1, code2))
return self.compCode(code1, code2)



# __all__=[classfiy_dHash]

72 changes: 72 additions & 0 deletions packages/wxa-cli/src/tester/imageSimilarity/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import json
from PIL import Image
from dHash import dHash

cwd_path = os.getcwd()
target_path = cwd_path + '\\__wxa_e2e_test__'

class DiffImages():

def __init__(self):
self.case_result = []
self.dHash = dHash()

#读取excel文件
def FindAndDiffImgs(self):

dirnames = self.GetDirList(cur_path = target_path, cur_type = "dir")
for cur_dir in dirnames:
obj = {}
obj['case_name'] = cur_dir # 当前用例
cur_base = target_path + "\\" + cur_dir + "\\base_screenshot\\"
cur_replay = target_path + "\\" + cur_dir + "\\replay_screenshot\\"
base_img_list = self.GetDirList(cur_path = cur_base, cur_type = "file")
replay_img_list = self.GetDirList(cur_path = cur_replay, cur_type = "file")
for base_img in base_img_list:
img_obj = {'name': base_img}
exist_replay = replay_img_list.count(base_img)
if exist_replay > 0:
image1 = self.GetImage(img_path = cur_base + base_img)
image2 = self.GetImage(img_path = cur_replay + base_img)
diff_result = self.dHash.classfiy_dHash(image1=image1, image2=image2, size=(414, 688))
img_obj['diff_result'] = diff_result
else:
diff_result = -1 #图片不存在,对比失败
img_obj['diff_result'] = diff_result
obj[base_img] = img_obj
self.case_result.append(obj)
print(self.case_result)
return self.WriteFile()


#上报Case结果
# def WriteJsonFile(self, params):

def GetImage(self, img_path):
return Image.open(img_path)

def GetDirList(self, cur_path, cur_type):
# 所有文件及目录
names = os.listdir(cur_path)
dirnames = None
if cur_type == 'dir':
# 测试用例目录
dirnames = [name for name in names if os.path.isdir(os.path.join(cur_path, name)) and name != '.cache']
elif cur_type == 'file':
dirnames = [name for name in names if os.path.isfile(os.path.join(cur_path, name))]
print(dirnames)
return dirnames
def WriteFile(self):
json_path = target_path+ "\\diff_result.json"
if os.path.exists(json_path):
os.remove(json_path)
with open(json_path, 'xt', encoding='utf-8') as f:
f.write(json.dumps(self.case_result,ensure_ascii=False,sort_keys=True, indent=4))




if __name__ == '__main__':
di = DiffImages()
di.FindAndDiffImgs()
13 changes: 13 additions & 0 deletions packages/wxa-cli/src/tester/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import path from 'path';
import E2ETester from './e2eTester';
import {execSync} from 'child_process';

const pyPath = path.join(__dirname, './imageSimilarity/init.py');

export default class TesterController {
constructor(cmdOptions, wxaConfigs) {
Expand All @@ -11,4 +15,13 @@ export default class TesterController {
return new E2ETester(this.cmdOptions, this.wxaConfigs).build();
}
}

// 测试结果图片校准
async diff() {
execSync(`python "${pyPath}"`, {
stdio: 'inherit',
});
return process.exit(0);
}

}
10 changes: 10 additions & 0 deletions packages/wxa-cli/src/wxa.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ commander
new Tester(cmd, wxaConfigs).build();
});

commander
.command('test-diff')
.description('测试结果校准')
.action((cmd)=>{
logger.info('Hey', `This is ${chalk.keyword('orange')('wxa@'+version)}, Running in ${chalk.keyword('orange')(process.env.NODE_ENV || 'development')}, Tester diff Mode`);
// logger.info(cmd);
let wxaConfigs = getConfigs();
new Tester(cmd, wxaConfigs).diff();
});

commander
.command('cli')
.description('微信开发者工具命令行调用')
Expand Down

0 comments on commit 4da2d2b

Please sign in to comment.