automation_object 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. checksums.yaml +4 -4
  2. data/lib/automation_object.rb +22 -11
  3. data/lib/automation_object/blue_print.rb +13 -12
  4. data/lib/automation_object/blue_print/composite/automatic_onload_modal.rb +9 -8
  5. data/lib/automation_object/blue_print/composite/base.rb +5 -4
  6. data/lib/automation_object/blue_print/composite/custom_method.rb +8 -7
  7. data/lib/automation_object/blue_print/composite/element.rb +6 -5
  8. data/lib/automation_object/blue_print/composite/element_array.rb +6 -5
  9. data/lib/automation_object/blue_print/composite/element_hash.rb +7 -6
  10. data/lib/automation_object/blue_print/composite/helpers/element_helper.rb +7 -7
  11. data/lib/automation_object/blue_print/composite/helpers/multiple_elements_helper.rb +5 -4
  12. data/lib/automation_object/blue_print/composite/hook.rb +9 -11
  13. data/lib/automation_object/blue_print/composite/hook_action.rb +25 -24
  14. data/lib/automation_object/blue_print/composite/hook_element_requirements.rb +14 -13
  15. data/lib/automation_object/blue_print/composite/modal.rb +11 -21
  16. data/lib/automation_object/blue_print/composite/screen.rb +22 -16
  17. data/lib/automation_object/blue_print/composite/top.rb +13 -12
  18. data/lib/automation_object/blue_print/composite/view.rb +27 -16
  19. data/lib/automation_object/blue_print/hash_adapter.rb +15 -11
  20. data/lib/automation_object/blue_print/hash_adapter/automatic_onload_modal.rb +12 -11
  21. data/lib/automation_object/blue_print/hash_adapter/composite.rb +28 -25
  22. data/lib/automation_object/blue_print/hash_adapter/custom_method.rb +11 -10
  23. data/lib/automation_object/blue_print/hash_adapter/element.rb +10 -9
  24. data/lib/automation_object/blue_print/hash_adapter/element_array.rb +10 -9
  25. data/lib/automation_object/blue_print/hash_adapter/element_hash.rb +11 -10
  26. data/lib/automation_object/blue_print/hash_adapter/helpers/element_helper.rb +20 -25
  27. data/lib/automation_object/blue_print/hash_adapter/helpers/multiple_elements_helper.rb +15 -14
  28. data/lib/automation_object/blue_print/hash_adapter/helpers/validation_error.rb +9 -8
  29. data/lib/automation_object/blue_print/hash_adapter/helpers/validation_helper.rb +19 -24
  30. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate.rb +8 -5
  31. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_allowed_keys.rb +7 -8
  32. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_element_presence_of.rb +28 -23
  33. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_instance_of.rb +12 -13
  34. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_modal_presence_of.rb +27 -23
  35. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_presence_of.rb +9 -8
  36. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_screen_presence_of.rb +27 -23
  37. data/lib/automation_object/blue_print/hash_adapter/helpers/validators/validate_view_presence_of.rb +25 -21
  38. data/lib/automation_object/blue_print/hash_adapter/hook.rb +14 -11
  39. data/lib/automation_object/blue_print/hash_adapter/hook_action.rb +45 -47
  40. data/lib/automation_object/blue_print/hash_adapter/hook_element_requirements.rb +24 -28
  41. data/lib/automation_object/blue_print/hash_adapter/modal.rb +14 -13
  42. data/lib/automation_object/blue_print/hash_adapter/screen.rb +23 -22
  43. data/lib/automation_object/blue_print/hash_adapter/top.rb +16 -15
  44. data/lib/automation_object/blue_print/hash_adapter/view.rb +58 -4
  45. data/lib/automation_object/blue_print/yaml_adapter.rb +33 -34
  46. data/lib/automation_object/driver.rb +15 -13
  47. data/lib/automation_object/driver/appium_adapter/driver.rb +47 -46
  48. data/lib/automation_object/driver/appium_adapter/element.rb +29 -96
  49. data/lib/automation_object/driver/base.rb +4 -2
  50. data/lib/automation_object/driver/common_selenium/driver.rb +60 -0
  51. data/lib/automation_object/driver/common_selenium/element.rb +107 -0
  52. data/lib/automation_object/driver/common_selenium/element_geometry.rb +86 -0
  53. data/lib/automation_object/driver/driver.rb +60 -30
  54. data/lib/automation_object/driver/element.rb +70 -15
  55. data/lib/automation_object/driver/nokogiri_adapter/driver.rb +88 -34
  56. data/lib/automation_object/driver/nokogiri_adapter/element.rb +94 -16
  57. data/lib/automation_object/driver/nokogiri_adapter/error.rb +29 -8
  58. data/lib/automation_object/driver/nokogiri_adapter/{backup/form.rb → form.rb} +19 -6
  59. data/lib/automation_object/driver/nokogiri_adapter/request.rb +17 -0
  60. data/lib/automation_object/driver/nokogiri_adapter/session.rb +94 -0
  61. data/lib/automation_object/driver/nokogiri_adapter/window.rb +86 -0
  62. data/lib/automation_object/driver/selenium_adapter/driver.rb +36 -45
  63. data/lib/automation_object/driver/selenium_adapter/element.rb +15 -85
  64. data/lib/automation_object/dsl.rb +11 -28
  65. data/lib/automation_object/dsl/_base.rb +70 -0
  66. data/lib/automation_object/dsl/_proxy.rb +24 -0
  67. data/lib/automation_object/dsl/element.rb +34 -0
  68. data/lib/automation_object/dsl/element_array.rb +34 -0
  69. data/lib/automation_object/dsl/element_hash.rb +34 -0
  70. data/lib/automation_object/dsl/modal.rb +43 -0
  71. data/lib/automation_object/dsl/screen.rb +45 -0
  72. data/lib/automation_object/dsl/top.rb +25 -0
  73. data/lib/automation_object/framework.rb +28 -38
  74. data/lib/automation_object/helpers/composite.rb +30 -27
  75. data/lib/automation_object/helpers/composite_hook.rb +16 -15
  76. data/lib/automation_object/{blue_print/helpers/file_helper.rb → helpers/file.rb} +12 -12
  77. data/lib/automation_object/helpers/hash.rb +6 -5
  78. data/lib/automation_object/helpers/kernel.rb +11 -0
  79. data/lib/automation_object/helpers/{reflection_helper.rb → reflection.rb} +9 -8
  80. data/lib/automation_object/helpers/string.rb +25 -15
  81. data/lib/automation_object/{proxies → proxy}/mutex_proxy.rb +18 -21
  82. data/lib/automation_object/proxy/proxy.rb +23 -0
  83. data/lib/automation_object/proxy/throttle_proxy.rb +52 -0
  84. data/lib/automation_object/state.rb +11 -29
  85. data/lib/automation_object/state/{blue_print_adapter/composite.rb → composite/_base.rb} +35 -25
  86. data/lib/automation_object/state/{blue_print_adapter/helpers/element_helper.rb → composite/_common_element.rb} +11 -11
  87. data/lib/automation_object/state/composite/element.rb +22 -0
  88. data/lib/automation_object/state/composite/element_array.rb +27 -0
  89. data/lib/automation_object/state/composite/element_hash.rb +33 -0
  90. data/lib/automation_object/state/composite/element_proxy.rb +33 -0
  91. data/lib/automation_object/state/composite/helpers/window.rb +61 -0
  92. data/lib/automation_object/state/composite/helpers/window_manager.rb +60 -0
  93. data/lib/automation_object/state/composite/hook.rb +56 -0
  94. data/lib/automation_object/state/{blue_print_adapter → composite}/hook_action.rb +10 -8
  95. data/lib/automation_object/state/composite/hook_actions/action_loop.rb +36 -0
  96. data/lib/automation_object/state/{blue_print_adapter → composite}/hook_actions/change_screen.rb +7 -5
  97. data/lib/automation_object/state/{blue_print_adapter → composite}/hook_actions/change_to_previous_screen.rb +7 -5
  98. data/lib/automation_object/state/composite/hook_actions/close_modal.rb +29 -0
  99. data/lib/automation_object/state/composite/hook_actions/close_screen.rb +19 -0
  100. data/lib/automation_object/state/composite/hook_actions/element_requirement.rb +32 -0
  101. data/lib/automation_object/state/composite/hook_actions/new_screen.rb +35 -0
  102. data/lib/automation_object/state/{blue_print_adapter → composite}/hook_actions/possible_screen_changes.rb +8 -7
  103. data/lib/automation_object/state/composite/hook_actions/reset_screen.rb +14 -0
  104. data/lib/automation_object/state/{blue_print_adapter → composite}/hook_actions/show_modal.rb +7 -5
  105. data/lib/automation_object/state/{blue_print_adapter → composite}/hook_actions/sleep.rb +4 -2
  106. data/lib/automation_object/state/{blue_print_adapter → composite}/hook_actions/wait_for_elements.rb +7 -5
  107. data/lib/automation_object/state/composite/modal.rb +45 -0
  108. data/lib/automation_object/state/composite/screen.rb +58 -0
  109. data/lib/automation_object/state/composite/top.rb +51 -0
  110. data/lib/automation_object/state/error.rb +45 -6
  111. data/lib/automation_object/state/session.rb +25 -23
  112. data/lib/automation_object/step_definitions.rb +5 -3
  113. data/lib/automation_object/step_definitions/element.rb +77 -0
  114. data/lib/automation_object/step_definitions/element_array.rb +135 -0
  115. data/lib/automation_object/step_definitions/element_hash.rb +126 -0
  116. data/lib/automation_object/step_definitions/screen.rb +62 -0
  117. data/lib/automation_object/step_definitions/{cache.rb → support/cache.rb} +8 -6
  118. data/lib/automation_object/step_definitions/support/element_array.rb +40 -0
  119. data/lib/automation_object/step_definitions/support/element_hash.rb +41 -0
  120. data/lib/automation_object/step_definitions/{parse.rb → support/parse.rb} +8 -6
  121. data/lib/automation_object/version.rb +2 -1
  122. metadata +203 -103
  123. data/lib/automation_object/driver/helpers/selenium_driver_helper.rb +0 -41
  124. data/lib/automation_object/driver/helpers/selenium_element_helper.rb +0 -138
  125. data/lib/automation_object/driver/nokogiri_adapter/backup/driver.rb +0 -83
  126. data/lib/automation_object/driver/nokogiri_adapter/backup/element.rb +0 -66
  127. data/lib/automation_object/driver/nokogiri_adapter/backup/helpers/driver_element_helper.rb +0 -52
  128. data/lib/automation_object/driver/nokogiri_adapter/backup/session.rb +0 -115
  129. data/lib/automation_object/dsl/blue_print_adapter.rb +0 -16
  130. data/lib/automation_object/dsl/blue_print_adapter/composite.rb +0 -54
  131. data/lib/automation_object/dsl/blue_print_adapter/element.rb +0 -14
  132. data/lib/automation_object/dsl/blue_print_adapter/element_array.rb +0 -14
  133. data/lib/automation_object/dsl/blue_print_adapter/element_hash.rb +0 -14
  134. data/lib/automation_object/dsl/blue_print_adapter/modal.rb +0 -23
  135. data/lib/automation_object/dsl/blue_print_adapter/screen.rb +0 -26
  136. data/lib/automation_object/dsl/blue_print_adapter/top.rb +0 -19
  137. data/lib/automation_object/dsl/models.rb +0 -18
  138. data/lib/automation_object/dsl/proxies/element.rb +0 -16
  139. data/lib/automation_object/dsl/proxies/element_array.rb +0 -16
  140. data/lib/automation_object/dsl/proxies/element_hash.rb +0 -16
  141. data/lib/automation_object/dsl/proxies/helpers/composite_helper.rb +0 -74
  142. data/lib/automation_object/dsl/proxies/modal.rb +0 -17
  143. data/lib/automation_object/dsl/proxies/screen.rb +0 -17
  144. data/lib/automation_object/dsl/proxies/top.rb +0 -17
  145. data/lib/automation_object/proxies/proxy.rb +0 -20
  146. data/lib/automation_object/proxies/throttle_proxy.rb +0 -57
  147. data/lib/automation_object/state/blue_print_adapter.rb +0 -16
  148. data/lib/automation_object/state/blue_print_adapter/element.rb +0 -20
  149. data/lib/automation_object/state/blue_print_adapter/element_array.rb +0 -25
  150. data/lib/automation_object/state/blue_print_adapter/element_hash.rb +0 -31
  151. data/lib/automation_object/state/blue_print_adapter/element_proxy.rb +0 -28
  152. data/lib/automation_object/state/blue_print_adapter/helpers/screen_manager.rb +0 -137
  153. data/lib/automation_object/state/blue_print_adapter/helpers/window.rb +0 -39
  154. data/lib/automation_object/state/blue_print_adapter/hook.rb +0 -56
  155. data/lib/automation_object/state/blue_print_adapter/hook_actions/action_loop.rb +0 -35
  156. data/lib/automation_object/state/blue_print_adapter/hook_actions/close_modal.rb +0 -31
  157. data/lib/automation_object/state/blue_print_adapter/hook_actions/close_screen.rb +0 -19
  158. data/lib/automation_object/state/blue_print_adapter/hook_actions/element_requirement.rb +0 -34
  159. data/lib/automation_object/state/blue_print_adapter/hook_actions/new_screen.rb +0 -37
  160. data/lib/automation_object/state/blue_print_adapter/hook_actions/reset_screen.rb +0 -18
  161. data/lib/automation_object/state/blue_print_adapter/modal.rb +0 -26
  162. data/lib/automation_object/state/blue_print_adapter/screen.rb +0 -32
  163. data/lib/automation_object/state/blue_print_adapter/top.rb +0 -45
  164. data/lib/automation_object/step_definitions/action.rb +0 -21
  165. data/lib/automation_object/step_definitions/element/actions.rb +0 -87
  166. data/lib/automation_object/step_definitions/element/regex.rb +0 -24
  167. data/lib/automation_object/step_definitions/element/steps.rb +0 -67
