Skip to content

Commit de2d747

Browse files
authored
add send_actions for w3c multiple actions (#140)
* addd send_actions for w3c multiple actions * tweak actions * tweak * update tests * update docstring
1 parent bbbc6f5 commit de2d747

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

lib/appium_lib_core/common/base/driver.rb

+28
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,34 @@ def compare_images(mode: :matchFeatures, first_image:, second_image:, options: n
798798
@bridge.compare_images(mode: mode, first_image: first_image, second_image: second_image, options: options)
799799
end
800800

801+
# For multiple actions
802+
# @param [Array] data Array of actions
803+
# @return nil|error
804+
#
805+
# @example: Zoom
806+
#
807+
# f1 = @driver.action.add_pointer_input(:touch, 'finger1')
808+
# f1.create_pointer_move(duration: 1, x: 200, y: 500,
809+
# origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
810+
# f1.create_pointer_down(:left)
811+
# f1.create_pointer_move(duration: 1, x: 200, y: 200,
812+
# origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
813+
# f1.create_pointer_up(:left)
814+
#
815+
# f2 = @driver.action.add_pointer_input(:touch, 'finger2')
816+
# f2.create_pointer_move(duration: 1, x: 200, y: 500,
817+
# origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
818+
# f2.create_pointer_down(:left)
819+
# f2.create_pointer_move(duration: 1, x: 200, y: 800,
820+
# origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
821+
# f2.create_pointer_up(:left)
822+
#
823+
# @driver.send_actions [f1, f2] #=> `nil` if the action succeed
824+
#
825+
def send_actions(data)
826+
@bridge.send_actions data.map(&:encode).compact
827+
end
828+
801829
# @since Appium 1.8.2
802830
# Return an element if current view has a partial image. The logic depends on template matching by OpenCV.
803831
# @see https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/image-comparison.md

test/functional/android/webdriver/w3c_actions_test.rb

+21
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ def test_tap
4545
el = @@core.wait { @driver.find_element(:accessibility_id, 'ImageButton') }
4646
assert_equal 'ImageButton', el.text
4747
end
48+
49+
# Note: Works with Espresso Driver
50+
def test_multiple_actions
51+
f1 = @driver.action.add_pointer_input(:touch, 'finger1')
52+
f1.create_pointer_move(duration: 1, x: 200, y: 500,
53+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
54+
f1.create_pointer_down(:left)
55+
f1.create_pointer_move(duration: 1, x: 200, y: 200,
56+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
57+
f1.create_pointer_up(:left)
58+
59+
f2 = @driver.action.add_pointer_input(:touch, 'finger2')
60+
f2.create_pointer_move(duration: 1, x: 200, y: 500,
61+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
62+
f2.create_pointer_down(:left)
63+
f2.create_pointer_move(duration: 1, x: 200, y: 800,
64+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
65+
f2.create_pointer_up(:left)
66+
67+
@driver.send_actions [f1, f2]
68+
end
4869
end
4970
end
5071
end

test/test_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def android
8080
platformName: :android,
8181
automationName: 'uiautomator2',
8282
app: 'test/functional/app/api.apk',
83-
platformVersion: '7.1.1',
83+
platformVersion: '8.1',
8484
deviceName: 'Android Emulator',
8585
appPackage: 'io.appium.android.apis',
8686
appActivity: 'io.appium.android.apis.ApiDemos',

test/unit/android/webdriver/w3c/actions_test.rb

+50
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,56 @@ def test_w3c_actions
5959

6060
assert_requested(:post, "#{SESSION}/actions", times: 1)
6161
end
62+
63+
def test_w3c__multiple_actions
64+
action_body = {
65+
actions: [{
66+
type: :pointer,
67+
id: 'finger1',
68+
actions: [
69+
{ type: :pointerMove, duration: 50, x: 100, y: 100, origin: 'viewport' },
70+
{ type: :pointerDown, button: 0 },
71+
{ type: :pointerMove, duration: 50, x: 50, y: 100, origin: 'viewport' },
72+
{ type: :pointerUp, button: 0 }
73+
],
74+
parameters: { pointerType: :touch }
75+
}, {
76+
type: :pointer,
77+
id: 'finger2',
78+
actions: [
79+
{ type: :pointerMove, duration: 50, x: 100, y: 100, origin: 'viewport' },
80+
{ type: :pointerDown, button: 0 },
81+
{ type: :pointerMove, duration: 50, x: 150, y: 100, origin: 'viewport' },
82+
{ type: :pointerUp, button: 0 }
83+
],
84+
parameters: { pointerType: :touch }
85+
}]
86+
}
87+
88+
stub_request(:post, "#{SESSION}/actions")
89+
.with(body: action_body.to_json)
90+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
91+
92+
f1 = @driver.action.add_pointer_input(:touch, 'finger1')
93+
f1.create_pointer_move(duration: 0.05, x: 100, y: 100,
94+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
95+
f1.create_pointer_down(:left)
96+
f1.create_pointer_move(duration: 0.05, x: 50, y: 100,
97+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
98+
f1.create_pointer_up(:left)
99+
100+
f2 = @driver.action.add_pointer_input(:touch, 'finger2')
101+
f2.create_pointer_move(duration: 0.05, x: 100, y: 100,
102+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
103+
f2.create_pointer_down(:left)
104+
f2.create_pointer_move(duration: 0.05, x: 150, y: 100,
105+
origin: ::Selenium::WebDriver::Interactions::PointerMove::VIEWPORT)
106+
f2.create_pointer_up(:left)
107+
108+
@driver.send_actions [f1, f2]
109+
110+
assert_requested(:post, "#{SESSION}/actions", times: 1)
111+
end
62112
end # class CommandsTest
63113
end # module W3C
64114
end # module WebDriver

0 commit comments

Comments
 (0)