ATX SikuliX 使用笔记

codeskyblue · 2018年12月11日 · 最后由 hope1 回复于 2022年01月17日 · 7671 次阅读

前言

Sikuli 是通过图像识别在应用的自动化的,完全的黑盒,适用于很多应用的测试
最初接触大概是 2014 年,今天又翻开看了看,发现这东西一直都在维护,现在名字叫 SikuliX 了,功能越来越强。正好项目组需要用到 Windows 自动化,重新学习起来

因为 SikuliX 是用 Java 写的,如果需要用其他语言写脚本,需要用到 Jython 或者 JRuby, Javascript(Rhino)
这里我的笔记里面用的是Python

使用记录

这里我用的是 1.1.3
截图如下
SikuliXIDE截图

常用快捷键

  • 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

图片查找和鼠标操作

图像查找接口

  • has 这个函数据说是对 exists 函数的一个封装。具体怎么不一样不太清楚了
  • findChange 返回跟原图的差异地方,list of areas

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)

App 操作

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

监控和事件 Observe

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 不在当前目录也可以。

其他

边看边补充,文章先发了。

附言 1  ·  2019年04月19日

version 1.1.4 is not very stable, recommend using 1.1.3

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 3 条回复 时间 点赞

哈哈~ 目前项目就是用 sikulix 搭建起来,一套脚本兼容 win 和 mac,不过 头疼的是 图片的管理和不同分辨率的兼容。

codeskyblue 专栏文章:2018年 终总结 中提及了此贴 02月18日 10:26

还几年前用过了,这玩意居然还在更新,神奇~~

linux 环境是不是也支持

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册