Mac上搭建ios自動化測試環境

  • http://ios-driver.github.io/ios-driver/ # 官網關于ios-driver詳細使用說明

  • 工具

    • VMware 12 Pro + vm tools.iso + Unlocker + OS X10.11 + Apple ID
    • Xcode7.3 + JDK1.8 + Maven3.5.3 + Gauge0.9.7 +ios-driver 0.6.5 + IDEA2017.3.4
  • System requirements for OSX

    • ios-driver is built upon 2 different frameworks from Apple.
      • For native apps, it uses the UIAutomation framework from Apple (more here). You need xcode with an IOS SDK > 5.0.
        • To check if you have the correct setup:
          • $ xcodebuild -showsdks
    • The server is written in Java and requires Java 7.To check that you have the correct version :
      • $ java -version
    • Mac 中 JDK默認安裝路徑
      • JETdeMac:apache-maven-3.5.3 JET$ /usr/libexec/java_home -V
      • Matching Java Virtual Machines (1):
      • 1.8.0_162, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
      • /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home
        • 設置環境變量 JAVA_HOME
  • 1、先得要有Mac系統( Mac筆記本,或者VMware安裝OS X 虛擬機)

  • 2、創建ios simulator

  • OS X EI Capitan 10.11.1快速升級方法介紹

  • Mac上安裝Xcode

    • 下載安裝包形式安裝
      • https://developer.apple.com/download/more/下載apple開發工具,包括xcode & Command Line Tools (macOS El Capitan version 10.11) for Xcode8.2等(需要輸入apple id)
        • Mac10.11, 對應安裝Xcode 8.2 (5.2G) 版本
        • Mac系統對應XCode版本,都可以在這個鏈接里查看得到
        • 建議使用safari瀏覽器下載,速度很快(我的達到了2M/s)
          • Mac下載的文件在哪?
            • 從 App Store 中下載的軟件:
              • 如果你是從 App Store 應用商店中下載的軟件,可以在 Launchpad 面板中看到。我們可以通過按鍵盤上的 F4 鍵,或在觸控板上用四個手指合攏打開 Launchpad 面板。
              • 另外我們還可以在 Finder 中的“應用程序”中找到它們,這里會顯示當前電腦上安裝的所有應用軟件
              • 還可以在終端命令窗口中輸入 ls /Applications 來查看所有的應用軟件。
            • 查看在 Safari 瀏覽器下載的:
              • 如果你是通過 Safari 瀏覽器下載的內容,可以點擊 Safari 窗口右上角的“下載”圖標.隨后可以看到最近的下載列表,如果想要打開下載文件的位置,請點擊文件右側的“查找”圖標.接著就會在 Finder 窗口中顯示該文件的所在位置
            • 直接在 Dock 欄中的下載欄目中查看:
              • 在屏幕下方 Dock 欄的右側有一個“下載”欄目,可以看到最近下載的文件
      • Xcode: This is the complete Xcode developer toolset for Apple Watch,Apple TV,iPhone,iPad,and Mac. It includes the Xcode IDE,ios Simulator,and all required toolsand frameworks for building iOS,watchOS,tvOS and macOS apps.
      • 安裝xcode_7.3.dmg
        • 雙擊xcode_7.3.dmg,等完成后,按照提示,拖動到Applications,耐心等待(正在將"xcode.app"拷貝到"應用程序" (10.48G)) ---- 此過程,時間很漫長...
      • 查看xcode的版本
        • 終端中輸入 xcodebuild -version
          • JETdeMac:~ JET$ xcodebuild -version
          • Xcode 7.3
          • Build version 7D175
            • Win7 64位上安裝的OS X虛擬機, Win7 32 bit mstsc連接 Win7 64bit, OS X Terminal上復制,直接復制到win7 32上是無法復制的;
              • 先復制到win7 64上,再復制到win7 32bit 上,是可以的.
          • 有時候直接輸入這個命令xcodebuild -version,報錯:error: tool 'xcodebuild' requires Xcode // select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
        • 點擊“Xcode”的menu菜單。點擊里面的“About xcode”選項,此時會打開對話框。
    • 安裝Xcode8.2
      • 下載下來以后,發現文件名是 Xcode_8.xip,以往的擴展名都是 .zip,今年的有一點不一樣。但是歸檔工具還是能識別這個文件,雙擊以后,本來挺激動。但是,發現過了一會報了一個錯誤 “未能完成該操作。cpio read error:Undefined error:0”。
      • 拿這個錯誤去簡書內搜了一下,沒有有價值的解決方案,后來又去了谷歌,在 stack overflow 上找到了答案,你只要把Mac系統升級到最新的 10.11.6,再重啟系統,雙擊打開就沒有問題了。
    • 命令行方式安裝指定版本的Xcode & simulator
    • 干凈地卸載Xcode
      • 根目錄下的必須要用管理員權限:
        • sudo rm -rf /Applications/Xcode.app
        • sudo rm -rf /Library/Preferences/com.apple.dt.Xcode.plist
      • 然后刪除自己目錄下的:
        • rm -rf ~/Library/Preferences/com.apple.dt.Xcode.plist
        • rm -rf ~/Library/Caches/com.apple.dt.Xcode
        • rm -rf ~/Library/Application\ Support/Xcode
      • 以及和開發者相關的:
        • rm -rf ~/Library/Developer/Xcode
        • rm -rf ~/Library/Developer/CoreSimulator
        • rm -rf ~/Library/Developer/XCPGDevices
    • 安裝/卸載command line tools for xcode
      • 安裝:
        • brew doctor
      • 卸載:
        • cd /Library/Developer
        • JETdeMac:Developer JET$ sudo rm -rf CommandLineTools/
  • mac安裝神器brew

    • 命令行輸入: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 回車(這里有提示目錄不存在,要創建需要按return鍵,如果用的windows鍵盤,就是回車鍵)
      • 此過程,自動安裝: Installing Command Line Tools (macOS El Capitan version 10.11) for Xcode
    • 它的用法
      • brew update 更新brew;
      • brew install {應用名,如git} 安裝軟件
      • brew cask install {應用名,如git} 也是下載安裝,與上面的區別,請查看https://www.zhihu.com/question/22624898
      • 更多用法請 brew help
  • Install Gauge for Mac OS X

    • 1. use brew
      • brew update
      • brew install gauge
        • gauge -v 驗證是否安裝OK
          • JETdeMac:Downloads JET$ gauge -v
          • Gauge version: 0.9.7
          • Plugins

          • No plugins found
          • Plugins can be installed with gauge install {plugin-name}
        • gauge init java
        • JETdeMac:MyGaugeTest JET$ gauge run specs/
          • Installing required plugins.
          • .....................
          • ........................
          • Specification Heading

          • Vowel counts in single word ? ?

          • Vowel counts in multiple word ? ?

          • Successfully generated html-report to => /Users/JET/Downloads/MyGaugeTest/reports/html-report/index.html
          • Specifications: 1 executed 1 passed 0 failed 0 skipped
          • Scenarios: 2 executed 2 passed 0 failed 0 skipped
    • 2. use zip
      • $ unzip -o gauge-0.9.7-darwin.x86_64.zip -d /usr/local/bin
  • Install JDK for OS X

    • oracle官網下載mac版本的JDK: jdk-8u162-macosx-x64.dmg
    • 雙擊安裝,出現pkg文件,直接雙擊安裝即可
    • java -version 查看是否安裝成功
      • 默認已經設置了環境變量
        • JETdeMac:~ JET$ java -version
        • java version "1.8.0_162"
        • Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
        • Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
  • Install Maven for OS X (IDEA201703.04版本自帶Maven3.3.9,在Mac中配置好很久自己安裝的3.5.2/3,都無法加載pom.xml中的依賴。還是使用IDEA自帶的Maven!!!)

    • htps://blog.csdn.net/u011886447/article/details/70200922
    • 從官網(http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.2/binaries/)下載 Maven (apache-maven-3.5.2-bin.tar.gz)并解壓。
      • tar -zxvf apache-maven-3.5.2-bin.tar.gz -C /data/maven/
      • mkdir -p /data/maven/LocalHouse
    • 設置環境變量
      • JETdeMac:Home JET$ sudo vi ~/.bash_profile # 設置maven & java 環境變量
        • export MAVEN_HOME="/data/maven/apache-maven-3.5.2"
        • export PATH="MAVEN_HOME/bin:PATH"
        • export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home"
        • export PATH="JAVA_HOME/bin:PATH"
      • JETdeMac:Home JET$ source ~/.bash_profile # 使環境變量生效
      • JETdeMac:Home JET$ mvn -v # 檢查環境變量是否生效
        • Apache Maven 3.5.2 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)
        • Maven home: /data/maven/apache-maven-3.5.2
        • Java version: 1.8.0_162, vendor: Oracle Corporation
        • Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre
        • Default locale: zh_CN, platform encoding: UTF-8
        • OS name: "mac os x", version: "10.11.1", arch: "x86_64", family: "mac"
  • Mac下安裝/注冊/配置IDEA

    • 安裝IDEA
      • 先到IDEA官網下載Mac版本的IDEA(ideaIU-2017.3.4.dmg)
      • 直接雙擊ideaIU-2017.3.4.dmg,拖動到Applications,安裝完成后,在Launch里找到IDEA,運行
    • 注冊IDEA
      • 先Finder里快捷鍵組合 Shift+Command+G 三個組合按鍵,并輸入 Hosts 文件的所在路徑:/etc/hosts,將“0.0.0.0 account.jetbrains.com”添加到hosts文件中
        • 注意:會提示"無權限更改“,所以只能通過下面的命令行sudo更改
      • sudo nano /private/etc/hosts
        • 將“0.0.0.0 account.jetbrains.com”添加到hosts文件中
        • mac nano 保存退出
          • Ctrl + x,保存,會提示按Y,不保存就 N,取消就 C
            • Enter
      • 登錄http://idea.lanyus.com/ ,復制Activation code,粘貼到IDEA中
        • Mac中,Command 相當于 Windows中的Ctrl
          • Mac中,復制:command+c, 粘貼: command+v
          • IDEA中,復制一行: command+d
    • 配置IDEA
      • 配置Maven(Mac中,使用IDEA自帶的Maven才正常加載pom.xml依賴!!)
      • 配置Gauge插件
        • Configure-Preferences-Plugins:
          • 搜索“gauge",下載(0.3.11), install
            • 但是不知道是網絡不好,還是啥原因,一直下載不成功
          • 后來直接到IDEA Plugin網站下載gauge(http://plugins.jetbrains.com/plugin/7535-gauge) Gauge-Java-Intellij-0.3.11.zip
            • IDEA:Configure-Preferences-Plugins:
              • Install plugins from disk, 選擇Gauge-Java-Intellij-0.3.11.zip,點擊restart IDEA
      • JDK配置
        • idea Structure Project:
          • 默認設置為了我自己安裝的1.8版本的(可以再檢查下)
  • 官網推薦ios-driver

    • For testing any iOS native, hybrid, or mobile web application using WebDriver - we recommend: ios-driver.
  • ios-driver官網推薦使用simulator

    • Using simulators is the default way to use ios-driver.It offers more flexibility, and it is faster than tests on a real device. However, if your specific needs require tests on real device, in the next release, ios-driver will also support devices without having to make any modifications! ( http://ios-driver.github.io/ios-driver/ )
    • Note: Release 0.6.5 does not support xCode 5. If you are using xCode 5, please use the latest snapshot 0.6.6-SNAPSHOT.
  • Mac系統如何啟動iOS模擬器 快速啟動iOS模擬器教程

    • 創建模擬器包:
      • 在終端上運行下面的命令來查找你的機器上可用的SDK的版本:
        • xcodebuild -showsdks
      • 繼續在終端運行如下命令,用你的模擬器SDK替換“-sdk”后面的字段
        • xcodebuild -arch i386 -sdk iphonesimulator9.3
          • 注意:如果你的項目是多“Target”的項目,那么你還需要指定一個Target,從而生成那個Target的模擬器包,命令如下:
            • xcodebuild -arch i386 -sdk [iphonesimulator] -scheme [NameOfScheme]
              • 運行成功后,在終端將輸出一系列信息,并以** BUILD SUCCEEDED **結尾。
              • 生成的模擬器包存儲在這個路徑:
                • {Derive data directory}/build/Release-iphonesimulator/{projectname}.app
                • 這一路徑的具體信息在最后終端的輸出信息中也可以找到。
      • 通過命令行啟動iOS模擬器:
        • brew install ios-sim
        • ios-sim launch <path to .app>
    • 用ios5.0模擬器(iphonesimulator5.0)建立工程
      • $ xcodebuild -sdk iphonesimulator5.0
        • 默認是Release工程,要設置成Debug模式如下:
          • $ xcodebuild -sdk iphonesimulator5.0 -configuration Debug
        • clean創建的工程
          • $ xcodebuild clean -sdk iphonesimulator5.0 -configuration Debug
  • https://www.cnblogs.com/feifeidxl/p/4581369.html[Selenium] IOS 之 ios-driver

    • xcodebuild -showsdks

      image
    • xcodebuild -arch i386 -sdk iphonesimulator9.3

    • 在第一次運行 ios-driver 之前,應確保一下目錄和文件的權限更新:

      image
      image
    • 先ios-driver網上下載ios-server-0.6.5-jar-with-dependencies.jar

      image
      image
    • 瀏覽器查看wd/hub/status,默認端口號為5555,可在瀏覽器中訪問如下地址,如果看到類似JSON 對象的信息,則說明前述操作成功

      image
  • 例子:

    • 例子1:自動化操作iOS瀏覽器,打開百度網頁等操作
      • IDEA中添加 ios-server-0.6.5-jar-with-dependencies.jar 文件,因為需要庫的支持org.uiautomation.ios.IOSCapabilities
      • maven:
        • <dependency>
        • <groupId>org.uiautomation</groupId>
        • <artifactId>ios-server</artifactId>
        • <version>0.6.5</version>
        • </dependency>
      • 代碼實例:
        • 示例代碼:
        • package com.learningselenium.ios;
        • import junit.framework.TestCase;
        • import java.net.URL;
        • import org.openqa.selenium.remote.RemoteWebDriver;
        • import org.openqa.selenium.remote.DesiredCapabilities;
        • import org.uiautomation.ios.IOSCapabilities;
        • public class testiOSBaidu extends TestCase{
        • public void testBaidu() throws Exception{
        • //設置DesiredCapabilities,其中IOSCapabilities設置為iphone,如果程序運行在iPad模擬器上,可設置為ipad。
        • //參數Safari 表示待測試程序的bundle name,因為這里是測試 Web 頁面,默認是用 Safari 打開
        • DesiredCapabilities safari = IOSCapabilities.iphone("Safari");
        • RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), safari );
        • driver.get("http://www.baidu.com");
        • driver.close();
        • }
        • }
    • 2.ios-driver 的 Native app 實例
      • 接下來以蘋果官網的示例程序 InternationalMountains 為例,對原生 app 的測試過程進行講解,示例代碼下載地址:
      • https://developer.apple.com/legacy/library/samplecode/internationalMountains/introduction/intro.html
      • 請確保app 程序和 UIAutomation已關聯,方法如下:
      • 在 Xcode 的菜單中選擇 Product->Profile,并選擇 Automation。該設定會構建 app 并啟動 Instruments
      • 將 Instruments 關閉,并在 Xcode 中選擇 Window->Organizer->Projects,可看到 InternationalMountains.app 文件所在位置為:
      • ~/Library/Developer/Xcode/DerivedData/InternationalMountainseordguimrxknwoaynobkvpirkacs/Build/Products/Debugiphonesimulator/InternationalMountains.app
      • 接著進入剛下載的 ios-server-0.6.5-jar-with-dependencies.jar 的路徑,執行如下命令:
      • $ java -jar ios-server-0.6.5-jar-with-dependencies.jar -aut
      • ~/Library/Developer/Xcode/DerivedData/InternationalMountainseordguimrxknwoaynobkvpirkacs/Build/Products/Debugiphonesimulator/InternationalMountains.app -port 4444
      • 接下來確認 ios-driver 啟動成功并可訪問該 app,可通過瀏覽器訪問如下地址,如果看到類似JSON 對象的信息,說明前述操作成功:
      • http://localhost:4444/wd/htb/status
      • 如果是在模擬器中運行app,則需要在啟動 ios-driver 時使用 -simulators 參數,執行命令及打印信息:
      • $ java -jar ios-server-0.6.5-jar-with-dependencies.jar -aut
      • ~/Library/Developer/Xcode/DerivedData/InternationalMountainseordguimrxknwoaynobkvpirkacs/Build/Products/Debugiphonesimulator/InternationalMountains.app -port 4444 -simulators
      • ... test can access the server at http://0.0.0.0:4444/wd/hub ...
      • 應確保已經通過了 Xcode 啟動了模擬器,否則需要通過瀏覽器查看 ios-driver 的啟動狀態是否正常
      • 針對 InternationalMountains 的測試用例代碼如下:
      • package com.learningselenium.ios;
      • import java.net.URL;
      • import jave.util.List;
      • import java.io.File;
      • import junit.framework.TestCase;
      • import org.openqa.selenium.By;
      • import org.openqa.selenium.WebElement;
      • import org.openqa.selenium.TakesScreenshot;
      • import org.openqa.selenium.OutputType;
      • import org.openqa.selenium.remote.RemoteWebDriver;
      • import org.openqa.selenium.remote.DesiredCapabilities;
      • import org.openqa.selenium.remote.Augmenter;
      • import org.uiautomation.ios.IOSCapabilities;
      • public class testiOSInternationalMountains extends TestCase{
      • public void testInternationalMountains() throws Exception{
      • DesiredCapabilities nativeAppCap= IOSCapabilities.iphone("InternationalMountains", "1.1");
      • RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), nativeAppCap);
      • List<WebElement> cells = driver.findElement(By.className("UIATableCell"));
      • assertEquals(9, cells.size());
      • WebElement first = cells.get(0);  //操作列表中第一個元素
      • first.click();
      • //截屏操作和操作頁面元素
      • TaksScreenshot screen = (TaksScreenshot ) new Augmenter().augment(driver);
      • File ss = new File("screenshot.png");
      • screen.getScreenshotAs(OutputType.FILE).renameTo(ss);
      • System.out.println("screenshot take:" + ss.getAbsolutePath());
      • By selector = By.xpath("http://UIAStaticText[contains(@name, ‘climbed’)]");
      • WebElement text = driver.findElement(selector);
      • System.out.println(text.getAttribute("name"));
      • driver.quit();
      • }
      • }
      • 如果需要在真實設備上運行app,則需要在啟動 ios-driver 時使用 -beta 參數,執行命令:
      • $ java -jar ios-server-0.6.5-jar-with-dependencies.jar -beta -port 4444
    • 3. ios-driver 的源碼編譯
      • https://github.com/ios-driver/ios-driver 源碼(git/clone ios-driver-dev.zip)
        • 在解壓后的源碼根目錄執行如下命令:
        • sudo mvn clean package
        • sudo mvn clean install
        • 如果編譯成成,會看到如下日志:
        • ...
        • [INFO]BUILD SUCCESS
        • ...
        • build without the tests : mvn package -DskipTests
    • 4. Web app 或混合式 app
      • 針對這種方式的 app,需要用到遠程 Webkit 的調試協議,并且 iOS 的版本要求為6+,Safari 的版本要求為6+。如果無法滿足以上條件,也可繼續測試原生 app,但無法在 Safari 上運行 Web 頁面,也不能使用 DOM 選擇器來與 UIWebviews 交互。
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 用到的組件 1、通過CocoaPods安裝 2、第三方類庫安裝 3、第三方服務 友盟社會化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 14,708評論 1 180
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,981評論 19 139
  • Qt是屬于一個跨平臺的GUI開發軟件,支持的平臺有Unix、Linux、Windows/WinCE、IOS等。 Q...
    一葉之界閱讀 8,233評論 0 17
  • 這期的歌手突圍賽,除了上期票選的3個直接入圍決賽的首發歌手張韶涵、汪峰和Jessie J,其余的10位歌手進行10...
    七日櫻閱讀 312評論 0 0
  • 朝九晚五的下班、買菜、做飯……這是上班族的不變節奏。 民以食為天,吃是老百姓生活中第一樁大事。最近幾年去菜場轉轉,...
    昕城閱讀 321評論 2 3