@@ -1,80 +1,17 @@
1
- require_relative '../helpers/selenium_element_helper'
1
+ # frozen_string_literal: true
2
+ require_relative '../common_selenium/element'
2
3
 
3
4
  module AutomationObject
4
5
  module Driver
5
6
  module AppiumAdapter
6
- #Element proxy for Appium
7
- #Conform Appium element interface to what's expected of the Driver Port
8
- class Element < AutomationObject::Proxies::Proxy
9
- include AutomationObject::Driver::SeleniumElementHelper
7
+ # Element proxy for Appium
8
+ # Conform Appium element interface to what's expected of the Driver Port
9
+ class Element < AutomationObject::Proxy::Proxy
10
+ include AutomationObject::Driver::CommonSelenium::Element
10
11
 
11
- def initialize(args)
12
- @driver = args.fetch :driver
13
- @subject = args.fetch :element
14
- end
15
-
16
- # Set or Get attribute
17
- # @param key [String] key of element
18
- # @param value [String, nil] set value or leave blank
19
- # @return [String, nil]
20
- def attribute(key, value = nil)
21
- @subject.attribute(key, value)
22
- end
23
-
24
- # @return [String] id of element
25
- def id
26
- @subject.id
27
- end
28
-
29
- # @return [String] href of element
30
- def href
31
- @subject.attribute('href')
32
- end
33
-
34
- # Text of element
35
- # @return [String, nil]
36
- def text
37
- @subject.text
38
- end
39
-
40
- # Type into an element
41
- # @return [void]
42
- def send_keys(string)
43
- @subject.send_keys(string)
44
- end
45
-
46
- # Clear the element field
47
- # @return [void]
48
- def clear
49
- @subject.clear
50
- end
51
-
52
- # @return [Boolean]
53
- def visible?
54
- @subject.displayed?
55
- end
56
-
57
- # @return [Boolean]
58
- def invisible?
59
- !@subject.displayed?
60
- end
61
-
62
- # Get the location
63
- # @return [Point]
64
- def location
65
- @subject.location
66
- end
67
-
68
- # Get the size of an element
69
- # @return [Dimension]
70
- def size
71
- @subject.size
72
- end
73
-
74
- # Perform a submit action on an element
75
- # @return [void]
76
- def submit
77
- @subject.submit
12
+ def initialize(driver, element)
13
+ @driver = driver
14
+ @subject = element
78
15
  end
