Class: Appium::Core::TouchAction

Inherits:
Object
  • Object
show all
Defined in:
lib/appium_lib_core/common/touch_action/touch_actions.rb

Overview

Perform a series of gestures, one after another. Gestures are chained together and only performed when ‘perform()` is called. Default is conducted by global driver.

Each method returns the object itself, so calls can be chained.

Consider to use W3C spec touch action like the followings. seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/W3CActionBuilder.html github.com/appium/ruby_lib_core/blob/master/test/functional/android/webdriver/w3c_actions_test.rb github.com/appium/ruby_lib_core/blob/master/test/functional/ios/webdriver/w3c_actions_test.rb

Examples:


@driver = Appium::Core.for(self, opts).start_driver
action = TouchAction.new(@driver).press(x: 45, y: 100).wait(5).release
action.perform
action = TouchAction.new(@driver).swipe(....)
action.perform

Constant Summary collapse

ACTIONS =
i(move_to long_press double_tap two_finger_tap press release tap wait perform).freeze
COMPLEX_ACTIONS =
i(swipe).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ TouchAction



27
28
29
30
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 27

def initialize(driver)
  @actions = []
  @driver = driver
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



25
26
27
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 25

def actions
  @actions
end

#driverObject (readonly)

Returns the value of attribute driver.



25
26
27
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 25

def driver
  @driver
end

Instance Method Details

#cancelObject

Does nothing, currently.



173
174
175
176
177
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 173

def cancel
  @actions << { action: cancel }
  @driver.touch_actions @actions
  self
end

#double_tap(opts) ⇒ Object

Double tap an element on the screen

Options Hash (opts):

  • :element (WebDriver::Element) — default: Optional

    Element to restrict scope too.

  • :x (integer)

    x co-ordinate to tap

  • :y (integer)

    y co-ordinate to tap



106
107
108
109
110
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 106

def double_tap(opts)
  args = opts.select { |k, _v| i(element x y).include? k }
  args = args_with_ele_ref(args)
  chain_method(:doubleTap, args) # doubleTap is what the appium server expects
end

#long_press(opts) ⇒ Object

Press down for a specific duration. Alternatively, you can use ‘press(…).wait(…).release()` instead of `long_press` if duration doesn’t work well. github.com/appium/ruby_lib/issues/231#issuecomment-269895512 e.g. Appium::TouchAction.new.press(x: 280, y: 530).wait(2000).release.perform

Options Hash (opts):

  • element (WebDriver::Element)

    the element to press.

  • x (integer)

    X co-ordinate to press on.

  • y (integer)

    Y co-ordinate to press on.

  • duration (integer)

    Number of milliseconds to press.



55
56
57
58
59
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 55

def long_press(opts)
  args = opts.select { |k, _v| i(element x y duration).include? k }
  args = args_with_ele_ref(args)
  chain_method(:longPress, args) # longPress is what the appium server expects
end

#move_to(opts) ⇒ Object

Move to the given co-ordinates.

‘move_to`’s ‘x` and `y` have two case. One is working as coordinate, the other is working as offset.

Options Hash (opts):

  • :x (integer)

    x co-ordinate to move to if element isn’t set. Works as an absolute if x is set with Element.

  • :y (integer)

    y co-ordinate to move to if element isn’t set. Works as an absolute if y is set with Element.

  • Element (WebDriver::Element)

    to scope this move within.



40
41
42
43
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 40

def move_to(opts)
  opts = args_with_ele_ref(opts)
  chain_method(:moveTo, opts)
end

#performObject

Ask the driver to perform all actions in this action chain.



166
167
168
169
170
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 166

def perform
  @driver.touch_actions @actions
  @actions.clear
  self
end

#press(opts) ⇒ Object

Press a finger onto the screen. Finger will stay down until you call ‘release`.

Options Hash (opts):

  • :element (WebDriver::Element) — default: Optional

    Element to press within.

  • :x (integer)

    x co-ordinate to press on

  • :y (integer)

    y co-ordinate to press on



67
68
69
70
71
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 67

def press(opts)
  args = opts.select { |k, _v| i(element x y).include? k }
  args = args_with_ele_ref(args)
  chain_method(:press, args)
end

#release(opts = nil) ⇒ Object

Remove a finger from the screen.

Options Hash (opts):

  • :element (WebDriver::Element) — default: Optional

    Element to release from.

  • :x (integer)

    x co-ordinate to release from

  • :y (integer)

    y co-ordinate to release from



79
80
81
82
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 79

def release(opts = nil)
  args = args_with_ele_ref(opts) if opts
  chain_method(:release, args)
end

#swipe(opts) ⇒ Object

Convenience method to perform a swipe.

Options Hash (opts):

  • :start_x (int)

    Where to start swiping, on the x axis. Default 0.

  • :start_y (int)

    Where to start swiping, on the y axis. Default 0.

  • :end_x (int)

    Move to the end, on the x axis. Default 0.

  • :end_y (int)

    Move to the end, on the y axis. Default 0.

  • :duration (int)

    How long the actual swipe takes to complete in milliseconds. Default 200.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 139

def swipe(opts)
  start_x = opts.fetch :start_x, 0
  start_y = opts.fetch :start_y, 0
  end_x   = opts.fetch :end_x, 0
  end_y   = opts.fetch :end_y, 0

  if opts[:offset_x]
    ::Appium::Logger.warn('[DEPRECATED] :offset_x was renamed to :end_x to indicate as an absolute point.')
    end_x = opts.fetch :offset_x, 0
  end
  if opts[:offset_y]
    ::Appium::Logger.warn('[DEPRECATED] :offset_y was renamed to :end_y to indicate as an absolute point.')
    end_y = opts.fetch :offset_y, 0
  end

  duration = opts.fetch :duration, 200

  press x: start_x, y: start_y
  wait(duration) if duration
  move_to x: end_x, y: end_y

  release

  self
end

#tap(opts) ⇒ Object

Touch a point on the screen. Alternatively, you can use ‘press(…).release.perform` instead of `tap(…).perform`.

Options Hash (opts):

  • :element (WebDriver::Element) — default: Optional

    Element to restrict scope too.

  • :x (integer)

    x co-ordinate to tap

  • :y (integer)

    y co-ordinate to tap

  • :fingers (integer)

    how many fingers to tap with (Default 1)



92
93
94
95
96
97
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 92

def tap(opts)
  opts[:count] = opts.delete(:fingers) if opts[:fingers]
  opts[:count] ||= 1
  args = args_with_ele_ref opts
  chain_method(:tap, args)
end

#two_finger_tap(opts) ⇒ Object

Two finger tap an element on the screen

Options Hash (opts):

  • :element (WebDriver::Element) — default: Optional

    Element to restrict scope too.

  • :x (integer)

    x co-ordinate to tap

  • :y (integer)

    y co-ordinate to tap



118
119
120
121
122
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 118

def two_finger_tap(opts)
  args = opts.select { |k, _v| i(element x y).include? k }
  args = args_with_ele_ref(args)
  chain_method(:twoFingerTap, args) # twoFingerTap is what the appium server expects
end

#wait(milliseconds) ⇒ Object

Pause for a number of milliseconds before the next action



126
127
128
129
# File 'lib/appium_lib_core/common/touch_action/touch_actions.rb', line 126

def wait(milliseconds)
  args = { ms: milliseconds }
  chain_method(:wait, args)
end