Sikuli 是通过图像识别在应用的自动化的,完全的黑盒,适用于很多应用的测试
最初接触大概是 2014 年,今天又翻开看了看,发现这东西一直都在维护,现在名字叫 SikuliX 了,功能越来越强。正好项目组需要用到 Windows 自动化,重新学习起来
因为 SikuliX 是用 Java 写的,如果需要用其他语言写脚本,需要用到 Jython 或者 JRuby, Javascript(Rhino)
这里我的笔记里面用的是Python
这里我用的是 1.1.3
截图如下
Alt+Shift+C
停止脚本运行Ctrl+R
运行Ctrl+Alt+R
单步执行Ctrl+T
显示/隐藏缩略图Ctrl+L
显示隐藏命令列表openApp(someApp) # we use an application someApp
click(imageButton) # we click some button
wait(imageExpected1) # we wait for the app to react and show the expected result on the screen
type(“some text”); type(Key.ENTER) # we fill in some text and press ENTER
wait(imageExpected2) # again we wait for some expected reaction or result
click(…) # we click …
wait(…) # we wait …
type(…) # we type …
wait(…) # we wait …
保存图片到本地 源码链接
# 保存图片,其中图片扩展名只能是.png, _ 开头的文件名
Screen().capture(Region(343,97,1425,881)).saveInBundle("g") # save as _g.png
从文档中很少看到默认值是啥,所以除非我备注写了,一般我都是不知道默认值的
https://sikulix-2014.readthedocs.io/en/latest/scripting.html#Settings
Settings.MoveMouseDelay = 0.5 # default,鼠标移动速度,如果设置为0,鼠标将瞬间跳转过去
Settings.ClickDelay # 点击时长,最大1s
Settings.TypeDelay # 输入延迟,最大1s
Settings.ObserveScanRate = 0.2 # 每秒监控的次数,也就是每5s检查一次
Settings.AlwaysResize = 0.5 # 匹配图片前,先将匹配图片宽高缩小一倍。通常用来匹配不同分辨率的设备
Settings.ImageCallback # 搜索前的回调,一般可以用来修改原图 https://sikulix-2014.readthedocs.io/en/latest/scripting.html#Settings.ImageCallback
图像查找接口
findAll
def by_y(match):
return match.y
icons = findAll("png_icon.png")
# sort the icons by their y coordinates and put them into a new variable sorted_icons
sorted_icons = sorted(icons, key=by_y)
# another shorter version is using lambda.
sorted_icons = sorted(icons, key=lambda m:m.y)
for icon in sorted_icons:
pass # do whatever you want to do with the sorted icons
Docs: https://sikulix-2014.readthedocs.io/en/latest/region.html#Region.findAll
文本查找接口
findAny
, findBest
# find the best matching pattern of the given list of patterns
result = findBest(pattern, pattern, pattern, ...) # var-arg parameterlist
result = findBestList(List<patterns>) # a list of patterns
# find all matching patterns in the list
result = findAny(pattern, pattern, pattern, ...) # var-arg parameterlist
result = findAnyList(List<patterns>) # a list of patterns
https://sikulix-2014.readthedocs.io/en/latest/interaction.html
popup("Hello World!\nHave fun with Sikuli!") # 弹出一个消息
answer = popAsk("Should we really continue?")
if not answer:
exit(1)
openApp
# Windows: run a batch file in a new command window:
openApp("cmd.exe /c start path-to-some.bat")
# Windows: opens Firefox (full path specified)
openApp(r"c:\Program Files\Mozilla Firefox\firefox.exe")
# Mac: opens Safari
openApp("Safari")
switchApp
# Windows: switches to an already opened Firfox or opens it otherwise
switchApp("c:\\Program Files\\Mozilla Firefox\\firefox.exe")
# Windows: switches to the frontmost opened browser window (or does nothing
# if no Firefox window is currently opened)
# works, because all Firefox window titles contain "Mozilla Firefox"
switchApp("Mozilla Firefox")
# Mac: switches to Safari or starts it
switchApp("Safari")
closeApp
# Windows: closes Firefox if it is running, does nothing otherwise
closeApp("c:\\Program Files\\Mozilla Firefox\\firefox.exe")
# Windows: stops firefox including all its windows
closeApp("Mozilla Firefox")
# Mac: closes Safari including all its windows
closeApp("Safari")
更多的操作可以看 https://sikulix-2014.readthedocs.io/en/latest/appclass.html#App
https://sikulix-2014.readthedocs.io/en/latest/region.html#observing-visual-events-in-a-region
def myHandler(event): # you can choose any valid function name
# event: can be any variable name, it references the ObserveEvent object
pass # add your statements here
onAppear("path-to-an-image-file", myHandler) # or any other onXYZ()
observe(10) # observes for 10 seconds
# observe(FOREVER)
https://sikulix-2014.readthedocs.io/en/latest/region.html#exception-findfailed
setFindFailedResponse(PROMPT)
加上这行代码之后,每当出现图像匹配出错的时候就会蹦出来一个对话框
问你想怎么处理,是跳过,还是重试,还是直接结束掉脚本。
最新的版本还提供了重新截图的功能(比较适合图片需要更新的情况,感觉很好用)
1.1.3 版本命令行运行
java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -Dsikuli.FromCommandLine -jar sikulix.jar -r some-app.sikuli
1.1.4 版本
参考 https://sikulix-2014.readthedocs.io/en/latest/faq/010-command-line.html
其中倒数第三个参数 sikulix.jar
不在当前目录也可以。
边看边补充,文章先发了。
version 1.1.4 is not very stable, recommend using 1.1.3