Module: Briar::Core

Defined in:
lib/briar/briar_core.rb

Instance Method Summary collapse

Instance Method Details

#default_device_or_createObject

returns the device that is currently being tested against

returns the device attr of Calabash::Cucumber::Launcher if it is defined. otherwise, creates a new Calabash::Cucumber::Device by querying the server.

raises an error if the server cannot be reached



216
217
218
219
220
221
222
# File 'lib/briar/briar_core.rb', line 216

def default_device_or_create
  device = default_device()
  if device.nil?
    device = Calabash::Cucumber::Device.new(nil, server_version())
  end
  device
end

#deviceObject

DEPRECATED since 0.9.163

replaced with:

  • calabash function default_device

  • methods in calabash-cucumber/environment_helpers.rb

  • briar function default_device_or_create



199
200
201
202
203
204
205
206
207
# File 'lib/briar/briar_core.rb', line 199

def device
  msg = "use the calabash function 'default_device', one of the methods in calabash-cucumber/environment_helpers.rb', or briar's 'default_device_or_create'"
  _deprecated('0.9.163', msg, :warn)
  d = default_device_or_create()
  if d.nil?
    d = Calabash::Cucumber::Device.new(nil, server_version())
  end
  d
end

#error_no_rescue(msg) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/briar/briar_core.rb', line 224

def error_no_rescue(msg)
  if RUBY_VERSION < '2.0'
    stack = Kernel.caller()[1..6].join("\n")
  else
    stack = Kernel.caller(0, 6)[1..-1].join("\n")
  end

  msg = "ERROR: '#{msg}'\n#{stack}"

  # todo error_no_rescue function does not output on a new line when called within cucumber
  begin
    warn "\033[31m\n#{msg}\033[0m"
  rescue
    warn "\n#{msg}"
  end
end

#send_backdoor_command(command, args = []) ⇒ Object

backdoor helpers canonical backdoor command: ‘calabash_backdoor_handle_command’ selector key = :selector args key = :args



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/briar/briar_core.rb', line 171

def send_backdoor_command(command, args=[])
  if args.empty?
    json = "{\":selector\" : \"#{command}\"}"
    return backdoor('calabash_backdoor_handle_command:', json)
  end

  array = args.kind_of?(Array) ? args : [args]
  json = "{\":selector\" : \"#{command}\", \":args\" : #{array}}"
  res = backdoor('calabash_backdoor_handle_command:', json)
  if res.eql?('ERROR: no matching selector')
    screenshot_and_raise "no matching backdoor selector found for '#{command}'"
  end
  res
end

#should_not_see_view(view_id) ⇒ Object



30
31
32
33
34
# File 'lib/briar/briar_core.rb', line 30

def should_not_see_view (view_id)
  if view_exists? view_id
    screenshot_and_raise "should not see view with id '#{view_id}'"
  end
end

#should_see_view(view_id) ⇒ Object



24
25
26
27
28
# File 'lib/briar/briar_core.rb', line 24

def should_see_view (view_id)
  unless view_exists? view_id
    screenshot_and_raise "should see view with id '#{view_id}'"
  end
end

#should_see_view_with_center(view_id, center_ht) ⇒ Object

todo refactor should_see_view_with_center to wait for view



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/briar/briar_core.rb', line 54

def should_see_view_with_center(view_id, center_ht)
  res = query("view marked:'#{view_id}'").first
  if res.nil?
    screenshot_and_raise "should see view with id '#{view_id}'"
  end

  actual_ht = {:x =>  res['rect']['center_x'].to_f, :y =>  res['rect']['center_y'].to_f}

  unless actual_ht == center_ht
    screenshot_and_raise "#{view_id} has center '#{actual_ht}' but should have center '#{center_ht}'"
  end
end

#should_see_view_with_frame(view_id, frame) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/briar/briar_core.rb', line 40

def should_see_view_with_frame(view_id, frame)
  res = query("view marked:'#{view_id}'").first
  if res.empty?
    screenshot_and_raise "should see view with id '#{view_id}'"
  end
  actual = res['frame']
  ['x', 'y', 'width', 'height'].each { |key|
    avalue = actual[key]
    evalue = frame[key]
    screenshot_and_raise "#{view_id} should have '#{key}' '#{evalue}' but found '#{avalue}'"
  }
end

#should_see_view_with_text(text) ⇒ Object



67
68
69
70
71
72
# File 'lib/briar/briar_core.rb', line 67

def should_see_view_with_text (text)
  res = view_exists_with_text? text
  unless res
    screenshot_and_raise "should see view with text '#{text}'"
  end
end

#step_pauseObject



7
8
9
# File 'lib/briar/briar_core.rb', line 7

def step_pause
  sleep(BRIAR_STEP_PAUSE)
end

#step_pause_if_xtcObject



11
12
13
# File 'lib/briar/briar_core.rb', line 11

def step_pause_if_xtc
  sleep(BRIAR_STEP_PAUSE) if xamarin_test_cloud?
end

#tokenize_list(list) ⇒ Object



186
187
188
189
190
# File 'lib/briar/briar_core.rb', line 186

def tokenize_list (list)
  tokens = list.split(/[,]|(and )/)
  stripped = tokens.map { |elm| elm.strip }
  stripped.delete_if { |elm| ['and', 'or', ''].include?(elm) }
end

#touch_and_wait_for_view(view_id, view_to_wait_for, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



84
85
86
87
# File 'lib/briar/briar_core.rb', line 84

def touch_and_wait_for_view(view_id, view_to_wait_for, timeout=BRIAR_WAIT_TIMEOUT)
  touch_view(view_id)
  wait_for_view(view_to_wait_for, timeout)
end

#touch_and_wait_to_disappear(view_id, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