79
16
 
80
17
  # Scroll the element into view
@@ -82,37 +19,33 @@ module AutomationObject
82
19
  def scroll_into_view
83
20
  @subject.location_once_scrolled_into_view
84
21
 
85
- #Only scroll better if this is a browser and not an app
86
- return unless @driver.is_browser?
22
+ # Only scroll better if this is a browser and not an app
23
+ return unless @driver.browser?
24
+
25
+ center = element_center
26
+ @driver.execute_script("window.scroll(#{center[:x]},#{ideal_y_position});")
27
+ # Just in case in close to the top or bottom bounds of the window
28
+ element_location = @subject.location_once_scrolled_into_view
29
+
30
+ return unless (element_location[:y]).negative?
87
31
 
88
- element_center = self.element_center
89
- window_height = self.inner_window_height
90
32
  scroll_position = @driver.scroll_position
33
+ scroll_y_position = scroll_position[:y] + element_location[:y]
34
+ @driver.execute_script("window.scroll(#{element_location[:x]},#{scroll_y_position});")
35
+ end
91
36
 
92
- if element_center[:y] < (window_height/2)
93
- ideal_y_position = (scroll_position[:y] + element_center[:y] - (window_height.to_f / 2.0)).abs
94
- else
95
- ideal_y_position = (scroll_position[:y] - element_center[:y] + (window_height.to_f / 2.0)).abs
96
- end
37
+ private
97
38
 
