Actions
Perform a chain or multiple chains of keyboard and pointer (touch, mouse, stylus) actions //执行一个或多个键盘和指针(触摸、鼠标、手写笔)操作链
Example Usage
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 | element = driver.find_element_by_accessibility_id("elId" )actions = ActionChains(driver) actions.move_to_element(element) actions.click(hidden_submenu) actions.perform() |
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_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] |
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}); |
PHP | // TODO |
Description
- input source: Represents an input device (pointer or key) that a series of actions are dispatched to. The input source has a unique ID.
/* 输入源:表示一系列操作被分派到的输入设备(指针或键)。输入源具有唯一的ID */ - action: An action that is dispatched to an input source. For a keyboard source, this can be ‘keyDown’ or ‘keyUp’. For a pointer event this can be ‘pointerMove’, ‘pointerDown’, or ‘pointerUp’. ‘pause’ events can also be sent to the device.
/* 动作:分派到输入源的动作。对于键盘源,可以是“keyDown”或“keyUp”。对于指针事件,可以是“pointerMove”、“pointerDown”或“pointerUp”“暂停”事件也可以发送到设备 /
The Actions API takes a list of input sources and executes each ‘tick’. A ‘tick’ is a slice of an action chain, so if you have two input sources, the first ‘tick’ is the 0-indexed action, the second ‘tick’ is the 1-indexed action, etc… All of the actions per tick are executed concurrently.
/ Actions API获取输入源列表并执行每个“勾选”。“tick”是动作链的一部分,因此如果有两个输入源,第一个“tick”是0索引动作,第二个“tick”是1索引动作,等等。。。。每个勾号的所有操作都同时执行 */
Support
Appium Server
Platform | Driver | Platform Versions | Appium Version | Driver Version |
---|---|---|---|---|
iOS | XCUITest | 9.3+ | 1.6.0+ | All |
UIAutomation | None | None | None | |
Android | Espresso | ?+ | 1.9.0+ | All |
UiAutomator2 | ?+ | 1.6.0+ | All | |
UiAutomator | None | None | None | |
Mac | Mac | ?+ | 1.6.4+ | All |
Windows | Windows | 10+ | 1.6.0+ | All |
Appium Clients
Language | Support | Documentation |
---|---|---|
Java | All | seleniumhq.github.io |
Python | All | selenium-python.readthedocs.io |
Javascript (WebdriverIO) | All | |
Javascript (WD) | All | |
Ruby | All | www.rubydoc.info |
PHP | All | github.com |
C# | All | github.com |
HTTP API Specifications
Endpoint
POST /session/:sessionId/actions
URL Parameters
name | description |
---|---|
session_id | ID of the session to route the command to |
JSON Parameters
name | type | description |
---|---|---|
actions | array<array> | An array of input sources |
actions[$INDEX] | object | An object that represents an input source |
actions[$INDEX].type | string | The type of input source. Can be ‘pointer’, ‘key’ or ‘null’ |
actions[$INDEX].id | string | Unique identifier of the input device which is used for current and future actions |
actions[$INDEX].parameters | object | (optional) Set parameters for the input source. Required for ‘pointer’ inputs |
actions[$INDEX].parameters.pointerType | string | Type of pointer. Can be ‘touch’, ‘mouse’ or ‘pen’ |
actions[$INDEX].actions | array<object> | A list of actions to perform on the input source |
actions[$INDEX].actions | array<object> | A list of actions to perform on the input source |
actions[$ INDEX].actions[$INDEX] | object | The action to perform on the input source |
actions[$ INDEX].actions[$INDEX].type | string | The type of action. For any input source it can be ‘pause’. For ‘pointer’ input source ‘pointerMove’, ‘pointerUp’ or ‘pointerDown’. For ‘key’ it can be ‘keyDown’ or ‘keyUp’ |
actions[$ INDEX].actions[$INDEX].value | string | For a ‘keyUp’ or ‘keyDown’ action thevalue to send to the keyboard. Should be a one-character string (“s”, “\uE009”) |
actions[$ INDEX].actions[$INDEX].duration | number | How long to perform the action in ‘ms’. Only applicable to ‘pause’ and ‘pointerMove’. |
actions[$ INDEX].actions[$INDEX].origin | string``|``object | For ‘pointerMove’, this tells the input source what x,y are relative to. Can be ‘viewport’, ‘pointer’ or {‘element-6066-11e4-a52e-4f735466cecf’: ‘’} |
actions[$ INDEX].actions[$INDEX].x | number | X coordinate of pointer move event |
actions[$ INDEX].actions[$INDEX].y | number | Y coordinate of pointer move event |
Response
null