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

  AppleScript的方法与应用

1.连接网络:在做自动化测试有时需要从服务器上下载build,那么我们需要连接网络  

mount volume "smb://xx.xx.xx.xx/xx/xx" as user name "userName" with password "password"

  连接成功后,会在xx's Mac 下看到映射的地址.

2.launch 程序:

  1>dmg:    

tell application "Finder"
  open document file "System:Users:zzz:Desktop:xx.dmg"

  2>app:

launch application "System:Users:zzz:Desktop:Accessibility Inspector.app"

 

3.打开URL:进入百度首页

open location "http://www.baidu.com"

   

4.获取第一个窗体的process name  

set frontApp to name of first application process whose frontmost is true

  

5.获取所有可见app的process name 

set visibleApps to application process whose visible is true

6.获取随机数

random number from 1 to 4

7.时间

tell application "System Events"
    set tmpDate to (current date)
  
    get seconds of tmpDate  --秒

    get minutes of tmpDate  --分

             get hours of tmpDate     --时

    get day of tmpDate         --天

    get month of tmpDate     --月

    get year of tmpDate        --年

    time string of tmpDate     --当前时间:4:58:09 PM

    date string of tmpDate     --Wednesday, February 19, 2014
    
   实现datediff功能(返回second)
    set StartTime to current date
    delay 2
    get (current date) - StartTime
end tell        

8.处理字符串:

  1>Split方法    

on Split(delimiter, splitString)
    tell application "System Events"
        set AppleScript's text item delimiters to delimiter
        set aa to every text item of splitString
    end tell
end Split

  2>Replace方法    

on Replace(repalceString, oldString, newString)
    tell application "System Events"
        set AppleScript's text item delimiters to oldString
        set aa to every text item of repalceString
        set AppleScript's text item delimiters to newString
        set bb to aa as string
    end tell
end Replace

  

  3>Contain:判断字符串A是否包含字符串B  

return “abc" contain "a"  --return true

  

9.处理文件

  1>Read  

tell application "System Events"
    try
        set fp to open for access file ((path to desktop as text) & "123.txt") -- ((path to desktop as text) & "123.txt")获取文件路径 
         set myText to read fp
        display dialog myText
    end try
    close access file ((path to desktop as text) & "123.txt")   --释放
end tell

  2>Write

   try
        set fp to open for access file ((path to desktop as text) & "123.txt") with write permission
        set eof of fp to 0
        write "123" to fp starting at eof
    end try
    close access file ((path to desktop as text) & "123.txt")


  3>Append

    try
        set fp to open for access file ((path to desktop as text) & "123.txt") with write permission
        write "123" to fp starting at eof
    end try
    close access file ((path to desktop as text) & "123.txt")

  

    

 

 

原文地址:https://www.cnblogs.com/Alvin-x/p/3555982.html