98
- @driver.execute_script("window.scroll(#{element_center[:x]},#{ideal_y_position});")
99
- #Just in case in close to the top or bottom bounds of the window
100
- element_location = @subject.location_once_scrolled_into_view
39
+ def ideal_y_position
40
+ center = element_center
41
+ window_height = inner_window_height
42
+ scroll_position = @driver.scroll_position
101
43
 
102
- if element_location[:y] < 0
103
- scroll_position = @driver.scroll_position
104
- scroll_y_position = scroll_position[:y] + element_location[:y]
105
- @driver.execute_script("window.scroll(#{element_location[:x]},#{scroll_y_position});")
106
- end
107
- end
44
+ return (scroll_position[:y] + center[:y] - (window_height.to_f / 2.0)).abs if center[:y] < (window_height / 2)
108
45
 
109
- # Perform a click action on the element
110
- # @return [void]
111
- def click
112
- self.scroll_into_view if @driver.is_browser?
113
- @subject.click
46
+ (scroll_position[:y] - center[:y] + (window_height.to_f / 2.0)).abs
114
47
  end
115
48
  end
116
49
  end
117
50
  end
118
- end
51
+ end
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
1
2
  module AutomationObject
2
3
  module Driver
4
+ # Base class for driver and element adapter interface
3
5
  class Base
