testcentricity 2.4.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +29 -0
  3. data/.rspec +2 -1
  4. data/.rubocop.yml +38 -0
  5. data/.ruby-version +1 -1
  6. data/.simplecov +9 -0
  7. data/.yardopts +3 -0
  8. data/CHANGELOG.md +275 -0
  9. data/CODE_OF_CONDUCT.md +13 -0
  10. data/{LICENSE.txt → LICENSE.md} +3 -4
  11. data/README.md +938 -1378
  12. data/Rakefile +63 -1
  13. data/config/cucumber.yml +145 -0
  14. data/config/locales/en-US.yml +56 -0
  15. data/config/test_data/LOCAL_data.yml +11 -0
  16. data/config/test_data/data.yml +10 -0
  17. data/features/deep_links.feature +26 -0
  18. data/features/login.feature +30 -0
  19. data/features/navigation.feature +31 -0
  20. data/features/step_definitions/generic_steps.rb +72 -0
  21. data/features/support/android/screens/about_screen.rb +11 -0
  22. data/features/support/android/screens/base_app_screen.rb +29 -0
  23. data/features/support/android/screens/checkout_address_screen.rb +17 -0
  24. data/features/support/android/screens/checkout_payment_screen.rb +22 -0
  25. data/features/support/android/screens/login_screen.rb +18 -0
  26. data/features/support/android/screens/products_screen.rb +13 -0
  27. data/features/support/android/screens/saucebot_screen.rb +16 -0
  28. data/features/support/android/screens/webview_screen.rb +13 -0
  29. data/features/support/android/sections/nav_widgets/nav_menu.rb +39 -0
  30. data/features/support/env.rb +61 -0
  31. data/features/support/hooks.rb +135 -0
  32. data/features/support/ios/screens/about_screen.rb +11 -0
  33. data/features/support/ios/screens/base_app_screen.rb +19 -0
  34. data/features/support/ios/screens/checkout_address_screen.rb +17 -0
  35. data/features/support/ios/screens/checkout_payment_screen.rb +22 -0
  36. data/features/support/ios/screens/login_screen.rb +18 -0
  37. data/features/support/ios/screens/products_screen.rb +13 -0
  38. data/features/support/ios/screens/saucebot_screen.rb +16 -0
  39. data/features/support/ios/screens/webview_screen.rb +13 -0
  40. data/features/support/ios/sections/list_items/product_cell_item.rb +13 -0
  41. data/features/support/ios/sections/modals/base_modal.rb +23 -0
  42. data/features/support/ios/sections/modals/logout_modal.rb +6 -0
  43. data/features/support/ios/sections/modals/reset_app_state_modal.rb +6 -0
  44. data/features/support/ios/sections/nav_widgets/nav_bar.rb +31 -0
  45. data/features/support/ios/sections/nav_widgets/nav_menu.rb +41 -0
  46. data/features/support/shared_components/screens/base_app_screen.rb +31 -0
  47. data/features/support/shared_components/screens/checkout_address_screen.rb +17 -0
  48. data/features/support/shared_components/screens/checkout_payment_screen.rb +22 -0
  49. data/features/support/shared_components/screens/login_screen.rb +39 -0
  50. data/features/support/shared_components/screens/saucebot_screen.rb +17 -0
  51. data/features/support/shared_components/screens/webview_screen.rb +12 -0
  52. data/features/support/shared_components/sections/nav_menu.rb +58 -0
  53. data/features/support/world_data.rb +12 -0
  54. data/features/support/world_pages.rb +26 -0
  55. data/lib/testcentricity/app_core/appium_connect_helper.rb +355 -111
  56. data/lib/testcentricity/app_core/screen_object.rb +252 -0
  57. data/lib/testcentricity/app_core/screen_objects_helper.rb +29 -280
  58. data/lib/testcentricity/app_core/screen_section.rb +350 -0
  59. data/lib/testcentricity/app_elements/app_element_helper.rb +17 -8
  60. data/lib/testcentricity/app_elements/checkbox.rb +3 -3
  61. data/lib/testcentricity/data_objects/environment.rb +133 -39
  62. data/lib/testcentricity/version.rb +1 -1
  63. data/lib/testcentricity.rb +4 -129
  64. data/reports/.keep +1 -0
  65. data/spec/fixtures/page_object.rb +22 -0
  66. data/spec/fixtures/page_section_object.rb +21 -0
  67. data/spec/fixtures/screen_object.rb +16 -0
  68. data/spec/fixtures/screen_section_object.rb +16 -0
  69. data/spec/spec_helper.rb +28 -9
  70. data/spec/testcentricity/elements/button_spec.rb +18 -0
  71. data/spec/testcentricity/elements/checkbox_spec.rb +28 -0
  72. data/spec/testcentricity/elements/image_spec.rb +13 -0
  73. data/spec/testcentricity/elements/label_spec.rb +18 -0
  74. data/spec/testcentricity/elements/list_spec.rb +13 -0
  75. data/spec/testcentricity/elements/ui_element_spec.rb +72 -0
  76. data/spec/testcentricity/mobile/appium_connect_spec.rb +117 -0
  77. data/spec/testcentricity/mobile/screen_object_spec.rb +63 -0
  78. data/spec/testcentricity/mobile/screen_section_object_spec.rb +56 -0
  79. data/spec/testcentricity/version_spec.rb +7 -0
  80. data/spec/testcentricity/web/browser_spec.rb +41 -0
  81. data/spec/testcentricity/web/local_webdriver_spec.rb +86 -0
  82. data/spec/testcentricity/web/mobile_webdriver_spec.rb +123 -0
  83. data/spec/testcentricity/web/page_object_spec.rb +85 -0
  84. data/spec/testcentricity/web/page_section_object_spec.rb +72 -0
  85. data/testcentricity.gemspec +28 -27
  86. metadata +196 -127
  87. data/.ruby-gemset +0 -1
  88. data/Gemfile.lock +0 -93
  89. data/bin/console +0 -14
  90. data/bin/setup +0 -8
  91. data/lib/devices/devices.yml +0 -352
  92. data/lib/testcentricity/app_core/appium_server.rb +0 -69
  93. data/lib/testcentricity/app_core/screen_sections_helper.rb +0 -491
  94. data/lib/testcentricity/browser_helper.rb +0 -174
  95. data/lib/testcentricity/data_objects/data_objects_helper.rb +0 -78
  96. data/lib/testcentricity/data_objects/excel_helper.rb +0 -242
  97. data/lib/testcentricity/exception_queue_helper.rb +0 -111
  98. data/lib/testcentricity/utility_helpers.rb +0 -28
  99. data/lib/testcentricity/web_core/drag_drop_helper.rb +0 -15
  100. data/lib/testcentricity/web_core/page_objects_helper.rb +0 -711
  101. data/lib/testcentricity/web_core/page_sections_helper.rb +0 -932
  102. data/lib/testcentricity/web_core/webdriver_helper.rb +0 -588
  103. data/lib/testcentricity/web_elements/button.rb +0 -8
  104. data/lib/testcentricity/web_elements/cell_button.rb +0 -8
  105. data/lib/testcentricity/web_elements/cell_checkbox.rb +0 -38
  106. data/lib/testcentricity/web_elements/cell_element.rb +0 -69
  107. data/lib/testcentricity/web_elements/cell_image.rb +0 -8
  108. data/lib/testcentricity/web_elements/cell_radio.rb +0 -31
  109. data/lib/testcentricity/web_elements/checkbox.rb +0 -100
  110. data/lib/testcentricity/web_elements/file_field.rb +0 -45
  111. data/lib/testcentricity/web_elements/image.rb +0 -34
  112. data/lib/testcentricity/web_elements/label.rb +0 -8
  113. data/lib/testcentricity/web_elements/link.rb +0 -8
  114. data/lib/testcentricity/web_elements/list.rb +0 -100
  115. data/lib/testcentricity/web_elements/list_button.rb +0 -8
  116. data/lib/testcentricity/web_elements/list_checkbox.rb +0 -38
  117. data/lib/testcentricity/web_elements/list_element.rb +0 -61
  118. data/lib/testcentricity/web_elements/list_radio.rb +0 -31
  119. data/lib/testcentricity/web_elements/radio.rb +0 -74
  120. data/lib/testcentricity/web_elements/select_list.rb +0 -208
  121. data/lib/testcentricity/web_elements/siebel_open_ui_helper.rb +0 -15
  122. data/lib/testcentricity/web_elements/table.rb +0 -612
  123. data/lib/testcentricity/web_elements/textfield.rb +0 -114
  124. data/lib/testcentricity/web_elements/ui_elements_helper.rb +0 -532
  125. data/lib/testcentricity/world_extensions.rb +0 -26
  126. data/my_templates/default/method_details/setup.rb +0 -3
  127. data/spec/testcentricity_spec.rb +0 -9
