使用AppleScript进行简单的自动化测试(三)

  简单的例子

 

  本文通过一个简单的实例介绍AppleScript如何结合inspector进行自动化测试。(*下面将要实现调用系统的计算器进行一个简单的计算,并且判断计算结果是否正确*)

 

Step1.调用计算器

launch application "Calculator"

  

Step2.识别控件

  此时,我们需要用到上一篇中讲到的工具:Accessibility Inspector.

  打开Accessibility Inspector,移动鼠标到我们需要的控件上,比如我们这里指向数字7,按下command+F7,我们会看到相关的属性(如下图)     

       

  

Step3.找控件

  此时,我们可以根据AXRoleDescription和AXTitle得到:button “7”就是我们需要的控件。依此类推,根据层级关系我们开始写代码,但是但我们找到button的AXParent:Group时会发现它没有Title。(此时我们记下它的一个属性:AXChildren 有22个Item)

        

 

一般我们遇到类似的container,我们需要找到它的AXParent(如下图)

       

 

  打开Group的父节点,我们会在AXChildren下面看到有两个Group,进入第一个group,我们会看到它的AXChildren与我们需要的Group的AXChildren不一致,点击Inspector下面的"向上"按钮,于是我们进入第二个Group,此控件正好是我们需要的。因为它没有title,所以我们需要用到Group的index。因为是第二个group,所以:button "7" of group 2 of window “Calculator” 就是我们需要的控件"7"了.

 

  在这里,我们需要注意的一点是:在本地的测试,类似于"Calculator“的字符串是没法用的,那么此时我们就需要用到index了,那么此时可以这样用:button "7" of group 2 of window 1(这里为了方便,button就不用index了)

 

tell application "System Events"
  --调用计算器
launch application
"Calculator" delay 1 tell process "Calculator" if window "Calculator" exists then click button "7" of group 2 of window 1 delay 0.5 click button "+" of group 2 of window 1 delay 0.5 click button "8" of group 2 of window 1 delay 0.5 click button "=" of group 2 of window 1 delay 1 if (value of static text 1 of group 1 of window 1 as integer) = 15 then display dialog "7+8=" & value of static text 1 of group 1 of window 1 end if end if end tell end tell

  

 

通过以上例子的学习后,那么我们自己就可以进行一些简单的自动化操作了,下面是一些常见的检查对象的方法:

  1>判断对象是否存在

if obj exists then
    return true
end if

 

  2>判断对象是否enabled  

if enabled of obj then
      return true
end if

 

  3>判断对象的类型

if class of obj is button then
       return true
end if

 

  4>通过Inspector获取相应的属性

get value of attribute "AXaaaa" of obj

 

  5>获取对象的类型  

get UI Elements of obj

这个方法很好用,有时候Inspector看到的AXRoleDescription同AppleScript认可的对象不一样,比如:AXRoleDescription的值为split group,但是AppleScript Editor认可的class名是:splitter group .此时用上面的方法可以让你省事不少。

 

  6>类型转换: 将A类型转换成B类型

将字符串转换成数字  

123as integer 

  

  

posted on 2014-02-20 11:08  Alvin-x  阅读(1613)  评论(0编辑  收藏  举报

导航