161
162
163
164
# File 'lib/briar/briar_core.rb', line 161

def touch_and_wait_to_disappear(view_id, timeout=BRIAR_WAIT_TIMEOUT)
  touch_view(view_id)
  wait_for_view_to_disappear view_id, timeout
end

#touch_custom_view(view_class, view_id, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



126
127
128
129
# File 'lib/briar/briar_core.rb', line 126

def touch_custom_view(view_class, view_id, timeout=BRIAR_WAIT_TIMEOUT)
  wait_for_custom_view view_class, view_id, timeout
  touch("view:'#{view_class}' marked:'#{view_id}'")
end

#touch_custom_view_and_wait_for_view(view_class, view_id, view_to_wait_for, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



132
133
134
135
136
# File 'lib/briar/briar_core.rb', line 132

def touch_custom_view_and_wait_for_view(view_class, view_id, view_to_wait_for, timeout=BRIAR_WAIT_TIMEOUT)
  wait_for_custom_view view_class, view_id, timeout
  touch("view:'#{view_class}' marked:'#{view_id}'")
  wait_for_view view_to_wait_for, timeout
end

#touch_view(view_id) ⇒ Object



74
75
76
77
# File 'lib/briar/briar_core.rb', line 74

def touch_view(view_id)
  wait_for_view view_id
  touch("view marked:'#{view_id}'")
end

#touch_view_named(view_id) ⇒ Object



79
80
81
82
# File 'lib/briar/briar_core.rb', line 79

def touch_view_named(view_id)
  _deprecated('0.1.3', "use 'touch_view' instead", :warn)
  touch_view(view_id)
end

#view_exists?(view_id) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/briar/briar_core.rb', line 20

def view_exists? (view_id)
  !query("view marked:'#{view_id}'").empty?
end

#view_exists_with_text?(text) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/briar/briar_core.rb', line 36

def view_exists_with_text? (text)
  element_exists("view text:'#{text}'")
end

#wait_for_animationObject



15
16
17
18
# File 'lib/briar/briar_core.rb', line 15

def wait_for_animation
  _deprecated('0.1.3', "use any of the 'wait_*' functions instead", :warn)
  sleep(0.6)
end

#wait_for_custom_view(view_class, view_id, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/briar/briar_core.rb', line 116

def wait_for_custom_view (view_class, view_id, timeout=BRIAR_WAIT_TIMEOUT)
  msg = "waited for '#{timeout}' seconds but did not see '#{view_id}'"
  wait_for(:timeout => timeout,
           :retry_frequency => BRIAR_WAIT_RETRY_FREQ,
           :post_timeout => BRIAR_WAIT_STEP_PAUSE,
           :timeout_message => msg) do
    !query("view:'#{view_class}' marked:'#{view_id}'").empty?
  end
end

#wait_for_query(qstr, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/briar/briar_core.rb', line 106

def wait_for_query(qstr, timeout=BRIAR_WAIT_TIMEOUT)
  msg = "waited for '#{timeout}' seconds but did not see\n '#{qstr}'"
  wait_for(:timeout => timeout,
           :retry_frequency => BRIAR_WAIT_RETRY_FREQ,
           :post_timeout => BRIAR_WAIT_STEP_PAUSE,
           :timeout_message => msg) do
    !query(qstr).empty?
  end
end

#wait_for_view(view_id, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/briar/briar_core.rb', line 96

def wait_for_view (view_id, timeout=BRIAR_WAIT_TIMEOUT)
  msg = "waited for '#{timeout}' seconds but did not see '#{view_id}'"
  wait_for(:timeout => timeout,
           :retry_frequency => BRIAR_WAIT_RETRY_FREQ,
           :post_timeout => BRIAR_WAIT_STEP_PAUSE,
           :timeout_message => msg) do
    view_exists? view_id
  end
end

#wait_for_view_to_disappear(view_id, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/briar/briar_core.rb', line 150

def wait_for_view_to_disappear(view_id, timeout=BRIAR_WAIT_TIMEOUT)
  msg = "waited for '#{timeout}' seconds for '#{view_id}' to disappear but it is still visible"
  options = {:timeout => timeout,
             :retry_frequency => BRIAR_WAIT_RETRY_FREQ,
             :post_timeout => BRIAR_WAIT_STEP_PAUSE,
             :timeout_message => msg}
  wait_for(options) do
    not view_exists? view_id
  end
end

#wait_for_views(views, timeout = BRIAR_WAIT_TIMEOUT) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/briar/briar_core.rb', line 139

def wait_for_views(views, timeout=BRIAR_WAIT_TIMEOUT)
  msg = "waited for '#{timeout}' seconds but did not see '#{views}'"
  options = {:timeout => timeout,
             :retry_frequency => BRIAR_WAIT_RETRY_FREQ,
             :post_timeout => BRIAR_WAIT_STEP_PAUSE,
             :timeout_message => msg}
  wait_for(options) do
    views.all? { |view_id| view_exists?(view_id) }
  end
end

#wait_opts(msg, timeout) ⇒ Object



89
90
91
92
93
94
# File 'lib/briar/briar_core.rb', line 89

def wait_opts(msg, timeout)
  {:timeout => timeout,
   :retry_frequency => BRIAR_WAIT_RETRY_FREQ,
   :post_timeout => BRIAR_WAIT_STEP_PAUSE,
   :timeout_message => msg}
end

#warn_pretty(msg) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/briar/briar_core.rb', line 241

def warn_pretty(msg)
  # todo warn_pretty does not output on a new line when called within cucumber
  msg = "WARN: #{msg}"
  begin
    puts "\033[34m#{msg}\033[0m"
  rescue
    puts "#{msg}"
  end
end