Class: Appium::TouchAction

Inherits:
Object show all
Defined in:
lib/appium_lib/device/touch_actions.rb

Overview

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

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

“‘ruby action = TouchAction.new.press(x: 45, y: 100).wait(5).release action.perform

Constant Summary collapse

ACTIONS =
[:move_to, :long_press, :press, :release, :tap, :wait, :perform]
COMPLEX_ACTIONS =
[:swipe]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTouchAction

Returns a new instance of TouchAction.



28
29
30
# File 'lib/appium_lib/device/touch_actions.rb', line 28

def initialize
  @actions = []
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



26
27
28
# File 'lib/appium_lib/device/touch_actions.rb', line 26

def actions
  @actions
end

Instance Method Details

#cancelObject

Does nothing, currently.



124
125
126
127
128
# File 'lib/appium_lib/device/touch_actions.rb', line 124

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

#long_press(opts) ⇒ Object

Press down for a specific duration.

Parameters:

  • element (Hash)

    a customizable set of options

  • x (Hash)

    a customizable set of options

  • y (Hash)

    a customizable set of options

  • duration (Hash)

    a customizable set of options



46
47
48
49
50
# File 'lib/appium_lib/device/touch_actions.rb', line 46

def long_press(opts)
  args = opts.select { |k, _v| [: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.

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :x (integer)

    x co-ordinate to move to.

  • :y (integer)

    y co-ordinate to move to.

  • Element (WebDriver::Element)

    to scope this move within.



36
37
38
39
# File 'lib/appium_lib/device/touch_actions.rb', line 36

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.



118
119
120
121
# File 'lib/appium_lib/device/touch_actions.rb', line 118

def perform
  $driver.touch_actions @actions
  self
end

#press(opts) ⇒ Object

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

Parameters:

  • opts (Hash)

    a customizable set of options

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



58
59
60
61
62
# File 'lib/appium_lib/device/touch_actions.rb', line 58

def press(opts)
  args = opts.select { |k, _v| [: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.

Parameters:

  • opts (Hash) (defaults to: nil)

    a customizable set of options

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



69
70
71
72
# File 'lib/appium_lib/device/touch_actions.rb', line 69

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

#swipe(opts) ⇒ Object

Convenience method to peform a swipe.

Note that iOS 7 simulators have broken swipe.

Parameters:

  • opts (Hash)

    a customizable set of options

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)

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

  • :end_y (int)

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

  • :duration (int)

    How long the actual swipe takes to complete in milliseconds.



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/appium_lib/device/touch_actions.rb', line 103

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
  duration = opts[:duration]

  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

Parameters:

  • opts (Hash)

    a customizable set of options

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)



80
81
82
83
84
85
# File 'lib/appium_lib/device/touch_actions.rb', line 80

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

#wait(milliseconds) ⇒ Object

Pause for a number of milliseconds before the next action

Parameters:

  • milliseconds (integer)

    Number of milliseconds to pause for



89
90
91
92
# File 'lib/appium_lib/device/touch_actions.rb', line 89

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