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

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

而且使用图像识别进行自动化,会比使用脚本实现的自动化更接近于真实的手动测试,毕竟人就是通过人眼来识别控件的,所以控件在UI上的变化都会影响到sikuli自动化,而对于BVT级别的自动化,重点还是关注与基本功能是否正常,对控件在UI上的变化还是不那么敏感为好。

在我实现一个产品的BVT自动化的时候,需要操作某些视频的缩略图进行上传到云端,和从云端下载下来的测试,其中大量依赖于其测试素材缩略图的识别,而产品在测试过程中发现了一个缩略图不能正常产生的bug,而这个bug势必会影响到我们其他BVT功能的测试。为了不使得这个bug block我们其他基本功能的验证,我采用的是使用AppleScript获取其测试素材在library中的坐标,然后使用一个工具cliclick来对获取到的坐标进行鼠标左右键的单击和双击等操作。

下面是官网上对这个模拟鼠标和键盘操作的小工具的介绍:

“Cliclick” is short for “Command-Line Interface Click”. It is a a tiny shell/Terminal application that will emulate mouse clicks or series of mouse clicks (including doubleclicks and control-clicks) at arbitrary screen coordinates. Moreover, it lets you move the mouse, get the current mouse coordinates, press modifier keys etc.

首先,使用AppleScript获取这个素材的坐标,(由于一个缩略图的操作焦点应该是其正中央的位置,所以在下面的脚本中要进行一次计算,将中间点的坐标输出):

 1 on run argv
 2     set clip_name to item 1 of argv
 3     tell application "RealTimes"
 4         activate
 5     end tell
 6 
 7     tell application "System Events"
 8         tell process "RealTimes"
 9             tell UI element 0 of scroll area 0 of group 0 of splitter group 0 of splitter group 0 of window 0
10                 tell (1st image whose title is clip_name)
11                     set p to position
12                     set s to size
13                     
14                     set x to ((item 1 of p) + (item 1 of s) / 2)
15                     set y to ((item 2 of p) + (item 2 of s) / 2)
16                     
17                     set output to ("" & x & "," & y)
18                     do shell script "echo " & quoted form of output
19                 end tell
20             end tell
21         end tell
22     end tell
23 end run

在python中,先调用上面的脚本获取坐标,再根据需要的操作使用cliclick的不同参数实现:

 1 def click_item_by_cliclick(name):
 2     command = cliclick_path + " c:{0}"
 3     _run_for_cliclick(name, command)
 4 
 5 
 6 def double_click_item_by_cliclick(name):
 7     command = cliclick_path + " dc:{0}"
 8     _run_for_cliclick(name, command)
 9 
10 
11 def right_click_item_by_cliclick(name):
12     command = cliclick_path + " kd:ctrl c:{0} ku:ctrl"
13     _run_for_cliclick(name, command)
14 
15 
16 def check_exist_by_cliclick(name):
17     try:
18         run_apple_script("get_clip_position_in_library.applescript", name)
19     except Exception:
20         return False
21     return True
22 
23 
24 def _run_for_cliclick(name, command):
25     co = run_apple_script("get_clip_position_in_library.applescript", name).strip().rstrip('
')
26     time.sleep(1)
27     command = command.format(co)
28     ret = exec_command(command)
29     if ret[0] != 0:
30         raise Exception(ret[1])
31 
32 
33 def multiple_select_by_cliclick(names):
34     command = " kd:cmd"
35     for name in names:
36         command += " c:" + run_apple_script("get_clip_position_in_library.applescript", name).strip().rstrip('
')
37     command += " ku:cmd"
38     command = cliclick_path + command
39     ret = exec_command(command)
40     if ret[0] != 0:
41         return False
42     return True

在这里,python充当的就是一个胶水的作用,将AppleScript的输出用在工具cliclick上,并返回结果。

原文地址:https://www.cnblogs.com/liupengblog/p/4650696.html