4
6
  attr_accessor :adapter
5
7
 
6
- def initialize(adapter_object, *args)
8
+ def initialize(adapter_object, *_args)
7
9
  self.adapter = adapter_object
8
10
  end
9
11
  end
10
12
  end
11
- end
13
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../driver'
3
+
4
+ module AutomationObject
5
+ module Driver
6
+ module CommonSelenium
7
+ # Common Selenium/AppiumMethods
8
+ module Driver
9
+ # Suspend timeout for block running
10
+ def suspend_timeout
11
+ original_timeout = @subject.manage.timeouts.implicit_wait
12
+ @subject.manage.timeouts.implicit_wait = 0
13
+
14
+ yield
15
+
16
+ @subject.manage.timeouts.implicit_wait = original_timeout
17
+ end
18
+
19
+ # @return [Point] x,y scroll position
20
+ def scroll_position
21
+ position = Point.new
22
+
23
+ position.x = @subject.execute_script('var doc = document.documentElement; return (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);').to_f
24
+ position.y = @subject.execute_script('var doc = document.documentElement; return (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);').to_f
25
+
26
+ position
27
+ end
28
+
29
+ # @return [Float] inner window height
30
+ def inner_window_height
31
+ @subject.execute_script('return window.innerHeight;').to_f
32
+ end
33
+
34
+ # @param script [String] JS to run
35
+ # @return [Object, nil]
36
+ def execute_script(script)
37
+ @subject.execute_script(script)
38
+ end
39
+
40
+ # Go back
41
+ # @return [void]
42
+ def back
43
+ @subject.navigate.back
44
+ end
45
+
46
+ # Go forward
47
+ # @return [void]
48
+ def forward
49
+ @subject.navigate.forward
50
+ end
51
+
52
+ # Refresh window
53
+ # @return [void]
54
+ def refresh
55
+ @subject.navigate.refresh
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../driver'
3
+ require_relative 'element_geometry'
4
+
5
+ module AutomationObject
6
+ module Driver
7
+ module CommonSelenium
8
+ # Helper module for Selenium based elements
9
+ module Element
10
+ include ElementGeometry
11
+
12
+ # @return [Boolean] element visible
13
+ def visible?
14
+ @subject.displayed?
15
+ end
16
+
17
+ # @return [Boolean] element invisible
18
+ def invisible?
19
+ @subject.displayed? ? false : true
20
+ end
21
+
22
+ # @return [String, nil] id of element
23
+ def id
24
+ @subject.attribute('id')
25
+ end
26
+
27
+ # @return [String, nil] href of element
28
+ def href
29
+ @subject.attribute('href')
30
+ end
31
+
32
+ # Text of element
33
+ # @return [String, nil]
34
+ def text
35
+ @subject.text
36
+ end
37
+
38
+ # @return [String, nil] content of element
39
+ def content
40
+ @subject.attribute('content')
41
+ end
42
+
43
+ # @param key [String] attribute key to get or set
44
+ # @param value [Object] optional value to set the key as
45
+ # @return [Object] value of attribute
46
+ def attribute(key, value = false)
47
+ if value
48
+ script = "return arguments[0].#{key} = '#{value}'"
49
+ @driver.execute_script(script, @subject)
50
+ end
51
+
52
+ @subject.attribute(key)
53
+ end
54
+
55
+ # Type into an element
56
+ # @return [void]
57
+ def send_keys(string)
58
+ @subject.send_keys(string)
59
+ end
60
+
61
+ # Clear the element field
62
+ # @return [void]
63
+ def clear
64
+ @subject.clear
65
+ end
66
+
67
+ # Perform a submit action on an element
68
+ # @return [void]
69
+ def submit
70
+ @subject.submit
71
+ end
72
+
73
+ # Perform a click action on the element
74
+ # @return [void]
75
+ def click
76
+ scroll_into_view if @driver.browser?
77
+ @subject.click
78
+ end
79
+
80
+ # Hover over element
81
+ def hover
82
+ scroll_into_view
83
+ @driver.action.move_to(@subject).perform
84
+ end
85
+
86
+ # Helper method to switch to this element's iframe
87
+ def switch_to_iframe
88
+ @driver.switch_to.frame(iframe_switch_value)
89
+ end
90
+
91
+ protected
92
+
93
+ # Helper method for getting the value to switch to
94
+ # If value doesn't exist then create one
95
+ # @return [String] iframe value to switch to
96
+ def iframe_switch_value
97
+ iframe_switch_value = attribute('id')
98
+ iframe_switch_value = attribute('name') if iframe_switch_value.length.zero?
99
+
100
+ iframe_switch_value = attribute('name', SecureRandom.hex(16)) unless iframe_switch_value
101
+
102
+ iframe_switch_value
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+ require_relative '../driver'
3
+
4
+ module AutomationObject
5
+ module Driver
6
+ module CommonSelenium
7
+ # Helper module for Selenium based elements
8
+ module ElementGeometry
9
+ # @return [Numeric] x position of element
10
+ def x
11
+ @subject.location.x
12
+ end
13
+
14
+ # @return [Numeric] y position of element
15
+ def y
16
+ @subject.location.y
17
+ end
18
+
19
+ # @return [Numeric] width of element
20
+ def width
21
+ @subject.size.width
22
+ end
23
+
24
+ # @return [Numeric] height of element
25
+ def height
26
+ @subject.size.height
27
+ end
28
+
29
+ # Get the location
30
+ # @return [Point]
31
+ def location
32
+ @subject.location
33
+ end
34
+
35
+ # Get the size of an element
36
+ # @return [Dimension]
37
+ def size
38
+ @subject.size
39
+ end
40
+
41
+ # @return [Hash] :x, :y coordinates
42
+ def element_center
43
+ element_location = @subject.location
44
+ element_size = @subject.size
45
+
46
+ center = Point.new
47
+ center.x = (element_location.x.to_f + element_size.width.to_f / 2).to_f
48
+ center.y = (element_location.y.to_f + element_size.height.to_f / 2).to_f
49
+
50
+ center
51
+ end
52
+
53
+ # @return [BoxCoordinates] :x1, :x2, :y1, :y2 coordinates of a box
54
+ def box_coordinates
55
+ element_location = @subject.location
56
+ element_size = @subject.size
57
+
58
+ box_coordinates = BoxCoordinates.new
59
+ box_coordinates.x1 = element_location.x.to_f
60
+ box_coordinates.y1 = element_location.y.to_f
61
+ box_coordinates.x2 = element_location.x.to_f + element_size.width.to_f
62
+ box_coordinates.y2 = element_location.y.to_f + element_size.height.to_f
63
+
64
+ box_coordinates
65
+ end
66
+
67
+ # @param second_element_object [Object] element to compare to
68
+ # @param collision_tolerance [Numeric] pixel tolerance of collisions
69
+ # @return [Boolean] element collides with other
70
+ def collides_with_element?(second_element_object, collision_tolerance = 0)
71
+ box_one = box_coordinates
72
+ box_two = second_element_object.box_coordinates
73
+
74
+ if box_one.x2 > box_two.x1 && box_one.x1 < box_two.x2 && box_one.y2 > box_two.y1 && box_one.y1 < box_two.y2
75
+ if box_one.x2 > (box_two.x1 + collision_tolerance) && (box_one.x1 + collision_tolerance) < box_two.x2 &&
76
+ box_one.y2 > (box_two.y1 + collision_tolerance) && (box_one.y1 + collision_tolerance) < box_two.y2
77
+ return true
78
+ end
79
+ end
80
+
81
+ false
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require_relative 'base'
2
3
  require_relative 'element'