@@ -1,491 +0,0 @@
1
- require 'test/unit'
2
-
3
- module TestCentricity
4
- class ScreenSection
5
- include Test::Unit::Assertions
6
-
7
- attr_reader :context, :name
8
- attr_accessor :locator
9
- attr_accessor :parent
10
- attr_accessor :parent_list
11
- attr_accessor :list_index
12
-
13
- def initialize(name, parent, locator, context)
14
- @name = name
15
- @parent = parent
16
- @locator = locator
17
- @context = context
18
- @parent_list = nil
19
- @list_index = nil
20
- end
21
-
22
- def get_locator
23
- if @locator.zero? && defined?(section_locator)
24
- my_locator = section_locator
25
- else
26
- my_locator = @locator
27
- end
28
- locators = []
29
- if @context == :section && !@parent.nil?
30
- locators.push(@parent.get_locator)
31
- end
32
-
33
- if @parent_list.nil?
34
- locators.push(my_locator)
35
- else
36
- locators.push(@parent_list.get_locator)
37
- if @list_index.nil?
38
- locators.push(my_locator)
39
- else
40
- list_key = my_locator.keys[0]
41
- list_value = "#{my_locator.values[0]}[#{@list_index}]"
42
- locators.push( { list_key => list_value } )
43
- end
44
- end
45
- locators
46
- end
47
-
48
- def set_list_index(list, index = 1)
49
- @parent_list = list unless list.nil?
50
- @list_index = index
51
- end
52
-
53
- def get_item_count
54
- raise 'No parent list defined' if @parent_list.nil?
55
- @parent_list.get_item_count
56
- end
57
-
58
- def get_list_items
59
- items = []
60
- (1..get_item_count).each do |item|
61
- set_list_index(nil, item)
62
- items.push(get_value)
63
- end
64
- items
65
- end
66
-
67
- def get_object_type
68
- :section
69
- end
70
-
71
- def get_name
72
- @name
73
- end
74
-
75
- def set_parent(parent)
76
- @parent = parent
77
- end
78
-
79
- def self.trait(trait_name, &block)
80
- define_method(trait_name.to_s, &block)
81
- end
82
-
83
- # def self.element(element_name, locator)
84
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppUIElement.new("#{element_name}", self, #{locator}, :section);end))
85
- # end
86
-
87
- def self.element(element_name, locator)
88
- define_method(element_name) do
89
- ivar_name = "@#{element_name}"
90
- ivar = instance_variable_get(ivar_name)
91
- return ivar if ivar
92
- instance_variable_set(ivar_name, TestCentricity::AppUIElement.new(element_name, self, locator, :section))
93
- end
94
- end
95
-
96
- def self.elements(element_hash)
97
- element_hash.each do |element_name, locator|
98
- element(element_name, locator)
99
- end
100
- end
101
-
102
- # def self.button(element_name, locator)
103
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppButton.new("#{element_name}", self, #{locator}, :section);end))
104
- # end
105
-
106
- def self.button(element_name, locator)
107
- define_method(element_name) do
108
- ivar_name = "@#{element_name}"
109
- ivar = instance_variable_get(ivar_name)
110
- return ivar if ivar
111
- instance_variable_set(ivar_name, TestCentricity::AppButton.new(element_name, self, locator, :section))
112
- end
113
- end
114
-
115
- def self.buttons(element_hash)
116
- element_hash.each do |element_name, locator|
117
- button(element_name, locator)
118
- end
119
- end
120
-
121
- # def self.textfield(element_name, locator)
122
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppTextField.new("#{element_name}", self, #{locator}, :section);end))
123
- # end
124
-
125
- def self.textfield(element_name, locator)
126
- define_method(element_name) do
127
- ivar_name = "@#{element_name}"
128
- ivar = instance_variable_get(ivar_name)
129
- return ivar if ivar
130
- instance_variable_set(ivar_name, TestCentricity::AppTextField.new(element_name, self, locator, :section))
131
- end
132
- end
133
-
134
- def self.textfields(element_hash)
135
- element_hash.each do |element_name, locator|
136
- textfield(element_name, locator)
137
- end
138
- end
139
-
140
- # def self.switch(element_name, locator)
141
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppSwitch.new("#{element_name}", self, #{locator}, :section);end))
142
- # end
143
-
144
- def self.switch(element_name, locator)
145
- define_method(element_name) do
146
- ivar_name = "@#{element_name}"
147
- ivar = instance_variable_get(ivar_name)
148
- return ivar if ivar
149
- instance_variable_set(ivar_name, TestCentricity::AppSwitch.new(element_name, self, locator, :section))
150
- end
151
- end
152
-
153
- def self.switches(element_hash)
154
- element_hash.each do |element_name, locator|
155
- switch(element_name, locator)
156
- end
157
- end
158
-
159
- # def self.checkbox(element_name, locator)
160
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppCheckBox.new("#{element_name}", self, #{locator}, :section);end))
161
- # end
162
-
163
- def self.checkbox(element_name, locator)
164
- define_method(element_name) do
165
- ivar_name = "@#{element_name}"
166
- ivar = instance_variable_get(ivar_name)
167
- return ivar if ivar
168
- instance_variable_set(ivar_name, TestCentricity::AppCheckBox.new(element_name, self, locator, :section))
169
- end
170
- end
171
-
172
- def self.checkboxes(element_hash)
173
- element_hash.each do |element_name, locator|
174
- checkbox(element_name, locator)
175
- end
176
- end
177
-
178
- # def self.label(element_name, locator)
179
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppLabel.new("#{element_name}", self, #{locator}, :section);end))
180
- # end
181
-
182
- def self.label(element_name, locator)
183
- define_method(element_name) do
184
- ivar_name = "@#{element_name}"
185
- ivar = instance_variable_get(ivar_name)
186
- return ivar if ivar
187
- instance_variable_set(ivar_name, TestCentricity::AppLabel.new(element_name, self, locator, :section))
188
- end
189
- end
190
-
191
- def self.labels(element_hash)
192
- element_hash.each do |element_name, locator|
193
- label(element_name, locator)
194
- end
195
- end
196
-
197
- # def self.list(element_name, locator)
198
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppList.new("#{element_name}", self, #{locator}, :section);end))
199
- # end
200
-
201
- def self.list(element_name, locator)
202
- define_method(element_name) do
203
- ivar_name = "@#{element_name}"
204
- ivar = instance_variable_get(ivar_name)
205
- return ivar if ivar
206
- instance_variable_set(ivar_name, TestCentricity::AppList.new(element_name, self, locator, :section))
207
- end
208
- end
209
-
210
- def self.lists(element_hash)
211
- element_hash.each do |element_name, locator|
212
- list(element_name, locator)
213
- end
214
- end
215
-
216
- # def self.selectlist(element_name, locator)
217
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppSelectList.new("#{element_name}", self, #{locator}, :section);end))
218
- # end
219
-
220
- def self.selectlist(element_name, locator)
221
- define_method(element_name) do
222
- ivar_name = "@#{element_name}"
223
- ivar = instance_variable_get(ivar_name)
224
- return ivar if ivar
225
- instance_variable_set(ivar_name, TestCentricity::AppSelectList.new(element_name, self, locator, :section))
226
- end
227
- end
228
-
229
- def self.selectlists(element_hash)
230
- element_hash.each do |element_name, locator|
231
- selectlist(element_name, locator)
232
- end
233
- end
234
-
235
- # def self.image(element_name, locator)
236
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppImage.new("#{element_name}", self, #{locator}, :section);end))
237
- # end
238
-
239
- def self.image(element_name, locator)
240
- define_method(element_name) do
241
- ivar_name = "@#{element_name}"
242
- ivar = instance_variable_get(ivar_name)
243
- return ivar if ivar
244
- instance_variable_set(ivar_name, TestCentricity::AppImage.new(element_name, self, locator, :section))
245
- end
246
- end
247
-
248
- def self.images(element_hash)
249
- element_hash.each do |element_name, locator|
250
- image(element_name, locator)
251
- end
252
- end
253
-
254
- # def self.alert(element_name, locator)
255
- # class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppAlert.new("#{element_name}", self, #{locator}, :page);end))
256
- # end
257
-
258
- def self.alert(element_name, locator)
259
- define_method(element_name) do
260
- ivar_name = "@#{element_name}"
261
- ivar = instance_variable_get(ivar_name)
262
- return ivar if ivar
263
- instance_variable_set(ivar_name, TestCentricity::AppAlert.new(element_name, self, locator, :page))
264
- end
265
- end
266
-
267
- def self.section(section_name, class_name, locator = 0)
268
- class_eval(%(def #{section_name};@#{section_name} ||= #{class_name}.new("#{section_name}", self, #{locator}, :section);end))
269
- end
270
-
271
- def self.sections(section_hash)
272
- section_hash.each do |section_name, class_name|
273
- section(section_name, class_name)
274
- end
275
- end
276
-
277
-
278
- def click
279
- section = find_section
280
- section_not_found_exception(section)
281
- section.click
282
- end
283
-
284
- def tap
285
- section = find_section
286
- section_not_found_exception(section)
287
- x = section.location.x
288
- y = section.location.y
289
- tap_action = Appium::TouchAction.new.tap(element: section, x: x, y: y)
290
- tap_action.perform
291
- end
292
-
293
- def double_tap
294
- section = find_section
295
- section_not_found_exception(section)
296
- x = section.location.x
297
- y = section.location.y
298
- tap_action = Appium::TouchAction.new.double_tap(element: section, x: x, y: y)
299
- tap_action.perform
300
- end
301
-
302
- def exists?
303
- section = find_section
304
- section != nil
305
- end
306
-
307
- def enabled?
308
- section = find_section
309
- section_not_found_exception(section)
310
- section.enabled?
311
- end
312
-
313
- def disabled?
314
- section = find_section
315
- section_not_found_exception(section)
316
- section.enabled?
317
- end
318
-
319
- def visible?
320
- if exists?
321
- section.displayed?
322
- else
323
- false
324
- end
325
- end
326
-
327
- def hidden?
328
- !visible?
329
- end
330
-
331
- def wait_until_exists(seconds = nil)
332
- timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
333
- wait = Selenium::WebDriver::Wait.new(timeout: timeout)
334
- wait.until { exists? }
335
- rescue
336
- raise "Could not find Section object '#{get_name}' (#{get_locator}) after #{timeout} seconds" unless exists?
337
- end
338
-
339
- def wait_until_gone(seconds = nil)
340
- timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
341
- wait = Selenium::WebDriver::Wait.new(timeout: timeout)
342
- wait.until { !exists? }
343
- rescue
344
- raise "Section object '#{get_name}' (#{get_locator}) remained visible after #{timeout} seconds" if exists?
345
- end
346
-
347
- def wait_until_visible(seconds = nil)
348
- timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
349
- wait = Selenium::WebDriver::Wait.new(timeout: timeout)
350
- wait.until { visible? }
351
- rescue
352
- raise "Could not find Section object '#{get_name}' (#{get_locator}) after #{timeout} seconds" unless visible?
353
- end
354
-
355
- def wait_until_hidden(seconds = nil)
356
- timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
357
- wait = Selenium::WebDriver::Wait.new(timeout: timeout)
358
- wait.until { hidden? }
359
- rescue
360
- raise "Section object '#{get_name}' (#{get_locator}) remained visible after #{timeout} seconds" if visible?
361
- end
362
-
363
-
364
- def width
365
- section = find_section
366
- section_not_found_exception(section)
367
- section.size.width
368
- end
369
-
370
- def height
371
- section = find_section
372
- section_not_found_exception(section)
373
- section.size.height
374
- end
375
-
376
- def x_loc
377
- section = find_section
378
- section_not_found_exception(section)
379
- section.location.x
380
- end
381
-
382
- def y_loc
383
- section = find_section
384
- section_not_found_exception(section)
385
- section.location.y
386
- end
387
-
388
-
389
- def verify_ui_states(ui_states)
390
- ui_states.each do |ui_object, object_states|
391
- object_states.each do |property, state|
392
-
393
- puts "#{ui_object.get_name} - #{property} = #{state}" if ENV['DEBUG']
394
-
395
- case property
396
- when :class
397
- actual = ui_object.get_attribute(:class)
398
- when :exists
399
- actual = ui_object.exists?
400
- when :enabled
401
- actual = ui_object.enabled?
402
- when :disabled
403
- actual = ui_object.disabled?
404
- when :visible
405
- actual = ui_object.visible?
406
- when :hidden
407
- actual = ui_object.hidden?
408
- when :checked
409
- actual = ui_object.checked?
410
- when :selected
411
- actual = ui_object.selected?
412
- when :value
413
- actual = ui_object.get_value
414
- when :caption
415
- actual = ui_object.get_caption
416
- when :name
417
- actual = ui_object.tag_name
418
- when :placeholder
419
- actual = ui_object.get_placeholder
420
- when :readonly
421
- actual = ui_object.read_only?
422
- when :maxlength
423
- actual = ui_object.get_max_length
424
- when :items
425
- actual = ui_object.get_list_items
426
- when :itemcount
427
- actual = ui_object.get_item_count
428
- when :width
429
- actual = ui_object.width
430
- when :height
431
- actual = ui_object.height
432
- when :x
433
- actual = ui_object.x_loc
434
- when :y
435
- actual = ui_object.y_loc
436
- end
437
- error_msg = "Expected UI object '#{ui_object.get_name}' (#{ui_object.get_locator}) #{property} property to"
438
- ExceptionQueue.enqueue_comparison(state, actual, error_msg)
439
- end
440
- end
441
- rescue ObjectNotFoundError => e
442
- ExceptionQueue.enqueue_exception(e.message)
443
- ensure
444
- ExceptionQueue.post_exceptions
445
- end
446
-
447
- def populate_data_fields(data, wait_time = nil)
448
- timeout = wait_time.nil? ? 5 : wait_time
449
- data.each do |data_field, data_param|
450
- unless data_param.blank?
451
- # make sure the intended UI target element is visible before trying to set its value
452
- data_field.wait_until_visible(timeout)
453
- if data_param == '!DELETE'
454
- data_field.clear
455
- else
456
- case data_field.get_object_type
457
- when :checkbox
458
- data_field.set_checkbox_state(data_param.to_bool)
459
- when :textfield
460
- data_field.clear
461
- data_field.set("#{data_param}\t")
462
- end
463
- end
464
- end
465
- end
466
- end
467
-
468
- private
469
-
470
- def find_section
471
- obj = nil
472
- locators = get_locator
473
- locators.each do |loc|
474
- if obj.nil?
475
- obj = find_element(loc.keys[0], loc.values[0])
476
- puts "Found object #{loc}" if ENV['DEBUG']
477
- else
478
- obj = obj.find_element(loc.keys[0], loc.values[0])
479
- puts "Found object #{loc}" if ENV['DEBUG']
480
- end
481
- end
482
- obj
483
- rescue
484
- nil
485
- end
486
-
487
- def section_not_found_exception(obj)
488
- raise ObjectNotFoundError.new("Section object '#{get_name}' (#{get_locator}) not found") unless obj
489
- end
490
- end
491
- end
@@ -1,174 +0,0 @@
1
- require 'yaml'
2
-
3
-
4
- module TestCentricity
5
- module Browsers
6
- include Capybara::DSL
7
-
8
- # Sets the size of the browser window.
9
- #
10
- # @param resolution [Array] the desired [width, height]
11
- # @example
12
- # Browsers.set_browser_window_size([1024, 768])
13
- #
14
- def self.set_browser_window_size(resolution)
15
- resolution = resolution.split(',') if resolution.is_a?(String)
16
- window = Capybara.current_session.driver.browser.manage.window
17
- window.resize_to(resolution[0], resolution[1])
18
- Environ.browser_size = [resolution[0].to_i, resolution[1].to_i]
19
- end
20
-
21
- # Maximizes the selenium browser window.
22
- #
23
- # @example
24
- # Browsers.maximize_browser
25
- #
26
- def self.maximize_browser
27
- window = Capybara.current_session.driver.browser.manage.window
28
- window.maximize
29
- end
30
- # Sets the position of the browser window.
31
- #
32
- # @param x [Integer] the desired x coordinate
33
- # @param y [Integer] the desired y coordinate
34
- # @example
35
- # Browsers.set_browser_window_position([100, 300])
36
- #
37
- def self.set_browser_window_position(x, y)
38
- window = Capybara.current_session.driver.browser.manage.window
39
- window.move_to(x, y)
40
- end
41
-
42
- # Refreshes the selenium browser.
43
- #
44
- # @example
45
- # Browsers.refresh_browser
46
- #
47
- def self.refresh_browser
48
- Capybara.page.driver.browser.navigate.refresh
49
- end
50
-
51
- # Emulates clicking the web browser's Back button. Navigates back by one page on the browser’s history.
52
- #
53
- # @example
54
- # Browsers.navigate_back
55
- #
56
- def self.navigate_back
57
- Capybara.page.driver.browser.navigate.back
58
- end
59
-
60
- # Emulates clicking the web browser's Forward button. Navigates forward by one page on the browser’s history.
61
- #
62
- # @example
63
- # Browsers.navigate_forward
64
- #
65
- def self.navigate_forward
66
- Capybara.page.driver.browser.navigate.forward
67
- end
68
-
69
- def self.switch_to_new_browser_instance
70
- Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.last)
71
- end
72
-
73
- def self.close_all_browser_instances
74
- Capybara.page.driver.browser.window_handles.each do |handle|
75
- page.driver.browser.switch_to.window(handle)
76
- page.driver.browser.close
77
- end
78
- end
79
-
80
- def self.num_browser_instances
81
- Capybara.page.driver.browser.window_handles.count
82
- end
83
-
84
- def self.close_current_browser_instance
85
- Capybara.page.driver.browser.close
86
- Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.last)
87
- end
88
-
89
- def self.close_old_browser_instance
90
- Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.first)
91
- Capybara.page.driver.browser.close
92
- Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.last)
93
- end
94
-
95
- def self.close_named_browser_instance(name)
96
- Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.find_window(name))
97
- Capybara.page.driver.browser.close
98
- Capybara.page.driver.browser.switch_to.window(Capybara.page.driver.browser.window_handles.last)
99
- end
100
-
101
- def self.suppress_js_alerts
102
- Capybara.page.execute_script('window.alert = function() {}')
103
- end
104
-
105
- def self.suppress_js_leave_page_modal
106
- Capybara.page.execute_script('window.onbeforeunload = null')
107
- end
108
-
109
- def self.delete_all_cookies
110
- if Capybara.current_driver == :selenium
111
- browser = Capybara.current_session.driver.browser
112
- if browser.respond_to?(:manage) && browser.manage.respond_to?(:delete_all_cookies)
113
- browser.manage.delete_all_cookies
114
- else
115
- raise 'Could not clear cookies.'
116
- end
117
- end
118
- end
119
-
120
- def self.set_device_orientation(orientation)
121
- if Environ.is_mobile? && !Environ.is_device?
122
- Browsers.set_browser_window_size(Browsers.browser_size(Environ.browser.to_s, orientation))
123
- else
124
- puts '***** Cannot change device orientation of desktop web browsers or remote devices *****'
125
- end
126
- end
127
-
128
- def self.mobile_device_agent(device)
129
- device_name = device.gsub(/\s+/, '').downcase.to_sym
130
- device = get_devices[device_name]
131
- agent_string = device[:user_agent]
132
- raise "Device '#{device}' is not defined" unless agent_string
133
- agent_string
134
- end
135
-
136
- def self.mobile_device_name(device)
137
- device_name = device.gsub(/\s+/, '').downcase.to_sym
138
- device = get_devices[device_name]
139
- name = device[:name]
140
- raise "Device '#{device}' is not defined" unless name
141
- Environ.device_os = device[:os]
142
- Environ.device_type = device[:type]
143
- name
144
- end
145
-
146
- def self.browser_size(browser, orientation)
147
- device_name = browser.gsub(/\s+/, '').downcase.to_sym
148
- device = get_devices[device_name]
149
- if device
150
- width = device[:css_width]
151
- height = device[:css_height]
152
- default_orientation = device[:default_orientation].to_sym
153
- if orientation
154
- Environ.device_orientation = orientation
155
- orientation.downcase.to_sym == default_orientation ?
156
- size = [width, height] :
157
- size = [height, width]
158
- else
159
- Environ.device_orientation = device[:default_orientation]
160
- size = [width, height]
161
- end
162
- else
163
- size = [1650, 1000]
164
- end
165
- size
166
- end
167
-
168
- private
169
-
170
- def self.get_devices
171
- YAML.load_file File.expand_path('../../devices/devices.yml', __FILE__)
172
- end
173
- end
174
- end