下面讲一下Sikuli的重要概念,就是region,所谓region就是Sikuli在进行图像识别的时候的一个区域,默认是整个屏幕。

当然,如果region选得太大的话,并且UI上存在相似的控件,那么就会造成图像识别的错误。而且region选得过大也会使得代码运行速度下降。

我在实际应用中,region选的是屏幕中间的工作区域,也就是除了最上方的global menu 和 system tray区域,和下方的dock区域,并且在被测程序启动后,将其最大化,以占满中间的工作区,防止其他应用的UI干扰测试运行。

下面的代码就是根据AppleScript获取当前屏幕大小,与dock大小,然后用这些值构造Region对象:

from sikuli import *
import helper # Get width and heigth of screen by applescripts
width_of_screen = helper.get_bounds_of_screen()[0]
height_of_screen = helper.get_bounds_of_screen()[1] # Height of top menu bar
height_of_top_menu_bar = 24 # Get height of dock by applescripts
height_of_dock = helper.get_height_of_dock() # Region of screen, without top menu bar and dock
region = Region(0, height_of_top_menu_bar, width_of_screen,
height_of_screen - height_of_top_menu_bar - height_of_dock)
print "region size is ({0}, {1}, {2}, {3})".format(region.x, region.y, region.w, region.h)

其中get_bounds_of_screen获取屏幕大小,使用的是下面的AppleScript:

tell app "Finder" to get bounds of window of desktop

获取dock的高度get_height_of_height_of_dock使用的是这个AppleScript:

tell application "System Events" to tell process "Dock"
set dock_dimensions to size in list 1
set dock_height to item 2 of dock_dimensions
end tell

然后计算出工作区大小,构造region对象:

region = Region(0, height_of_top_menu_bar, width_of_screen,
height_of_screen - height_of_top_menu_bar - height_of_dock)

当然测试system tray的功能也是必不可少的,那么就需要再准备一个对应最上方global menu区域的region:

# Height of top menu bar
height_of_top_menu_bar = 24 # Region for top menu bar region
top_menu_bar_region = Region(
0, 0, helper.get_bounds_of_screen()[0], height_of_top_menu_bar)

region构造完成之后,就可以使用它来进行click,doubleclick,drag&drop的操作了,传递的参数就是一个截图:

在我的实际应用中,是将所有的截图都放到一个统一的screenshots的sikuli文件中,其他模块再去导入它:

这样的好处是它就像一个描述UI的ID,Name或者是父子关系的一个xml文件一样,当有UI变化的时候,不必牵连到其他模块的修改,只需将screenshots模块中对应的image更改即可。

另外,sikuli的图像识别是有一个相似度的,如果不满意,可以在sikuli IDE上点击图片进行修改:

下面的红色高亮表示的就是识别到得区域:

由于sikuli的IDE的功能简单,稳定性差,我在实际工作中是不会使用它来coding的,当你需要一个sikuli文件时,打开sikuli的IDE并new一个空文件并保存,这个文件就可以通过右键的“show package contents”打开它,看到它就是一个包含py文件和html文件的一个文件夹:

其中html文件的内容才是sikuli IDE中显示的内容,但是实际运行时与这个html没关系,使用的是py文件编译后的java字节码文件。

所以我们可以用任何编辑器编辑py文件,而不管html文件是什么内容,除非你想用这个IDE来方便的截图与修改图像匹配度。

下面的截图是我用sublime text打开的一个项目,可以看到sikuli文件就是一个文件夹:

下面是一些我常用的AppleScript脚本:

1. 最大化(非全屏)一个app,需要用到dock的高度:

tell application "Finder"
set screenResolution to bounds of window of desktop
end tell set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution tell application "System Events" to tell process "Dock"
set dock_dimensions to size in list 1
set dock_height to item 2 of dock_dimensions
end tell tell application "System Events" to tell process "RealTimes"
activate
set position of window 1 to {0, 24}
set size of window 1 to {screenWidth, screenHeight - dock_height - 24}
end tell

2. 通过AppleScript将Finder定位到一个路径上去,并且将Finder窗口放到屏幕工作区域的右半边:

#! /usr/bin/osascript

on run(arguments)
set myDocumentFolder to POSIX path of (first item of arguments) tell application "Finder"
close every window
activate
make new Finder window
set toolbar visible of the front Finder window to false
set statusbar visible of the front Finder window to false
set the current view of front Finder window to icon view
set the target of the front Finder window to (POSIX file myDocumentFolder)
set screenResolution to bounds of window of desktop
end tell set screenWidth to item 3 of screenResolution
set screenHeight to item 4 of screenResolution tell application "System Events" to tell process "Dock"
set dock_dimensions to size in list 1
set dock_height to item 2 of dock_dimensions
end tell tell application "Finder"
activate
set frontmost to true
set bounds of the front Finder window to {screenWidth / 2, 24, screenWidth, screenHeight - dock_height}
end tell
end run