3
4
 
@@ -5,101 +6,130 @@ module AutomationObject
5
6
  module Driver
6
7
  Point = Struct.new(:x, :y)
7
8
  Dimension = Struct.new(:width, :height)
9
+ BoxCoordinates = Struct.new(:x1, :y1, :x2, :y2)
8
10
 
9
- #Driver adapter interface
10
- #Adapters should meet this interface standard
11
+ # Driver adapter interface
12
+ # Adapters should meet this interface standard
11
13
  class Driver < Base
12
14
  # Navigates current window to a given url
13
15
  # @param url [String] navigate to the following url
14
16
  # @return [void]
15
17
  def get(url)
16
- self.adapter.get(url)
18
+ adapter.get(url)
19
+ end
20
+
21
+ # Get the title of the document
22
+ # @return [String]
23
+ def title
24
+ adapter.title
25
+ end
26
+
27
+ # Refreshes current window
28
+ # @return [void]
29
+ def refresh
30
+ adapter.refresh
31
+ end
32
+
33
+ # Go forward
34
+ # @return [void]
35
+ def forward
36
+ adapter.forward
37
+ end
38
+
39
+ # Go back
40
+ # @return [void]
41
+ def back
42
+ adapter.back
17
43
  end
18
44
 
19
45
  # Set timeout wait
