## Pushing/Pulling 文件
Appium 提供了[Pull 目錄](http://appium.io../commands/device/files/pull-folder/), [Pull 文件](http://appium.io../commands/device/files/pull-file/) 和 [Push 文件](http://appium.io../commands/device/files/push-file/) 三個命令來移動文件。
本文檔旨在幫助理解它們是如何工作的。
### 命令使用格式
#### 名詞解釋:
bundle_id:bundle ID可以翻譯成包ID,也可以叫APP ID 或應用ID,它是每一個ios應用的全球唯一標識。無論代碼怎么改,圖標和應用名稱怎么換,只要bundle id沒變,ios系統就認為這是同一個應用。每開發一個新應用,首先都需要到member center->identifier->APP IDS去創建一個bundle id。
container:每一個App都被放在沙盒(sandbox)中,在沙盒中,有一個Containers,Containers又被分為Bundle Container和Data Container
基本格式如下:
1. `@<app_bundle_id>:<optional_container_type>/<文件或者目錄在container里的路徑>`
2. `@<app_bundle_id>/<文件或者目錄在container里的路徑>`
3. `<文件或者目錄在container里的路徑>`
### 真機
#### 命令使用格式
以下是該方法參數的格式:
針對上面說的基本格式
- `@<app_bundle_id>` 是應用的 bundle identifier
- `optional_container_type` 是 container 類型
- `documents` 是唯一選項
- 你可以通過`ifuse -u <udid> --list-apps`得到 bundle id列表,然后給列表中的bundle id指定 container 類型為`documents`。
- 比如下面的 _On My iPhone_ 圖中有 _Slack_ 目錄,但是在 `--list-apps` 中并不存在 `com.tinyspeck.chatlyio` 。所以我們就無法加載`com.tinyspeck.chatlyio@documents/`
<img src='../writing-running-appium/ios/ios-xctest-file-movement/on_my_iphone.png' width=100>
- 你也可以不指定container類型,比如 _格式 2_
- 只有應用的`info.plist`里有 `UIFileSharingEnabled` 標簽才可以加載。
- `文件或者目錄在container里的路徑` 指的是pull/push文件或者目錄的目標。
- 如果 `optional_container_type` 是 `documents`,那么這個路徑可以映射為`Files`應用中的`On My iPhone/<app name>`
真機中不允許使用 _格式 3_
#### 示例
如果你想從 keynote 應用中把 _Presentation.key_ 拉到本地,你可以這樣做:
- Pull file
```javascript
// webdriver.io
let data = driver.pullFile('@io.appium.example:documents/Presentation.key');
await fs.writeFile('presentation.key', Buffer.from(data, 'base64'), 'binary');
```
```ruby
# ruby_lib_core
file = @driver.pull_file '@com.apple.Keynote:documents/Presentation.key'
File.open('presentation.key', 'wb') { |f| f<< file }
```
該文件在 _Files_ 應用中的 _On My iPhone/Keynote_.
|Top | On My iPhone | Keynote |
|:----:|:----:|:----:|
||||
如果文件目錄比較深,比如 _On My iPhone/Keynote/Dir1/Dir2_,那么代碼命令如下:
```javascript
// webdriver.io
let data = driver.pullFile('@io.appium.example:documents/Dir1/Dir2/Presentation.key');
await fs.writeFile('presentation.key', Buffer.from(data, 'base64'), 'binary');
```
```ruby
# ruby_lib_core
file = @driver.pull_file '@com.apple.Keynote:documents/Dir1/Dir2/Presentation.key'
File.open('presentation.key', 'wb') { |f| f<< file }
```
- Pull 目錄
如果你要把 _On My iPhone/Keynote_ 拉到本地,你可以這樣做: `@driver.pull_folder '@com.apple.Keynote:documents/'`。
```javascript
// webdriver.io
let data = driver.pullFolder('@io.appium.example:documents/');
await fs.writeFile('documents.zip', Buffer.from(data, 'base64'), 'binary');
```
```ruby
# ruby_lib_core
file = @driver.pull_folder '@com.apple.Keynote:documents/'
File.open('documents.zip', 'wb') { |f| f<< file }
```
- Push 文件
和 pull 文件一樣
```javascript
// webdriver.io
driver.pushFile('@com.apple.Keynote:documents/text.txt', new Buffer("Hello World").toString('base64'));
```
```ruby
# ruby_lib_core
@driver.push_file '@com.apple.Keynote:documents/text.txt', (File.read 'path/to/file')
```
### 模擬器
#### 格式
參數格式如下:
針對上面說的基本格式
- `@<app_bundle_id>`
- `optional_container_type` 可選的容器類型
- `app`, `data`, `groups` 或者 `<A specific App Group container>`
- 如果不指定的話,默認使用 `app` container
- `文件或者目錄在container里的路徑`
_格式 3_ 只能處理 `app` container
#### 示例
```java
// Java
// Get AddressBook.sqlitedb in test app package ('app' container)
byte[] fileContent = driver.pullFile("Library/AddressBook/AddressBook.sqlitedb");
Path dstPath = Paths.get(new File("/local/path/AddressBook.sqlitedb"));
Files.write(dstPath, fileContent);
```
### 參考
- https://stackoverflow.com/questions/1108076/where-does-the-iphone-simulator-store-its-data
- https://stackoverflow.com/questions/48884248/how-can-i-add-files-to-the-ios-simulator
- https://apple.stackexchange.com/questions/299413/how-to-allow-the-files-app-to-save-to-on-my-iphone-or-to-on-my-ipad-in-ios/299565#299565
- 關于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 包結構
- 鳴謝