最近我把之前的 UI 测试新思路与代码整理了一番,
于是有了 Pic-Diff-Recognizer 的诞生。
Pic-Diff-Recognizer 是基于图像差异识别与页面遍历探索的 Selenium UI 自动化测试插件库。
此库诞生的目的是为了让 UI 自动化测试不再需要与页面元素打交道,
而是直接使用真实的视觉差异来判断测试结果是否符合预期。
源码地址:
github: https://github.com/amazingTest/Pic-Diff-Recognizer
gitee: https://gitee.com/amazingTest/Pic-Diff-Recognizer
首先安装开源项目目录下的依赖包:
pip install -r requirements.txt
然后再安装今天的主角:
pip install pic-diff-recognizer
下面给大家放出最佳实践:
from pic_diff_recognizer.searchHandler import SearchHandler
from violent_webdriver import Chrome
from selenium.webdriver.chrome.options import Options
# add some useful options :)
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("disable-infobars")
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
# init driver , executable_path should be your own path!
dr = Chrome.violent_chromedriver(chrome_options=chrome_options,
executable_path='/usr/local/bin/chromedriver')
# init searchHandler
search_handler = SearchHandler(browser=dr)
# search and saving baseline images in current directory named baseline
search_handler.traverse_href(origin_url='https://gitbook.cn/gitchat/author/5cea0bfbb78cc870612d8bba')
# -------------------- assuming after some iterations of current project -----------------------------
search_handler.url_histories = []
# load baseline images
search_handler.picture_handler.load_base_line()
# search and comparing screen shots and baseline images
search_handler.traverse_href(origin_url='https://gitbook.cn/gitchat/author/5cea0bfbb78cc870612d8bba',
compare_baseline_and_screen_shots=True)
# generate_diff_between_base_line_and_screen_shot and output diffResults to current directory
search_handler.picture_handler.generate_diff_between_base_line_and_screen_shot()
# output testReport.txt to current directory
search_handler.picture_handler.export_picture_comparison_result()
可以看到在项目迭代分割线之前,
程序先探索并保存了待测页面的图像。
而在项目迭代分割线之后,
程序再次探索待测页面并对比了图像差异,
最终输出测试报告。
相信这很好的对应了现实项目迭代中的回测节奏。
当然,目前来说此库还有许多可以改进的地方。
源码地址:
github: https://github.com/amazingTest/Pic-Diff-Recognizer
gitee: https://gitee.com/amazingTest/Pic-Diff-Recognizer