Class: TestaAppiumDriver::ScrollActions

Inherits:
Object
  • Object
show all
Includes:
JsonWireScrollActions, W3cScrollActions
Defined in:
lib/testa_appium_driver/common/scroll_actions.rb,
lib/testa_appium_driver/android/scroll_actions/uiautomator_scroll_actions.rb

Overview

noinspection RubyInstanceMethodNamingConvention

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from W3cScrollActions

#apply_w3c_correction, #w3c_action, #w3c_align, #w3c_attempt_align, #w3c_drag_to, #w3c_page_or_fling, #w3c_scroll_each, #w3c_scroll_each_v1, #w3c_scroll_each_v2, #w3c_scroll_to, #w3c_scroll_to_start_or_end

Constructor Details

#initialize(scrollable, params = {}) ⇒ ScrollActions



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 19

def initialize(scrollable, params = {})
  @scrollable = scrollable
  @locator = params[:locator]
  # TODO: raise error if locator is for multiple, but not for scroll each, chekc other cases aswell
  @deadzone = params[:deadzone]
  @max_scrolls = params[:max_scrolls]
  @default_scroll_strategy = params[:default_scroll_strategy]
  @driver = @locator.driver

  if @scrollable.nil?
    # if we dont have a scrollable element or if we do have it, but it is not compatible with uiautomator
    # then find first scrollable in document
    @scrollable = @driver.scrollable
  end

  @strategy = nil
  if @scrollable.strategy == FIND_STRATEGY_XPATH || # uiautomator cannot resolve scrollable from a xpath locator
    !@deadzone.nil? ||
    !@scrollable.from_element.instance_of?(TestaAppiumDriver::Driver) # uiautomator cannot resolve nested scrollable
    @strategy = SCROLL_STRATEGY_W3C
  end

  @bounds = @scrollable.bounds
end

Instance Attribute Details

#locatorObject

Returns the value of attribute locator.



11
12
13
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 11

def locator
  @locator
end

Instance Method Details

#align(with, scroll_to_find, max_attempts) ⇒ Object



44
45
46
47
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 44

def align(with, scroll_to_find, max_attempts)
  w3c_align(with, scroll_to_find, max_attempts)
  @locator
end

#default_deadzone!Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 245

def default_deadzone!
  @deadzone = {} if @deadzone.nil?
  if @deadzone[:top].nil?
    @deadzone[:top] = 1
  else
    @deadzone[:top] = @deadzone[:top].to_f
  end
  if @deadzone[:bottom].nil?
    @deadzone[:bottom] = 1
  else
    @deadzone[:bottom] = @deadzone[:bottom].to_f
  end
  if @deadzone[:right].nil?
    @deadzone[:right] = 1
  else
    @deadzone[:right] = @deadzone[:right].to_f
  end
  if @deadzone[:left].nil?
    @deadzone[:left] = 1
  else
    @deadzone[:left] = @deadzone[:left].to_f
  end
end

#drag_to(x0, y0, x1, y1) ⇒ Object



199
200
201
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 199

def drag_to(x0, y0, x1, y1)
  w3c_drag_to(x0, y0, x1, y1)
end

#fling_downObject



167
168
169
170
171
172
173
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 167

def fling_down
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_FLING, :down)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_FLING, :down)
  end
end

#fling_leftObject



191
192
193
194
195
196
197
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 191

def fling_left
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_FLING, :left)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_FLING, :left)
  end
end

#fling_rightObject



175
176
177
178
179
180
181
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 175

def fling_right
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_FLING, :right)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_FLING, :right)
  end
end

#fling_upObject



183
184
185
186
187
188
189
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 183

def fling_up
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_FLING, :up)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_FLING, :up)
  end
end

#is_aligned?(with, element) ⇒ Boolean



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 203

def is_aligned?(with, element)
  align_bounds = @locator.bounds(force_cache_element: element)
  case with
  when :top
    @align_offset = align_bounds.top_left.y - @bounds.top_left.y - @deadzone[:top]
  when :bottom
    @align_offset = @bounds.bottom_right.y - @deadzone[:bottom] - align_bounds.bottom_right.y
  when :right
    @align_offset = @bounds.bottom_right.x - @deadzone[:right] - align_bounds.bottom_right.x
  when :left
    @align_offset = align_bounds.top_left.x - @bounds.top_left.x - @deadzone[:left]
  else
    raise "Unsupported align with option: #{with}"
  end
  @align_offset < SCROLL_ALIGNMENT_THRESHOLD
