# Actions
执行一个或多个键盘和触控(触摸、鼠标、手写笔)操作
## 使用样例
```java
// Java
WebElement source = (MobileElement) driver.findElementsByAccessibilityId("SomeAccessibilityID");
WebElement target = (MobileElement) driver.findElementsByAccessibilityId("SomeOtherAccessibilityID");
Point source = dragMe.getCenter();
Point target = driver.findElementByAccessibilityId("dropzone").getCenter();
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence dragNDrop = new Sequence(finger, 1);
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(0),
PointerInput.Origin.viewport(), source.x, source.y));
dragNDrop.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(700),
PointerInput.Origin.viewport(),target.x, target.y));
dragNDrop.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(dragNDrop));
```
```python
# Python
element = driver.find_element_by_accessibility_id("elId")
actions = ActionChains(driver)
actions.move_to_element(element)
actions.click(hidden_submenu)
actions.perform()
```
```javascript
// Javascript
// webdriver.io example
// Example: expressing a 1-second pinch-and-zoom
// with a 500ms wait after the fingers first touch:
driver.performActions([{
"type": "pointer",
"id": "finger1",
"parameters": {"pointerType": "touch"},
"actions": [
{"type": "pointerMove", "duration": 0, "x": 100, "y": 100},
{"type": "pointerDown", "button": 0},
{"type": "pause", "duration": 500},
{"type": "pointerMove", "duration": 1000, "origin": "pointer", "x": -50, "y": 0},
{"type": "pointerUp", "button": 0}
]
}, {
"type": "pointer",
"id": "finger2",
"parameters": {"pointerType": "touch"},
"actions": [
{"type": "pointerMove", "duration": 0, "x": 100, "y": 100},
{"type": "pointerDown", "button": 0},
{"type": "pause", "duration": 500},
{"type": "pointerMove", "duration": 1000, "origin": "pointer", "x": 50, "y": 0},
{"type": "pointerUp", "button": 0}
]
}]);
// release an action
driver.releaseActions();
// wd example
// Performs a 'pinch-and-zoom'
var actions = new wd.W3CActions(driver);
var touchInput = actions.addTouchInput();
touchInput.pointerMove({duration: 0, x: 100, y: 100});
touchInput.pointerDown({button: 0});
touchInput.pause({duration: 500});
touchInput.pointerMove({duration: 1000, origin: 'pointer', x: -50, y: 100});
touchInput.pointerUp({button: 0});
var secondTouchInput = actions.addTouchInput();
secondTouchInput.pointerMove({duration: 0, x: 200, y: 200});
secondTouchInput.pointerDown({button: 0});
secondTouchInput.pause({duration: 300});
secondTouchInput.pointerMove({duration: 1000, origin: 'pointer', x: 50, y: 100});
secondTouchInput.pointerUp({button: 0});
await actions.perform();
// Releases any previously run actions (e.g.: if a key is 'down' because of /actions, releases it using key up)
await driver.releaseW3CActions();
```
```ruby
# Ruby
# ruby_lib example
# Send keys to an element
# Build Single action chain
action_builder = action
keyboard = action_builder.key_inputs
el = find_element(id: "some_id")
action.click(el).pause(keyboard).pause(keyboard).pause(keyboard).send_keys('keys').perform
# Build multiple action chains
# Example: expressing a 1-second pinch-and-zoom
# with a 500ms wait after the fingers first touch:
f1 = action.add_pointer_input(:touch, 'finger1')
f1.create_pointer_move(duration: 1, x: 200, y: 500, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f1.create_pointer_down(:left)
f1.create_pause(0.5)
f1.create_pointer_move(duration: 1, x: 200, y: 200, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f1.create_pointer_up(:left)
f2 = action.add_pointer_input(:touch, 'finger2')
f2.create_pointer_move(duration: 1, x: 200, y: 500, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f2.create_pointer_down(:left)
f2.create_pause(0.5)
f2.create_pointer_move(duration: 1, x: 200, y: 800, origin: ::Selenium::Web@Driver::Interactions::PointerMove::VIEWPORT)
f2.create_pointer_up(:left)
perform_actions [f1, f2]
# ruby_lib_core example
# Send keys to an element
# Build Single action chain
action_builder = @driver.action
keyboard = action_builder.key_inputs
el = @driver.find_element(id: "some_id")
@driver.action.click(el).pause(keyboard).pause(keyboard).pause(keyboard).send_keys('keys').perform
# Build multiple action chains
# Example: expressing a 1-second pinch-and-zoom
# with a 500ms wait after the fingers first touch:
f1 = @driver.action.add_pointer_input(:touch, 'finger1')
f1.create_pointer_move(duration: 1, x: 200, y: 500, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f1.create_pointer_down(:left)
f1.create_pause(0.5)
f1.create_pointer_move(duration: 1, x: 200, y: 200, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f1.create_pointer_up(:left)
f2 = @driver.action.add_pointer_input(:touch, 'finger2')
f2.create_pointer_move(duration: 1, x: 200, y: 500, origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
f2.create_pointer_down(:left)
f2.create_pause(0.5)
f2.create_pointer_move(duration: 1, x: 200, y: 800, origin: ::Selenium::Web@Driver::Interactions::PointerMove::VIEWPORT)
f2.create_pointer_up(:left)
@driver.perform_actions [f1, f2]
```
```php
# PHP
// TODO
```
```csharp
// C#
var inputDevice = new PointerInputDevice(PointerKind.Touch);
var actionSequence = new ActionSequence(inputDevice, 0);
actionSequence.AddAction(inputDevice.CreatePointerMove(element));
actionSequence.AddAction(inputDevice.CreatePointerDown(PointerButton.TouchContact));
actionSequence.AddAction(inputDevice.CreatePause(TimeSpan.FromSeconds(1)));
actionSequence.AddAction(inputDevice.CreatePointerUp(PointerButton.TouchContact));
driver.PerformActions(new List<ActionSequence> {actionSequence});
```
## 描述
* input source: 表示一系列操作被分派到的输入设备(指针或键)。输入源具有唯一的ID。
* action: 分派到输入源的操作。 对于键盘源,操作行为可以是“ keyDown”或“ keyUp”。 对于指针事件,可以是“pointerMove”,“pointerDown”或“pointerUp”。 “pause”事件也可以发送到设备。
The Actions API会获取输入源列表,并执行每个”tick”。一个 “tick”是动作链的一部分,因此,如果您有两个输入源,那么第一个”tick”是索引为0的动作,第二个“tick”是索引为1的动作,依此类推。所有动作的每个“tick“均同时执行。
## 支持
### Appium Server
|平台|Driver|平台版本|Appium版本|Driver版本|
|--------|----------------|------|--------------|--------------|
| iOS | [XCUITest](../../drivers/ios-xcuitest.md) | 9.3+ | 1.6.0+ | All |
| | [UIAutomation](../../drivers/ios-uiautomation.md) | None | None | None |
| Android | [Espresso](../../drivers/android-espresso.md) | ?+ | 1.9.0+ | All |
| | [UiAutomator2](../../drivers/android-uiautomator2.md) | ?+ | 1.6.0+ | All |
| | [UiAutomator](../../drivers/android-uiautomator.md) | None | None | None |
| Mac | [Mac](../../drivers/mac.md) | ?+ | 1.6.4+ | All |
| Windows | [Windows](../../drivers/windows.md) | 10+ | 1.6.0+ | All |
### Appium 客户端
|语言|支持|文档|
|--------|-------|-------------|
|[Java](https://github.com/appium/java-client/releases/latest)| All | [seleniumhq.github.io](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html) |
|[Python](https://github.com/appium/python-client/releases/latest)| All | [selenium-python.readthedocs.io](https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains) |
|[Javascript (WebdriverIO)](http://webdriver.io/index.html)| All | |
|[Javascript (WD)](https://github.com/admc/wd/releases/latest)| All | |
|[Ruby](https://github.com/appium/ruby_lib/releases/latest)| All | [www.rubydoc.info](https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/W3CActionBuilder) |
|[PHP](https://github.com/appium/php-client/releases/latest)| All | [github.com](https://github.com/jupeter/selenium-php-webdriver/blob/master/Webdriver/Common/ActionChains.php) |
|[C#](https://github.com/appium/appium-dotnet-driver/releases/latest)| All | [github.com](https://github.com/appium/appium-dotnet-driver/) |
## HTTP API 规范
### 终端
`POST /session/:sessionId/actions`
### URL 参数
|名称|描述|
|----|-----------|
|session_id|将指令发往的会话(session)的ID|
### JSON 参数
|名称|类型|描述|
|----|----|-----------|
| actions | `array<array>` | 一系列输入源 |
| actions[$INDEX] | `object` | 代表输入源的对象 |
| actions[$INDEX].type | `string` | 输入源的类型。 可以是“point”,“key”或“null” |
| actions[$INDEX].id | `string` | 输入设备的唯一标识符,用于当前和将来的操作 |
| actions[$INDEX].parameters | `object` | 可选)设置输入源的参数。 “pointer”是必须输入项 |
| actions[$INDEX].parameters.pointerType | `string` | 指针类型。 可以是“touch”,“mouse”或“pen” |
| actions[$INDEX].actions | `array<object>` | 在输入源上执行的操作的列表 |
| actions[$INDEX].actions | `array<object>` | 在输入源上执行的操作的列表 |
| actions[$INDEX].actions[$INDEX] | `object` | 在输入源上执行的操作 |
| actions[$INDEX].actions[$INDEX].type | `string` | 操作类型。 对于任何输入源,都可以“pause”。 对于“pointer”,输入源为“ pointerMove”,“ pointerUp”或“ pointerDown”。 对于“key”,它可以是“ keyDown”或“ keyUp” |
| actions[$INDEX].actions[$INDEX].value | `string` | 对于“ keyUp”或“ keyDown”操作,该值将发送到键盘。 应该是一个字符的字符串(“ s”,“ \ uE009”) |
| actions[$INDEX].actions[$INDEX].duration | `number` | 以“ ms”为单位执行操作的时间。 仅适用于“pause”和“pointerMov”。 |
| actions[$INDEX].actions[$INDEX].origin | `string|object` | 对于'pointerMove',它告诉输入源x,y是相对的。 可以是“viewport”,“pointe”或\{'element-6066-11e4-a52e-4f735466cecf': '<ELEMENT_ID>'\} |
| actions[$INDEX].actions[$INDEX].x | `number` | 指针移动事件的X坐标 |
| actions[$INDEX].actions[$INDEX].y | `number` | 指针移动事件的Y坐标 |
### 响应
null
## 参考
* [W3C Specification](https://www.w3.org/TR/webdriver/#actions)
- 关于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 包结构
- 鸣谢