20
46
  # @param timeout [Integer] the timeout in seconds
21
47
  # @return [void]
22
- def set_wait(timeout = nil)
23
- self.adapter.set_wait(timeout)
48
+ def wait(timeout = nil)
49
+ adapter.wait(timeout)
24
50
  end
25
51
 
26
52
  # @param selector_type [Symbol] selector type (:css, :xpath, etc...)
27
53
  # @param selector_path [String] path to element
28
54
  # @return [Boolean] exists or not
29
55
  def exists?(selector_type, selector_path)
30
- self.adapter.exists?(selector_type, selector_path)
56
+ adapter.exists?(selector_type, selector_path)
31
57
  end
32
58
 
33
59
  # @param selector_type [Symbol] selector type, :css, :xpath, etc...
34
60
  # @param selector_path [String] path to element
35
61
  # @return [AutomationObject::Driver::Element] element
36
62
  def find_element(selector_type, selector_path)
37
- self.adapter.find_element(selector_type, selector_path)
63
+ adapter.find_element(selector_type, selector_path)
38
64
  end
39
65
 
40
66
  # @param selector_type [Symbol] selector type, :css, :xpath, etc...
41
67
  # @param selector_path [String] path to element
42
68
  # @return [Array<AutomationObject::Driver::Element>] elements
