## iOS的UIAutomation驅動
> **注意**: 此驅動已經過時且不應該被使用除非有必要。 此文檔中的信息可能不最新
>結合實際情況,該驅動程序將在Appium的未來版本中刪除。
> 當前開始使用Appium進行ios自動化,請使用 [XCUITest
> Driver](../drivers/ios-xcuitest.md) 替代.
Appium以前用于iOS應用程序自動化的方法是基于“UIAutomation”(UIAutomation是蘋果在iOS 10之前iOS SDK附帶的框架,后續ios版本已被移除)
UIAutomation是一個包含蘋果的儀器分析系統,并提供了同步運行的單個應用程序上下文JavaScript API。
Appium UIAutomation驅動程序建立了一套異步的,基于會話的WebDriver前端API。
有關UIAutomation 驅動到
[appium-ios-driver](https://github.com/appium/appium-ios-driver) 了解更多
### 要求和支持
除了Appium的一般要求之外的要求:
* Xcode 版本小于或等于7.
* iOS 仿真器 或 設備 版本小于等于9.3.
* 所有版本的Appium附帶本驅動
* 為了使驅動程序正常工作,請參見下面的其他設置。
### 用法
通過設置您的[新會話請求](#TODO)中的`platformName` [參數](#TODO)的值為iOS來使用UIAutomation驅動程序啟動會話。當然,您至少還必須配置合適的`platformVersion`,`deviceName`和`app`參數。
### 功能
UIAutomation驅動程序支持許多Appium的標準功能 [Appium
功能](../writing-running-appium/caps.md), 但還有一個額外的
僅適用于該驅動程序的一組功能 (查看 [iOS
部分](../writing-running-appium/caps.md#ios-only)
的前述文件).
要想自動使用Safari而不是你自己的應用程序,請將“app”功能設置為空,而將“browserName”功能設置為“Safari”。
### 命令
要查看Appium支持的各種命令,特別是關于命令如何映射到UIAutomation驅動程序的行為的信息,請參見 [API
相關](#TODO).
### 模擬器配置
(注意,由于Xcode和iOS模擬器的限制,在任何給定的時間,只能有一個模擬器是開放的,并且是自動的。對于多模擬器支持,您將需要升級到 [XCUITest 驅動](ios-xcuitest.md)).
1. 要讓儀器實現iOS模擬器的自動化,你需要修改系統的授權數據庫。Appium通過安裝和運行授權腳本提供了一種簡單的方法:
```
sudo authorize-ios
```
1. 默認情況下,基于儀器的自動化受到指令之間1秒硬編碼延遲的限制,這是由蘋果的工程師出于不明原因做的。有一種方法可以繞過這個限制
[instruments-without-delay](https://github.com/facebookarchive/instruments-without-delay)
(IWD). IWD為小于7的Xcode附帶提供Appium。Xcode為7或者以上的,在使用Appium之前,用戶必須手動安裝IWD。安裝步驟如下:
* 克隆 [appium-ios-driver](https://github.com/appium/appium-ios-driver)倉庫
* 在倉庫內,運行bin目錄中包含的xcode-iwd.sh腳本,
向它傳遞幾個參數:
(1)您使用的Xcode應用程序的路徑。
(2)appium-instruments文件夾的路徑。例如:
```
sh ./bin/xcode-iwd.sh /Applications/Xcode.app /Users/me/appium-instruments/
```
1. 為了獲得最佳結果,請啟動您要使用的每個模擬器,并確保以下各項:
* 啟用了軟鍵盤(Simulator應用程序中的Command + K)
* UIAutomation 開啟了開發者設置選項
* 在Xcode的“Devices”管理器中不存在具有相同名稱的多個模擬器
### 真實設備配置
由于代碼簽名和對蘋果限制的額外變通,在真實設備上運行測試要復雜得多。使用此驅動程序成功實現自動化策略的基本過程如下:
1. 在您將運行測試的特定類型的真實設備上,使用調試配置構建您的應用程序,確保在您的特定設備上運行的應用程序已經進行了簽名。例如:
```
xcodebuild -sdk <iphoneos> -target <target_name> -configuration Debug \
CODE_SIGN_IDENTITY="iPhone Developer: Mister Smith" \
PROVISIONING_PROFILE="XXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX"
```
1. 將構建的應用程序(通常位于Xcode中指定的構建目錄中)安裝到您自己的測試設備上,確保它存在于設備上,并且沒有簽名錯誤。在設備上安裝應用程序有很多方法。一是只使用Xcode本身。另一個是使用“ideviceinstaller”工具(“libimobiledevice”套件的一部分)。第三種是使用[ios-deploy](https://npmjs.org/package/ios-deploy)。這是一個關于`ideviceinstaller`的例子:
```
# 首次安裝ideviceinstaller, 使用 Homebrew (http://brew.sh)
brew install libimobiledevice
ideviceinstaller -u <UDID of your device> -i <path to your built app>
```
1. 使用你的應用程序的bundle ID作為`app`參數的值。
1. 使用UUID作為你的設備的`udid`參數的值。
1. 如上所述,確保在Developer設置中開啟了UI Automation。
遵循這些步驟應該能確保你的運行成功!如果你使用的是更新版本的Xcode(例如7.x),您可能希望咨詢[XCUITest
Driver Real Device Docs](../drivers/ios-xcuitest-real-devices.md),他們可能包含更多相關的信息。
### 真實設備 Hybrid / Web Testing
對混合和web的測試,Appium需要使用“遠程調試協議”去發送JavaScript,在網絡視圖中執行。對于真正的iOS設備,
此協議已加密,必須使用第三方工具來訪問。由Google提供的工具稱為
[ios-webkit-debug-proxy](https://github.com/google/ios-webkit-debug-proxy)
(IWDP)。有關在Appium中安裝和使用IWDP的信息,請參閱
[IWDP doc](../writing-running-appium/web/ios-webkit-debug-proxy.md).
對于web測試,即在Safari瀏覽器中運行的測試,我們還需要跨越另一個障礙。在真實的設備上,沒有開發者簽名的應用程序不能被UIAutomation識別,例如Safari就是這樣一個應用。因此,我們有一個名為“SafariLauncher”的助手應用,它可以由開發者簽名。它在啟動時的唯一目的是返回并啟動Safari,然后通過遠程調試器結合IWDP實現自動化。不幸的是,在這種情況下,您不能轉移到本地上下文中并對瀏覽器本身進行任何自動化操作。
更多有關 `SafariLauncher`的設置信息, 請查閱 [SafariLauncher
doc](../drivers/ios-uiautomation-safari-launcher.md).
### 文件生成和 iOS 測試執行
在iOS上進行測試所生成的文件有時可能會很大。這些包括
日志,臨時文件和Xcode運行的派生數據。一般從下面的位置找到它們并根據需要刪除:
```
$HOME/Library/Logs/CoreSimulator/*
/Library/Caches/com.apple.dt.instruments/*
```
### 通過Jenkins運行 iOS 測試
首先下載`jenkins-cli.jar`并驗證Mac是否成功
連接到Jenkins主節點。確保已運行上文提到的?authorize-ios`命令。
```
wget https://jenkins.ci.cloudbees.com/jnlpJars/jenkins-cli.jar
java -jar jenkins-cli.jar \
-s https://team-appium.ci.cloudbees.com \
-i ~/.ssh/id_rsa \
on-premise-executor \
-fsroot ~/jenkins \
-labels osx \
-name mac_appium
```
接下來為Jenkins定義一個LaunchAgent,在登錄時自動啟動。
LaunchDaemon無法使用,因為守護程序沒有GUI訪問權限。確保
plist不包含“ SessionCreate”或“ User”鍵,因為這可能會阻止
測試運行。當配置錯誤的時候,您會看到“無法授權權限”錯誤。
```
$ sudo nano /Library/LaunchAgents/com.jenkins.ci.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.jenkins.ci</string>
<key>ProgramArguments</key>
<array>
<string>java</string>
<string>-Djava.awt.headless=true</string>
<string>-jar</string>
<string>/Users/appium/jenkins/jenkins-cli.jar</string>
<string>-s</string>
<string>https://instructure.ci.cloudbees.com</string>
<string>on-premise-executor</string>
<string>-fsroot</string>
<string>/Users/appium/jenkins</string>
<string>-executors</string>
<string>1</string>
<string>-labels</string>
<string>mac</string>
<string>-name</string>
<string>mac_appium</string>
<string>-persistent</string>
</array>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/Users/appium/jenkins/stdout.log</string>
<key>StandardErrorPath</key>
<string>/Users/appium/jenkins/error.log</string>
</dict>
</plist>
```
最后設置所有者,權限,然后啟動代理。
```
sudo chown root:wheel /Library/LaunchAgents/com.jenkins.ci.plist
sudo chmod 644 /Library/LaunchAgents/com.jenkins.ci.plist
launchctl load /Library/LaunchAgents/com.jenkins.ci.plist
launchctl start com.jenkins.ci
```
- 關于TesterHome和MTSC
- 關于Appium
- 簡介
- Appium 客戶端
- 入門指南
- 已支持的平臺
- API 文檔
- Appium驅動
- XCUITest (iOS)
- XCUITest Real Devices (iOS)
- UIAutomation (iOS)
- UIAutomation Safari Launcher (iOS)
- UIAutomator (Android)
- UIAutomator2 (Android)
- Espresso (Android)
- Windows
- Mac
- Appium命令
- Status
- Execute Mobile Command
- Session
- Create
- End
- Get Session Capabilities
- Go Back
- Screenshot
- Source
- Timeouts
- Timeouts
- Implicit Wait
- Async Script
- Orientation
- Get Orientation
- Set Orientation
- Geolocation
- Get Geolocation
- Set Geolocation
- Logs
- Get Log Types
- Get Logs
- Events
- Log event
- Get events
- Settings
- Update Settings
- Get Device Settings
- Settings
- Update Settings
- Get Device Settings
- Execute Driver Script
- Device
- Activity
- Start Activity
- Current Activity
- Current Package
- App
- Install App
- Is App Installed
- Launch App
- Background App
- Close App
- Reset App
- Remove App
- Activate App
- Terminate App
- Get App State
- Get App Strings
- End Test Coverage
- Clipboard
- Get Clipboard
- Set Clipboard
- Emulator
- Power AC
- Power Capacity
- Files
- Push File
- Pull File
- Pull Folder
- Interactions
- Shake
- Lock
- Unlock
- Is Locked
- Rotate
- Keys
- Press keycode
- Long press keycode
- Hide Keyboard
- Is Keyboard Shown
- Network
- Toggle Airplane Mode
- Toggle Data
- Toggle WiFi
- Toggle Location Services
- Send SMS
- GSM Call
- GSM Signal
- GSM Voice
- Network Speed
- Performance Data
- Get Performance Data
- Performance Data Types
- Screen Recording
- Start Screen Recording
- Stop Screen Recording
- Simulator
- Perform Touch ID
- Toggle Touch ID Enrollment
- System
- Open Notifications
- System Bars
- System Time
- Display density
- Authentication
- Finger Print
- Element
- Find Element
- Find Elements
- Actions
- Click
- Send Keys
- Clear
- Attributes
- Text
- Name
- Attribute
- Selected
- Enabled
- Displayed
- Location
- Size
- Rect
- CSS Property
- Location in View
- Other
- Submit
- Active Element
- Equals Element
- Context
- Get Context
- Get All Contexts
- Set Context
- Interactions
- Mouse
- Move To
- Click
- Double Click
- Button Down
- Button Up
- Touch
- Single Tap
- Double Tap
- Move
- Touch Down
- Touch Up
- Long Press
- Scroll
- Flick
- Multi Touch Perform
- Touch Perform
- W3C Actions
- Web
- Window
- Set Window
- Close Window
- Get Handle
- Get Handles
- Get Title
- Get Window Size
- Set Window Size
- Get Window Position
- Set Window Position
- Maximize Window
- Navigation
- Go to URL
- Get URL
- Back
- Forward
- Refresh
- Storage
- Get All Cookies
- Set Cookie
- Delete Cookie
- Delete All Cookies
- Frame
- Switch to Frame
- Switch to Parent Frame
- Execute Async
- Execute
- 編寫 & 運行Appium腳本
- Running Tests
- Desired Capabilities
- The --default-capabilities flag
- Finding Elements
- Touch Actions
- CLI Arguments
- Server Security
- Web/Web Views
- Mobile Web Testing
- Automating Hybrid Apps
- Using ios-webkit-debug-proxy
- Using Chromedriver
- Image Comparison
- iOS
- Low-Level Insights on iOS Input Events
- XCUITest Mobile Gestures
- XCUITest Mobile App Management
- iOS Pasteboard Guide
- iOS Predicate Guide
- iOS Touch ID Guide
- iOS Install Certificate
- tvOS support
- Pushing/Pulling files
- Audio Capture
- Android
- Low-Level Insights on Android Input Events
- UiSelector Guide
- Espresso Datamatcher Guide
- Android Code Coverage Guide
- Activities Startup Troubleshooting Guide
- How To Execute Shell Commands On The Remote Device
- Android Device Screen Streaming
- How To Emulate IME Actions Generation
- How To Test Android App Bundle
- Other
- Reset Strategies
- Network Connection Guide
- Using Unicode with Appium
- Troubleshooting
- Tutorial
- Swipe Tutorial
- Screen
- Element
- Partial screen
- Simple
- Multiple scroll views
- Add scroll layout
- Tricks and Tips
- Screen
- Element
- Element search
- Fast
- Slow
- Guide
- 進階概念
- 定位圖像中的元素
- 使用定位元素的插件
- 遷移到 XCUITest
- 在 Appium 中使用 Selenium Grid
- Appium Logs Filtering
- 跨域 iframes
- 使用自定義 WDA 服務器
- 使用不同版本的 Xcode 運行
- The Event Timings API
- 并行測試的設置
- The Settings API
- Memory Collection
- 向Appium項目做貢獻
- 從源代碼運行 Appium
- 開發者概述
- 標準開發命令
- Appium 風格指南
- 如何編寫文檔
- Appium 包結構
- 鳴謝