end

#is_end_of_scroll?Boolean



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 222

def is_end_of_scroll?
  # if @driver.device == :android

    old_elements = @previous_elements
    @previous_elements = @scrollable.first_and_last_leaf


    # $__.puts "old: #{old_elements}"
    # $__.puts "new: #{@previous_elements}"
    # $__.puts "end_of_scroll? #{     old_elements == @previous_elements}"
    old_elements == @previous_elements
  # else
  #   # for ios, check location of first and last elements
  #   old_elements = @previous_elements
  #   @previous_elements = @scrollable.first_and_last_child&.map(&:location)
  #   # $__.puts "old:"
  #   # $__.puts old_elements
  #   # $__.puts "prev:"
  #   # $__.puts @previous_elements
  #   old_elements == @previous_elements
  # end
end

#page_backObject



110
111
112
113
114
115
116
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 110

def page_back
  if @scrollable.scroll_orientation == :vertical
    page_up
  else
    page_right
  end
end

#page_downObject



118
119
120
121
122
123
124
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 118

def page_down
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :down)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :down)
  end
end

#page_leftObject



142
143
144
145
146
147
148
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 142

def page_left
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :left)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :left)
  end
end

#page_nextObject



102
103
104
105
106
107
108
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 102

def page_next
  if @scrollable.scroll_orientation == :vertical
    page_down
  else
    page_left
  end
end

#page_rightObject



126
127
128
129
130
131
132
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 126

def page_right
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :right)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :right)
  end
end

#page_upObject



134
135
136
137
138
139
140
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 134

def page_up
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :up)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_page_or_fling(SCROLL_ACTION_TYPE_SCROLL, :up)
  end
end

#resolve_strategyObject



70
71
72
73
74
75
76
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 70

def resolve_strategy
  if @strategy.nil?
    @default_scroll_strategy
  else
    @strategy
  end
end

#scroll_down_toObject



86
87
88
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 86

def scroll_down_to
  w3c_scroll_to(:down)
end

#scroll_each(&block) ⇒ Array



50
51
52
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 50

def scroll_each(&block)
  w3c_scroll_each(nil, &block)
end

#scroll_each_down(&block) ⇒ Object



54
55
56
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 54

def scroll_each_down(&block)
  w3c_scroll_each(:down, &block)
end

#scroll_each_left(&block) ⇒ Object



66
67
68
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 66

def scroll_each_left(&block)
  w3c_scroll_each(:left, &block)
end

#scroll_each_right(&block) ⇒ Object



62
63
64
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 62

def scroll_each_right(&block)
  w3c_scroll_each(:right, &block)
end

#scroll_each_up(&block) ⇒ Object



58
59
60
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 58

def scroll_each_up(&block)
  w3c_scroll_each(:up, &block)
end

#scroll_left_toObject



98
99
100
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 98

def scroll_left_to
  w3c_scroll_to(:left)
end

#scroll_right_toObject



94
95
96
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 94

def scroll_right_to
  w3c_scroll_to(:right)
end

#scroll_toObject



78
79
80
81
82
83
84
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 78

def scroll_to
  if @locator.strategy != FIND_STRATEGY_XPATH && resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_scroll_to
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_scroll_to(nil)
  end
end

#scroll_to_endObject



159
160
161
162
163
164
165
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 159

def scroll_to_end
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_scroll_to_start_or_end(:end)
  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_scroll_to_start_or_end(:end)
  end
end

#scroll_to_startObject



150
151
152
153
154
155
156
157
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 150

def scroll_to_start
  if resolve_strategy == SCROLL_STRATEGY_UIAUTOMATOR
    uiautomator_scroll_to_start_or_end(:start)

  elsif resolve_strategy == SCROLL_STRATEGY_W3C
    w3c_scroll_to_start_or_end(:start)
  end
end

#scroll_up_toObject



90
91
92
# File 'lib/testa_appium_driver/common/scroll_actions.rb', line 90

def scroll_up_to
  w3c_scroll_to(:up)
end