## iOS XCUITest 驅動
Appium 通過 `XCUITest` 驅動支持基本的 iOS 應用自動化。_(Appium 新手? 讀一讀 [介紹 Appium 驅動](#TODO))_。它在底層使用 [XCUITest](https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/09-ui_testing.html) 庫以更好的自動化你的 App。 對 XCUITest 的使用是通過 [WebDriverAgent](https://github.com/facebook/webdriveragent) 服務間接完成的。
WebDriverAgent (也被叫做"WDA") 是 Facebook 管理的項目, Appium 核心團隊對項目做了很大的貢獻。 WDA是一個運行在 iOS 模擬器或設備上暴露 XCUITest API 的服務,兼容 WebDriver。Appium 的 XCUITest 驅動把 WDA 作為對 Appium 用戶不透明的子進程控制, 代理發送給和接受自 WDA 的命令, 并且提供大量額外功能 (比如模擬器控制及其他方法)。
XCUITest 驅動的開發在 [appium-xcuitest-driver](https://github.com/appium/appium-xcuitest-driver) 倉庫進行。
### 依賴和支持
除了 Appium 通用依賴以外的要求:
* 蘋果的 XCUITest 庫只能運行在 iOS 9.3 及以上的版本的模擬器或設備上。
* 安裝 macOS 10.11 或 10.12 的 Mac 電腦。
* Xcode 7 及以上的版本。
* Appium 在 1.6 及以上的版本才提供 XCUITest 驅動。
* 需要額外的系統庫驅動才能正常工作(查看下面的安裝分節)。
### 從 UIAutomation 驅動遷移
如果需要從 Appium 的舊 [UIAutomation-based 驅動](../drivers/ios-uiautomation.md) 遷移到 XCUITest 驅動,可以查閱[遷移指南](../advanced-concepts/migrating-to-xcuitest.md)。
### 用法
使用 XCUITest 驅動建立會話需要在[新會話請求](#TODO)里包含值為 `XCUITest` 的 `automationName` [capability](#TODO)。當然最少也要包含恰當的`platformName`,`platformVersion`,`deviceName`,和 `app` capabilities。
iPhone 或 iPad 的 `platformName` 應該是 `iOS`。tvOS 設備的 `platformName` 應該是 `tvOS`。
- iOS
```json
{
"automationName": "XCUITest",
"platformName": "iOS",
"platformVersion": "12.2",
"deviceName": "iPhone 8",
...
}
```
- tvOS
```json
{
"automationName": "XCUITest",
"platformName": "tvOS",
"platformVersion": "12.2",
"deviceName": "Apple TV",
...
}
```
### Capabilities
XCUITest 驅動除了支持許多標準 [Appium capabilities](../writing-running-appium/caps.md),還有一組額外的 capabilities 調整驅動的行為。可以在 [appium-xcuitest-driver README](https://github.com/appium/appium-xcuitest-driver#desired-capabilities) 查看。
如果要自動化 Safari 而不是自有的應用,`app` capability 留空,設置 `browserName` capability 為 `Safari`。
### 命令
查看 Appium 支持的各種命令,特別是關于命令如何映射到 XCUITest 驅動行為的信息,請參閱 [API 參考](#TODO)。
### 基本安裝
_(我們建議使用 [Homebrew](https://brew.sh) 安裝系統依賴)_
1. 確保已經安裝配置了 Appium 通用依賴(比如 Node & NPM)。
2. 安裝 [Carthage](https://github.com/Carthage/Carthage):
```bash
brew install carthage
```
如果你不需要在真機上自動運行就安好了!在模擬器上自動化應用,`app` capability 應該設為指向你的為模擬器構建的 `.app` 或 `.app.zip` 文件的絕對路徑或鏈接。
### 真機安裝
因為蘋果對真機上運行的應用的嚴格限制,在真機上運行 XCUITest 自動化要復雜得多。請看 [XCUITest 真機安裝文檔](ios-xcuitest-real-devices.md)中的介紹。
安裝完成后,通過使用以下 desired capabilitie 和真機建立會話:
* `app` 或 `bundleId` - 指定應用 (已簽名 `.ipa` 文件的本地路徑或鏈接),如果應用已經安裝,Appium 只要有應用的 bundle identifier 就可以啟動它。
* `udid` - 指定運行測試的真機 id。如果只有一臺設備也可以設為 `auto`,Appium 會確定設備和 id。
### 可選安裝
* 安裝 idb 可以更好的處理各種模擬器操作,比如生物識別,設置定位和窗口焦點。
* 根據 https://github.com/appium/appium-idb#installation 安裝必要的庫 (Appium 1.14.0 以后)。
* 安裝 [AppleSimulatorUtils](https://github.com/wix/AppleSimulatorUtils) 后可以使用 [permissions capability](https://github.com/appium/appium-xcuitest-driver#desired-capabilities)。
### 運行測試產生的文件
在 iOS 上測試產生的文件有時會占用很多空間,包括日志、臨時文件和 Xcode 運行的派生數據。可以在以下路徑刪除它們:
```
$HOME/Library/Logs/CoreSimulator/*
$HOME/Library/Developer/Xcode/DerivedData/*
```
### 配置鍵盤
在 Appium 1.14.0 之后,為了讓測試運行的更穩定 Appium 會重置鍵盤的設置為默認項。你可以通過設置 API 修改:
- 在 _鍵盤_ 關閉`自動改正`
- 在 _鍵盤_ 關閉`輸入預測`
- 標記鍵盤教程為完全版
- (僅模擬器) 打開軟鍵盤
- 關于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 包結構
- 鳴謝