43
69
  def find_elements(selector_type, selector_path)
44
- self.adapter.find_elements(selector_type, selector_path)
70
+ adapter.find_elements(selector_type, selector_path)
45
71
  end
46
72
 
47
- #Accept prompt either in browser or mobile
73
+ # Accept prompt either in browser or mobile
48
74
  def accept_prompt
49
- self.adapter.accept_prompt
75
+ adapter.accept_prompt
50
76
  end
51
77
 
52
- #Dismiss the prompt
78
+ # Dismiss the prompt
53
79
  def dismiss_prompt
54
- self.adapter.dismiss_prompt
80
+ adapter.dismiss_prompt
55
81
  end
56
82
 
57
83
  # Check if browser, more useful for Appium but can be generic here
58
84
  # @return [Boolean] whether or not browser is being used
59
- def is_browser?
60
- self.adapter.is_browser?
85
+ def browser?
86
+ adapter.browser?
61
87
  end
62
88
 
63
- #Window Handles
89
+ # Window Handles
64
90
  # @return [Array<String>] array of window handle ids
65
91
  def window_handles
66
- self.adapter.window_handles
92
+ adapter.window_handles
67
93
  end
68
94
 
69
- #Current window handle
95
+ # Current window handle
70
96
  # @return [String] handle id
71
97
  def window_handle
72
- self.adapter.window_handle
98
+ adapter.window_handle
73
99
  end
74
100
 
75
- #Set current window handle to, will switch windows
101
+ # Set current window handle to, will switch windows
76
102
  # @param handle_value [String] window handle value
77
103
  def window_handle=(handle_value)
78
- self.adapter.window_handle(handle_value)
104
+ adapter.window_handle(handle_value)
79
105
  end
80
106
 
81
107
  # Run script in browser to check if document in JS is complete
82
108
  # @return [Boolean] document is complete
83
109
  def document_complete?
84
- self.adapter.document_complete?
85
- end
86
-
87
- # Wait till the document is complete
88
- # @return [void]
89
- def document_complete_wait
90
- self.adapter.document_complete_wait
110
+ adapter.document_complete?
91
111
  end
92
112
 
93
113
  # @param script [String] JS to run
94
114
  # @return [Object, nil]
95
115
  def execute_script(script)
96
- self.adapter.execute_script(script)
116
+ adapter.execute_script(script)
117
+ end
118
+
119
+ # @return [Point] x,y scroll position
120
+ def scroll_position
121
+ adapter.scroll_position
122
+ end
123
+
124
+ # @return [Float] inner window height
125
+ def inner_window_height
126
+ adapter.inner_window_height
97
127
  end
98
128
 
99
129
  # Destroy the driver
100
130
  def quit
101
- self.adapter.quit
131
+ adapter.quit
102
132
  end
103
133
  end
104
134
  end
105
- end
135
+ end