当要从一个Finder中选择一个文件拖放到某个App中的时候,这个脚本就很有用了。

Mac下的UI自动化测试 (二)的更多相关文章

  1. Mac下的UI自动化测试 (一)

    在我看来,实现UI自动化测试的过程一向都是令人快乐的事情,而维护它们就是跟噩梦一样了,尤其是对每次CI产生的build进行BVT测试,由于开发不会告诉你任何UI的变化,那么你拿到的测试结果就势必会一片 ...

  2. Mac下的UI自动化测试 (三)

    使用sikuli进行UI自动化测试固然是方便很多,不用一切都使用AppleScript那烦人的语法,只要界面的UI没有变化,结构的变化不会影响到基于sikuli的自动化,但是基于AppleScript ...

  3. Mac下的UI自动化测试 (四)

    在实际写testcase的时候会使用unittest框架,但是在sikuli中需要使用它提供的command来运行,位于/Applications/SikuliX.app/run,使用-r参数指定要运 ...

  4. Mac 下纯lua(二)

    Lua库 基本函数 assert(v,[,message]) 当v时false时,返回message assert(money >0,"error -1001"); coll ...

  5. 使用phantomjs进行无界面UI自动化测试

    PhantomJS(http://phantomjs.org/) 是一个基于WebKit的服务器端JavaScript API.它全面支持web而不需浏览器支持,其快速.原生支持各种Web标准:DOM ...

  6. Jenkins下构建UI自动化之初体验

    一.缘 起 笔者之前一直在Windows环境下编写UI自动化测试脚本,近日在看<京东系统质量保障技术实战>一书中,萌生出在jenkins下构建UI自动化测试的想法 二.思 路 首先,在Li ...

  7. [原创]浅谈Web UI自动化测试

    [原创]浅谈Web UI自动化测试 Web UI自动化测试相信大家都不陌生,今天来谈谈这个,我最早接触自动化测试时大约是在2004年,2006年当时在腾讯财付通算是开始正式接触自动化测试,之所以是正式 ...

  8. 【Mac + Appium + Python3.6学习(二)】之Android自动化测试,appium-desktop配置和简易自动化测试脚本

    上一篇文章介绍安装appium测试环境,这一片研究介绍如何测试Android自动化. 上一篇地址:<[Mac + Appium学习(一)]之安装Appium环境> 这一篇参考:<Ma ...

  9. RF+Appium框架自动化测试系列一之(Mac下Appium环境搭建)万事开头难

    消失了3个月,有一段时间没来园子更新博客了,各位看官见谅哈哈,消失是因为刚换了工作环境没外网,好多笔记没能及时的记录分享,以后有时间慢慢补上吧,这段时间主要接触了移动端app的自动化测试,公司为了快速 ...

随机推荐

  1. python的编解码问题

    http://blog.chinaunix.net/uid-27838438-id-4227131.html

  2. unitychan-crs 头发随动脚本

    // //SpringCollider for unity-chan! // //Original Script is here: //ricopin / SpringCollider.cs //Ro ...

  3. 解决sql脚本文件太大无法打开的问题

    as we known,sql数据库高版本向低版本还原是不太可能但是又经常会碰到的事,今天实测了一种方法 步骤:任务—>生成脚本—> 下一步->高级,选择数据库版本和编写脚本数据类型 ...

  4. 【转载】Linux 进程调度时间测量

    测试Context Switch time(进程上下文切换时间) --------------------------------------------------     创建两个进程(实时进程) ...

  5. DataGridview刷新异常的问题

    datsSet 绑定到dataGrieView,在刷新dataSet的数据时,常会bug:索引0没有值或索引(int)x没有值 昨天弄了一个下午,发现bug原因: dataGridView中有数据时, ...

  6. SUSE 设置IP地址、网关、DNS

    说明: ip:172.18.4.107 子网掩码:255.255.255.0 网关:172.18.4.254 dns:172.18.0.6 1.设置ip地址 vi /etc/sysconfig/net ...

  7. mysql添加用户

    增加新用户: 格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码" 例1.增加一个用户test1密码为abc,让他可以在任 ...

  8. centos7的xfs配置

    XFS是扩展性高.高性能的文件系统.也是rhel7/centos7的默认文件系统.XFS支持metadata journaling,这使其能从crash中更快速的恢复.它也支持在挂载和活动的状态下进行 ...

  9. Linux 命令及获取帮助 目录文件浏览,管理和维护

    开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 使用快捷键切换到虚拟终端2,使用普通用户身份登录,查看系统提示符 $ 使用命令退出虚拟终端2上登录的用户 exit ...

  10. fontconfig

    vlc-android 默认是 禁用 fontconfig 的 如果想要使用的话需要手动修改 compile.sh