ruby_raider 3.13.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ffmpeg/build.sh +208 -0
  3. data/.github/ffmpeg/check.sh +72 -0
  4. data/.github/ffmpeg/frames.rb +34 -0
  5. data/.github/ffmpeg/probe.sh +17 -0
  6. data/.github/workflows/ffmpeg.yml +160 -0
  7. data/.github/workflows/runtime.yml +38 -0
  8. data/.github/workflows/system_tests.yml +1 -23
  9. data/.reek.yml +1 -0
  10. data/.rspec +1 -0
  11. data/Gemfile +2 -0
  12. data/README.md +172 -70
  13. data/RELEASE.md +29 -0
  14. data/bin/raider +5 -0
  15. data/lib/commands/scaffolding_commands.rb +2 -25
  16. data/lib/commands/utility_commands.rb +23 -3
  17. data/lib/generators/automation/automation_generator.rb +0 -5
  18. data/lib/generators/automation/templates/component.tt +1 -0
  19. data/lib/generators/automation/templates/home.tt +2 -2
  20. data/lib/generators/automation/templates/login.tt +6 -26
  21. data/lib/generators/automation/templates/page.tt +23 -18
  22. data/lib/generators/automation/templates/partials/home_page_selector.tt +1 -2
  23. data/lib/generators/automation/templates/partials/initialize_selector.tt +0 -8
  24. data/lib/generators/automation/templates/partials/pdp_page_selector.tt +1 -2
  25. data/lib/generators/automation/templates/partials/url_methods.tt +8 -2
  26. data/lib/generators/automation/templates/partials/visit_method.tt +0 -5
  27. data/lib/generators/automation/templates/pdp.tt +1 -9
  28. data/lib/generators/common_generator.rb +0 -2
  29. data/lib/generators/generator.rb +12 -19
  30. data/lib/generators/helper_generator.rb +21 -19
  31. data/lib/generators/infrastructure/templates/azure_pipelines.tt +1 -1
  32. data/lib/generators/infrastructure/templates/github.tt +2 -3
  33. data/lib/generators/infrastructure/templates/github_appium.tt +0 -8
  34. data/lib/generators/invoke_generators.rb +4 -23
  35. data/lib/generators/menu_generator.rb +1 -9
  36. data/lib/generators/rspec/templates/accessibility_spec.tt +1 -9
  37. data/lib/generators/rspec/templates/spec.tt +8 -24
  38. data/lib/generators/template_renderer/partial_cache.rb +3 -1
  39. data/lib/generators/template_renderer.rb +1 -1
  40. data/lib/generators/templates/common/gemfile.tt +3 -10
  41. data/lib/generators/templates/common/git_ignore.tt +0 -5
  42. data/lib/generators/templates/common/partials/mobile_config.tt +3 -1
  43. data/lib/generators/templates/common/partials/web_config.tt +10 -1
  44. data/lib/generators/templates/common/rakefile.tt +0 -15
  45. data/lib/generators/templates/common/read_me.tt +363 -257
  46. data/lib/generators/templates/common/rubocop.tt +0 -2
  47. data/lib/generators/templates/helpers/driver_helper.tt +8 -0
  48. data/lib/generators/templates/helpers/gesture_helper.tt +78 -0
  49. data/lib/generators/templates/helpers/network_helper.tt +521 -0
  50. data/lib/generators/templates/helpers/partials/quit_driver.tt +0 -2
  51. data/lib/generators/templates/helpers/partials/screenshot.tt +0 -2
  52. data/lib/generators/templates/helpers/partials/selenium_driver.tt +5 -2
  53. data/lib/generators/templates/helpers/screenshot_helper.tt +490 -0
  54. data/lib/generators/templates/helpers/spec_helper.tt +39 -29
  55. data/lib/generators/templates/helpers/trace_helper.tt +240 -95
  56. data/lib/generators/templates/helpers/video_helper.tt +4 -10
  57. data/lib/ruby_raider/ffmpeg.rb +38 -11
  58. data/lib/ruby_raider/runtime/actionability.rb +129 -0
  59. data/lib/ruby_raider/runtime/actionable.rb +49 -0
  60. data/lib/ruby_raider/runtime/by_role.rb +55 -0
  61. data/lib/ruby_raider/runtime/config.rb +79 -0
  62. data/lib/ruby_raider/runtime/context.rb +38 -0
  63. data/lib/ruby_raider/runtime/errors.rb +69 -0
  64. data/lib/ruby_raider/runtime/finders.rb +73 -0
  65. data/lib/ruby_raider/runtime/js/actionability.js +134 -0
  66. data/lib/ruby_raider/runtime/js/role_engine.js +315 -0
  67. data/lib/ruby_raider/runtime/locator_converter.rb +72 -0
  68. data/lib/ruby_raider/runtime/matchers/base.rb +222 -0
  69. data/lib/ruby_raider/runtime/matchers/element.rb +183 -0
  70. data/lib/ruby_raider/runtime/matchers/page.rb +71 -0
  71. data/lib/ruby_raider/runtime/matchers/presence.rb +41 -0
  72. data/lib/ruby_raider/runtime/matchers/retrying_block.rb +104 -0
  73. data/lib/ruby_raider/runtime/observer.rb +170 -0
  74. data/lib/ruby_raider/runtime/role_finder.rb +83 -0
  75. data/lib/ruby_raider/runtime/rspec.rb +78 -0
  76. data/lib/ruby_raider/runtime/selectors.rb +60 -0
  77. data/lib/ruby_raider/runtime/strict.rb +142 -0
  78. data/lib/ruby_raider/runtime/wait.rb +84 -0
  79. data/lib/ruby_raider/runtime.rb +20 -0
  80. data/lib/scaffolding/name_normalizer.rb +1 -1
  81. data/lib/scaffolding/project_detector.rb +2 -28
  82. data/lib/scaffolding/scaffolding.rb +0 -20
  83. data/lib/scaffolding/templates/component.tt +0 -14
  84. data/lib/scaffolding/templates/page_object.tt +2 -14
  85. data/lib/scaffolding/templates/spec.tt +1 -9
  86. data/lib/utilities/utilities.rb +10 -8
  87. data/lib/version +1 -1
  88. data/ruby_raider.gemspec +2 -1
  89. data/sig/commands/scaffolding_commands.rbs +0 -2
  90. data/sig/commands/utility_commands.rbs +6 -0
  91. data/sig/generators/generator.rbs +0 -4
  92. data/sig/generators/helper_generator.rbs +0 -1
  93. data/sig/generators/menu_generator.rbs +0 -1
  94. data/sig/ruby_raider/ffmpeg.rbs +1 -0
  95. data/sig/ruby_raider/runtime/actionability.rbs +25 -0
  96. data/sig/ruby_raider/runtime/actionable.rbs +13 -0
  97. data/sig/ruby_raider/runtime/by_role.rbs +15 -0
  98. data/sig/ruby_raider/runtime/config.rbs +26 -0
  99. data/sig/ruby_raider/runtime/context.rbs +12 -0
  100. data/sig/ruby_raider/runtime/errors.rbs +36 -0
  101. data/sig/ruby_raider/runtime/finders.rbs +26 -0
  102. data/sig/ruby_raider/runtime/locator_converter.rbs +21 -0
  103. data/sig/ruby_raider/runtime/matchers/base.rbs +64 -0
  104. data/sig/ruby_raider/runtime/matchers/element.rbs +78 -0
  105. data/sig/ruby_raider/runtime/matchers/page.rbs +38 -0
  106. data/sig/ruby_raider/runtime/matchers/presence.rbs +21 -0
  107. data/sig/ruby_raider/runtime/matchers/retrying_block.rbs +27 -0
  108. data/sig/ruby_raider/runtime/observer.rbs +46 -0
  109. data/sig/ruby_raider/runtime/role_finder.rbs +18 -0
  110. data/sig/ruby_raider/runtime/selectors.rbs +21 -0
  111. data/sig/ruby_raider/runtime/strict.rbs +35 -0
  112. data/sig/ruby_raider/runtime/wait.rbs +17 -0
  113. data/sig/ruby_raider/runtime.rbs +2 -0
  114. data/sig/scaffolding/project_detector.rbs +0 -4
  115. data/sig/scaffolding/scaffolding.rbs +0 -5
  116. data/sig/utilities/utilities.rbs +2 -1
  117. data/sig/vendor/selenium_webdriver.rbs +71 -0
  118. data/spec/commands/raider_commands_spec.rb +38 -3
  119. data/spec/commands/scaffolding_commands_spec.rb +27 -0
  120. data/spec/commands/utility_commands_spec.rb +53 -0
  121. data/spec/generators/gesture_helper_spec.rb +123 -0
  122. data/spec/generators/network_helper_spec.rb +634 -0
  123. data/spec/generators/screenshot_helper_spec.rb +776 -0
  124. data/spec/generators/template_renderer_spec.rb +0 -4
  125. data/spec/generators/trace_helper_spec.rb +489 -97
  126. data/spec/generators/video_helper_spec.rb +21 -13
  127. data/spec/integration/commands/browser_update_after_creation_spec.rb +0 -8
  128. data/spec/integration/commands/scaffolding_commands_spec.rb +12 -70
  129. data/spec/integration/commands/utility_commands_spec.rb +19 -41
  130. data/spec/integration/content/azure_content_spec.rb +2 -6
  131. data/spec/integration/content/ci_content_spec.rb +1 -55
  132. data/spec/integration/content/common_content_spec.rb +1 -118
  133. data/spec/integration/content/config_content_spec.rb +34 -31
  134. data/spec/integration/content/gemfile_content_spec.rb +7 -72
  135. data/spec/integration/content/helper_content_spec.rb +105 -99
  136. data/spec/integration/content/page_content_spec.rb +85 -87
  137. data/spec/integration/content/readme_content_spec.rb +141 -77
  138. data/spec/integration/content/rspec_reporter_content_spec.rb +2 -2
  139. data/spec/integration/content/rubocop_content_spec.rb +1 -1
  140. data/spec/integration/content/syntax_validation_spec.rb +2 -2
  141. data/spec/integration/content/test_content_spec.rb +23 -147
  142. data/spec/integration/end_to_end_features_spec.rb +1 -43
  143. data/spec/integration/end_to_end_spec.rb +334 -329
  144. data/spec/integration/generators/automation_generator_spec.rb +3 -37
  145. data/spec/integration/generators/axe_addon_spec.rb +2 -35
  146. data/spec/integration/generators/azure_generator_spec.rb +1 -1
  147. data/spec/integration/generators/common_generator_spec.rb +1 -40
  148. data/spec/integration/generators/config_features_spec.rb +1 -24
  149. data/spec/integration/generators/github_generator_spec.rb +0 -12
  150. data/spec/integration/generators/helpers_generator_spec.rb +74 -80
  151. data/spec/integration/generators/invoke_generators_spec.rb +54 -45
  152. data/spec/integration/generators/rspec_generator_spec.rb +0 -6
  153. data/spec/integration/scaffolding_e2e_spec.rb +17 -179
  154. data/spec/integration/spec_helper.rb +4 -6
  155. data/spec/integration/trace_format_checker.rb +11 -2
  156. data/spec/integration/trace_page_server.rb +72 -5
  157. data/spec/menus/menu_generator_spec.rb +14 -39
  158. data/spec/runtime/actionability_spec.rb +207 -0
  159. data/spec/runtime/actionable_spec.rb +125 -0
  160. data/spec/runtime/browser/actionability_script_browser_spec.rb +150 -0
  161. data/spec/runtime/browser/actionable_browser_spec.rb +98 -0
  162. data/spec/runtime/browser/finders_browser_spec.rb +148 -0
  163. data/spec/runtime/browser/matchers_browser_spec.rb +67 -0
  164. data/spec/runtime/browser/retrying_block_browser_spec.rb +37 -0
  165. data/spec/runtime/browser/role_engine_browser_spec.rb +184 -0
  166. data/spec/runtime/browser/strict_browser_spec.rb +38 -0
  167. data/spec/runtime/by_role_spec.rb +52 -0
  168. data/spec/runtime/config_spec.rb +79 -0
  169. data/spec/runtime/context_spec.rb +73 -0
  170. data/spec/runtime/errors_spec.rb +89 -0
  171. data/spec/runtime/finders_spec.rb +165 -0
  172. data/spec/runtime/fixtures/actionability.html +70 -0
  173. data/spec/runtime/fixtures/locators.html +46 -0
  174. data/spec/runtime/fixtures/matchers.html +33 -0
  175. data/spec/runtime/fixtures/roles.html +162 -0
  176. data/spec/runtime/matchers/base_spec.rb +165 -0
  177. data/spec/runtime/matchers/element_spec.rb +277 -0
  178. data/spec/runtime/matchers/page_spec.rb +102 -0
  179. data/spec/runtime/matchers/presence_spec.rb +59 -0
  180. data/spec/runtime/matchers/retrying_block_spec.rb +165 -0
  181. data/spec/runtime/observer_spec.rb +341 -0
  182. data/spec/runtime/role_finder_spec.rb +156 -0
  183. data/spec/runtime/role_query_spec.rb +75 -0
  184. data/spec/runtime/selectors_spec.rb +135 -0
  185. data/spec/runtime/strict_spec.rb +257 -0
  186. data/spec/runtime/support/browser.rb +48 -0
  187. data/spec/runtime/support/fake_bridge.rb +117 -0
  188. data/spec/runtime/wait_spec.rb +71 -0
  189. data/spec/scaffolding/name_normalizer_spec.rb +3 -2
  190. data/spec/scaffolding/scaffold_project_detector_spec.rb +4 -43
  191. data/spec/system/selenium_spec.rb +1 -5
  192. data/spec/system/support/system_test_helper.rb +2 -5
  193. data/spec/utilities/ffmpeg_spec.rb +80 -7
  194. data/spec/utilities/headless_config_spec.rb +0 -7
  195. data/spec/utilities/utilities_spec.rb +24 -0
  196. data/spec/utilities/video_command_spec.rb +13 -0
  197. metadata +118 -53
  198. data/docs/superpowers/plans/2026-10-25-failure-video-replay.md +0 -400
  199. data/docs/superpowers/plans/2026-10-26-parity-cleanup.md +0 -233
  200. data/docs/superpowers/plans/2026-10-28-failure-card.md +0 -295
  201. data/docs/superpowers/specs/2026-10-25-failure-video-replay-design.md +0 -214
  202. data/docs/superpowers/specs/2026-10-28-failure-card-design.md +0 -210
  203. data/lib/generators/automation/templates/header.tt +0 -9
  204. data/lib/generators/cucumber/cucumber_generator.rb +0 -58
  205. data/lib/generators/cucumber/templates/accessibility_feature.tt +0 -5
  206. data/lib/generators/cucumber/templates/accessibility_steps.tt +0 -17
  207. data/lib/generators/cucumber/templates/cucumber.tt +0 -1
  208. data/lib/generators/cucumber/templates/env.tt +0 -7
  209. data/lib/generators/cucumber/templates/feature.tt +0 -21
  210. data/lib/generators/cucumber/templates/partials/appium_env.tt +0 -26
  211. data/lib/generators/cucumber/templates/partials/driver_world.tt +0 -18
  212. data/lib/generators/cucumber/templates/partials/mobile_steps.tt +0 -20
  213. data/lib/generators/cucumber/templates/partials/selenium_env.tt +0 -120
  214. data/lib/generators/cucumber/templates/partials/watir_env.tt +0 -48
  215. data/lib/generators/cucumber/templates/partials/watir_world.tt +0 -4
  216. data/lib/generators/cucumber/templates/partials/web_steps.tt +0 -28
  217. data/lib/generators/cucumber/templates/steps.tt +0 -5
  218. data/lib/generators/cucumber/templates/user_factory.tt +0 -15
  219. data/lib/generators/cucumber/templates/world.tt +0 -7
  220. data/lib/generators/templates/helpers/browser_helper.tt +0 -31
  221. data/lib/generators/templates/helpers/raider_element.tt +0 -159
  222. data/lib/generators/templates/helpers/wait_helper.tt +0 -611
  223. data/lib/scaffolding/templates/feature.tt +0 -6
  224. data/lib/scaffolding/templates/steps.tt +0 -19
  225. data/sig/generators/cucumber/cucumber_generator.rbs +0 -12
  226. data/spec/fixtures/watir_master/cucumber_watir/Readme.md.snapshot +0 -175
  227. data/spec/fixtures/watir_master/cucumber_watir/features/step_definitions/login_steps.rb.snapshot +0 -24
  228. data/spec/fixtures/watir_master/cucumber_watir/page_objects/abstract/page.rb.snapshot +0 -37
  229. data/spec/fixtures/watir_master/cucumber_watir/page_objects/pages/login.rb.snapshot +0 -33
  230. data/spec/fixtures/watir_master/rspec_watir/Readme.md.snapshot +0 -174
  231. data/spec/fixtures/watir_master/rspec_watir/page_objects/abstract/page.rb.snapshot +0 -37
  232. data/spec/fixtures/watir_master/rspec_watir/page_objects/pages/login.rb.snapshot +0 -33
  233. data/spec/fixtures/watir_master/rspec_watir/spec/login_page_spec.rb.snapshot +0 -49
  234. data/spec/integration/content/cucumber_reporter_content_spec.rb +0 -161
  235. data/spec/integration/generators/cucumber_generator_spec.rb +0 -100
  236. data/spec/integration/helpers/wait_helper_mobile_spec.rb +0 -281
  237. data/spec/integration/helpers/wait_helper_spec.rb +0 -679
  238. data/spec/integration/helpers/wait_helper_support.rb +0 -183
  239. data/spec/system/watir_spec.rb +0 -38
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'selenium-webdriver'
4
+ require_relative 'config'
5
+ require_relative 'role_finder'
6
+
7
+ # Reports to an optional observer. A failing observer is switched off, never the test.
8
+ module Raider
9
+ class << self
10
+ # Calls hook on the observer when it answers it; the block builds the details lazily.
11
+ # A broken observer (even a NotImplementedError or SystemStackError) is switched off, never the test.
12
+ def report(hook, *args, **details)
13
+ watcher = observer
14
+ return unless watcher.respond_to?(hook)
15
+
16
+ Observer.guarded { watcher.public_send(hook, *args, **(block_given? ? yield : details)) }
17
+ rescue StandardError, ScriptError, SystemStackError => e
18
+ Observer.stop(watcher, e)
19
+ end
20
+ end
21
+
22
+ # Element actions as events, and the locator stamped on every found element.
23
+ module Observer
24
+ LIB_DIR = File.expand_path(__dir__)
25
+ GUARD_KEY = :raider_in_observer
26
+ MESSAGE_LIMIT = 200
27
+
28
+ STRATEGIES = {
29
+ 'css_selector' => 'css', 'css selector' => 'css', 'class' => 'class_name', 'class name' => 'class_name',
30
+ 'link' => 'link_text', 'link text' => 'link_text', 'partial_link' => 'partial_link_text',
31
+ 'partial link text' => 'partial_link_text', 'tag name' => 'tag_name'
32
+ }.freeze
33
+
34
+ # One reported action. :reek:TooManyInstanceVariables { max_instance_variables: 6 } -- the event's whole state
35
+ class Run
36
+ def initialize(element, action, args)
37
+ @element = element
38
+ @action = action
39
+ @args = args&.dup&.freeze # an observer can read the keys, never change what gets typed
40
+ @retries = 0
41
+ @token = Raider.report(:action_started) { Observer.started(element, action, @args) }
42
+ @started = Observer.clock
43
+ end
44
+
45
+ def ready(retries)
46
+ @retries = retries
47
+ Raider.report(:action_ready, @token) { { element: @element } } unless @token.nil?
48
+ end
49
+
50
+ def finish(error)
51
+ Raider.report(:action_finished, @token) { finished(error) } unless @token.nil?
52
+ end
53
+
54
+ private
55
+
56
+ def finished(error)
57
+ @retries = error.retries if error.is_a?(NotActionable)
58
+ { status: error ? :failed : :passed, waited_ms: Observer.millis(@started), retries: @retries,
59
+ error: error, element: @element }
60
+ end
61
+ end
62
+
63
+ class << self
64
+ # Yields a Run (nil without an observer); reports the outcome and re-raises whatever the body raised.
65
+ def emit_action(element, action, args = nil)
66
+ return yield(nil) if Raider.observer.nil? || Thread.current[GUARD_KEY]
67
+
68
+ run = Run.new(element, action, args)
69
+ begin
70
+ result = yield(run)
71
+ rescue Exception => e # rubocop:disable Lint/RescueException -- reports and re-raises, never swallows
72
+ run.finish(e)
73
+ raise
74
+ end
75
+ run.finish(nil)
76
+ result
77
+ end
78
+
79
+ def started(element, action, args)
80
+ stamp = locator_of(element)
81
+ { action: action, locator: stamp&.slice(:strategy, :selector), within: stamp ? stamp[:within] : [], args: args }
82
+ end
83
+
84
+ def locator_of(element)
85
+ element.instance_variable_get(:@raider_locator)
86
+ end
87
+
88
+ def stamp(element, args, parent)
89
+ attach(element, frozen_stamp(args, parent))
90
+ end
91
+
92
+ # One stamp per find, shared by every element it returned.
93
+ def stamp_all(elements, args, parent)
94
+ return elements if elements.empty?
95
+
96
+ shared = frozen_stamp(args, parent)
97
+ elements.each { |element| attach(element, shared) }
98
+ end
99
+
100
+ # Marks the observer as busy so an action taken inside a hook is not reported again.
101
+ def guarded
102
+ previous = Thread.current[GUARD_KEY]
103
+ Thread.current[GUARD_KEY] = true
104
+ yield
105
+ ensure
106
+ Thread.current[GUARD_KEY] = previous
107
+ end
108
+
109
+ # Prints one line and clears the observer, unless the observer was replaced meanwhile.
110
+ def stop(watcher, error)
111
+ $stderr.puts "[raider] observer stopped: #{summary(error)}" # rubocop:disable Style/StderrPuts -- must not be silenced by -W0
112
+ Raider.observer = nil if Raider.observer.equal?(watcher)
113
+ end
114
+
115
+ def millis(started)
116
+ ((clock - started) * 1000).round
117
+ end
118
+
119
+ def clock
120
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
121
+ end
122
+
123
+ private
124
+
125
+ def attach(element, stamp)
126
+ return element if !element.is_a?(Selenium::WebDriver::Element) || element.frozen?
127
+
128
+ element.instance_variable_set(:@raider_locator, stamp)
129
+ element
130
+ end
131
+
132
+ def frozen_stamp(args, parent)
133
+ stamp = describe(args, parent)
134
+ stamp[:within].each(&:freeze).freeze
135
+ stamp.freeze
136
+ end
137
+
138
+ # The class and the first line of the message; never raises, whatever #message does.
139
+ def summary(error)
140
+ line = error.message.to_s.lines.first.to_s.chomp
141
+ "#{error.class}: #{line[0, MESSAGE_LIMIT]}"
142
+ rescue StandardError, ScriptError, SystemStackError
143
+ error.class.to_s
144
+ end
145
+
146
+ def describe(args, parent)
147
+ how, what = args.size == 1 && args.first.is_a?(Hash) ? args.first.first : args
148
+ strategy = how.to_s
149
+ { strategy: -STRATEGIES.fetch(strategy, strategy).to_s, selector: -selector(strategy, what, args),
150
+ within: chain(parent) }
151
+ end
152
+
153
+ def selector(strategy, what, args)
154
+ return role_text(args) if strategy == 'role'
155
+
156
+ (what.is_a?(Array) ? what.first : what).to_s
157
+ end
158
+
159
+ def role_text(args)
160
+ role, options = RoleFinder.parse(args)
161
+ [role, *options.map { |key, value| "#{key}: #{value}" }].join(', ')
162
+ end
163
+
164
+ def chain(parent)
165
+ found = parent.is_a?(Selenium::WebDriver::Element) ? locator_of(parent) : nil
166
+ found ? [*found[:within], found.slice(:strategy, :selector)] : []
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'selenium-webdriver'
4
+
5
+ module Raider
6
+ # The in-page role engine and the query that calls it.
7
+ module RoleFinder
8
+ ENGINE = File.read(File.join(__dir__, 'js', 'role_engine.js')).freeze
9
+ QUERY = "if (!(window.__raider && window.__raider.role)) throw new Error('__raider not installed'); " \
10
+ 'return window.__raider.role(arguments[0] || document, arguments[1], arguments[2])'
11
+
12
+ NATIVE_MESSAGE = 'by_role needs a web context; use accessibility_id on native screens'
13
+
14
+ # Prepended to SearchContext: sends the :role key to the engine, everything else to Selenium.
15
+ def find_elements(*args)
16
+ role, options = RoleFinder.parse(args)
17
+ return super unless role
18
+
19
+ raise NotSupported, NATIVE_MESSAGE unless Context.web?(bridge)
20
+
21
+ RoleQuery.run(bridge, is_a?(Selenium::WebDriver::Driver) ? nil : self, role, options)
22
+ end
23
+
24
+ def find_element(*args)
25
+ return super unless RoleFinder.role_args?(args)
26
+
27
+ find_elements(*args).first or
28
+ raise Selenium::WebDriver::Error::NoSuchElementError, "no such element: #{Locator.describe(args)}"
29
+ end
30
+
31
+ def self.role_args?(args)
32
+ key = args.size == 1 && args.first.is_a?(Hash) ? args.first.keys.first : args.first
33
+ key.to_s == 'role'
34
+ end
35
+
36
+ # Returns [role, symbol-keyed options] for a role locator, nil for any other.
37
+ def self.parse(args)
38
+ return unless role_args?(args)
39
+
40
+ value = args.size == 1 ? args.first.values.first : args[1]
41
+ role, options = value.is_a?(Array) ? value : [value, nil]
42
+ raise ArgumentError, 'role: needs a role name and an optional options Hash' unless options.nil? || options.is_a?(Hash)
43
+
44
+ options = (options || {}).transform_keys(&:to_sym)
45
+ raise ArgumentError, "role: name must be a String, got #{options[:name].class}" unless [NilClass, String].include?(options[:name].class)
46
+
47
+ token = validated(role)
48
+ RoleLocator.validated_level(token, options[:level])
49
+ [token, options]
50
+ end
51
+
52
+ def self.validated(role)
53
+ raise ArgumentError, "role: needs a role name, got #{role.inspect}" unless role.is_a?(String) || role.is_a?(Symbol)
54
+
55
+ RoleLocator.validated_role(role)
56
+ end
57
+ end
58
+
59
+ # Runs a role query through the bridge, installing the engine on first use.
60
+ module RoleQuery
61
+ module_function
62
+
63
+ def run(bridge, scope, role, options)
64
+ options_js = options_js(options)
65
+ bridge.execute_script(RoleFinder::QUERY, scope, role, options_js)
66
+ rescue Selenium::WebDriver::Error::JavascriptError => e
67
+ raise unless e.message.include?('__raider')
68
+
69
+ bridge.execute_script("#{RoleFinder::ENGINE}\n#{RoleFinder::QUERY}", scope, role, options_js)
70
+ end
71
+
72
+ def options_js(options)
73
+ {
74
+ 'name' => options[:name],
75
+ 'exact' => options[:exact] ? true : false,
76
+ 'includeHidden' => options[:include_hidden] ? true : false,
77
+ 'level' => options[:level]
78
+ }
79
+ end
80
+ end
81
+ end
82
+
83
+ Selenium::WebDriver::SearchContext.prepend(Raider::RoleFinder)
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/expectations'
4
+ require_relative '../runtime'
5
+ require_relative 'matchers/base'
6
+ require_relative 'matchers/element'
7
+ require_relative 'matchers/page'
8
+ require_relative 'matchers/presence'
9
+ require_relative 'matchers/retrying_block'
10
+
11
+ module Raider
12
+ # Web-first RSpec matchers that retry until the page settles.
13
+ # rubocop:disable Naming/PredicatePrefix -- the matcher names are RSpec's vocabulary
14
+ module Matchers
15
+ def have_text(text, exact: false, wait: nil)
16
+ Element::HaveText.new(text, exact: exact, wait: wait)
17
+ end
18
+
19
+ def have_value(value, wait: nil)
20
+ Element::HaveValue.new(value, wait: wait)
21
+ end
22
+
23
+ def have_attribute(name, value = Element::HaveAttribute::PRESENT, wait: nil)
24
+ Element::HaveAttribute.new(name, value, wait: wait)
25
+ end
26
+
27
+ def be_visible(wait: nil)
28
+ Element::BeVisible.new(wait: wait)
29
+ end
30
+
31
+ def be_hidden(wait: nil)
32
+ Element::BeHidden.new(wait: wait)
33
+ end
34
+
35
+ def be_enabled(wait: nil)
36
+ Element::BeEnabled.new(wait: wait)
37
+ end
38
+
39
+ def be_disabled(wait: nil)
40
+ Element::BeDisabled.new(wait: wait)
41
+ end
42
+
43
+ def be_checked(wait: nil)
44
+ Element::BeChecked.new(wait: wait)
45
+ end
46
+
47
+ def have_element(locator = nil, wait: nil, **by)
48
+ raise ArgumentError, 'have_element takes a locator, not both a hash and keywords' if locator && !by.empty?
49
+ raise ArgumentError, 'have_element needs a locator' if locator.nil? && by.empty?
50
+
51
+ Presence::HaveElement.new(locator || by, wait: wait)
52
+ end
53
+
54
+ def have_css(selector, wait: nil)
55
+ have_element({ css: selector }, wait: wait)
56
+ end
57
+
58
+ def have_xpath(selector, wait: nil)
59
+ have_element({ xpath: selector }, wait: wait)
60
+ end
61
+
62
+ def have_count(count, wait: nil)
63
+ Page::HaveCount.new(count, wait: wait)
64
+ end
65
+
66
+ def have_title(expected, wait: nil)
67
+ Page::HaveTitle.new(expected, wait: wait)
68
+ end
69
+
70
+ def have_url(expected, wait: nil)
71
+ Page::HaveUrl.new(expected, wait: wait)
72
+ end
73
+ end
74
+ # rubocop:enable Naming/PredicatePrefix
75
+ end
76
+
77
+ target = RSpec::Expectations::BlockExpectationTarget
78
+ target.prepend(Raider::Matchers::RetryingBlock) unless target.ancestors.include?(Raider::Matchers::RetryingBlock)
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'config'
4
+
5
+ module Raider
6
+ # Builds the XPath and CSS strings behind the label, placeholder, alt,
7
+ # title, text and test_id locator keys. Every caller-supplied value passes
8
+ # through xpath_literal or the CSS escaper; nothing else is interpolated.
9
+ module Selectors
10
+ ATTRIBUTE_NAME = /\A[A-Za-z_][\w-]*\z/
11
+ LABEL_CONTROLS = %w[input textarea select button meter output progress]
12
+ .map { |tag| "self::#{tag}" }.join(' or ').freeze
13
+ EXCLUDED_TEXT = 'not(self::script or self::style or self::noscript or self::template or self::title or self::head) ' \
14
+ 'and not(ancestor::head)'
15
+
16
+ module_function
17
+
18
+ def xpath_literal(string)
19
+ value = string.to_s
20
+ return "'#{value}'" unless value.include?("'")
21
+ return %("#{value}") unless value.include?('"')
22
+
23
+ parts = value.split("'", -1).map { |part| "'#{part}'" }
24
+ "concat(#{parts.join(%q(, "'", ))})"
25
+ end
26
+
27
+ def label(value, exact:)
28
+ literal = xpath_literal(value)
29
+ [
30
+ ".//*[@id = //label[#{match('.', literal, exact)}]/@for]",
31
+ ".//label[#{match('.', literal, exact)}]//*[(#{LABEL_CONTROLS}) and not(self::input[@type='hidden'])]",
32
+ ".//*[#{match('@aria-label', literal, exact)}]",
33
+ ".//*[@aria-labelledby and #{match('string(id(@aria-labelledby))', literal, exact)}]"
34
+ ].join(' | ')
35
+ end
36
+
37
+ def text(value, exact:)
38
+ matcher = match('.', xpath_literal(value), exact)
39
+ candidate = "#{EXCLUDED_TEXT} and #{matcher}"
40
+ ".//*[#{candidate} and not(.//*[#{candidate}])]"
41
+ end
42
+
43
+ def attribute_css(name, value, exact:)
44
+ raise ArgumentError, "invalid attribute name: #{name}" unless name.to_s.match?(ATTRIBUTE_NAME)
45
+
46
+ escaped = value.to_s.gsub('\\') { '\\\\' }.gsub('"') { '\\"' }
47
+ "[#{name}#{exact ? '=' : '*='}\"#{escaped}\"]"
48
+ end
49
+
50
+ def test_id(value)
51
+ attribute_css(Raider.test_id_attribute, value, exact: true)
52
+ end
53
+
54
+ def match(expression, literal, exact)
55
+ return "normalize-space(#{expression}) = #{literal}" if exact
56
+
57
+ "contains(normalize-space(#{expression}), #{literal})"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'selenium-webdriver'
4
+ require_relative 'config'
5
+ require_relative 'errors'
6
+ require_relative 'context'
7
+ require_relative 'finders'
8
+ require_relative 'observer'
9
+
10
+ module Raider
11
+ # Renders a find_element argument list for error messages.
12
+ module Locator
13
+ module_function
14
+
15
+ def describe(args)
16
+ how, what = args
17
+ args.size == 1 && how.is_a?(Hash) ? from_hash(how) : "{ #{how.to_s.inspect}: #{what.inspect} }"
18
+ rescue StandardError
19
+ args.inspect
20
+ end
21
+
22
+ def from_hash(hash)
23
+ key, value = hash.first
24
+ return "by_role(#{role_arguments(value)})" if key == :role && value.is_a?(Array)
25
+
26
+ "{ #{key}: #{plain_arguments(value)} }"
27
+ end
28
+
29
+ def role_arguments(value)
30
+ name, options = value
31
+ ["'#{name}'", *render_options(options)].join(', ')
32
+ end
33
+
34
+ def plain_arguments(value)
35
+ return value.inspect unless value.is_a?(Array)
36
+
37
+ text, options = value
38
+ [text.inspect, *render_options(options)].join(', ')
39
+ end
40
+
41
+ def render_options(options)
42
+ return [] unless options.is_a?(Hash)
43
+
44
+ options.map { |key, option| "#{key}: #{option.inspect}" }
45
+ end
46
+ end
47
+
48
+ # Prepended to SearchContext: find_element must match exactly one element, and
49
+ # both finds wait for a match.
50
+ module StrictFinder
51
+ def find_element(*args)
52
+ Finders.ensure_registered
53
+ attempt = element_attempt(args) { super(*args) }
54
+ Observer.stamp(await_element(args, attempt), args, self)
55
+ end
56
+
57
+ def find_elements(*args)
58
+ return Observer.stamp_all(super, args, self) if Wait.immediate?
59
+
60
+ attempt = elements_attempt(args) { super(*args) }
61
+ found = []
62
+ begin
63
+ Wait.until(timeout: Raider.timeout) { (found = attempt.call).any? }
64
+ rescue TimeoutError
65
+ nil
66
+ end
67
+ Observer.stamp_all(found, args, self)
68
+ end
69
+
70
+ private
71
+
72
+ def await_element(args, attempt)
73
+ return attempt.call || raise(Selenium::WebDriver::Error::NoSuchElementError, not_found(args)) if Wait.immediate?
74
+
75
+ started = clock
76
+ begin
77
+ Wait.until(timeout: Raider.timeout) { attempt.call }
78
+ rescue TimeoutError
79
+ raise Selenium::WebDriver::Error::NoSuchElementError,
80
+ "#{not_found(args)} (waited #{Describe.seconds(clock - started)}s)"
81
+ end
82
+ end
83
+
84
+ # A callable making one attempt at the element: the element, or nil when nothing
85
+ # matched yet. The context is decided here, once per find, not per attempt.
86
+ def element_attempt(args, &raw)
87
+ if RoleFinder.role_args?(args)
88
+ queried = role_attempt(args)
89
+ strict = Raider.strict
90
+ return -> { sole(args, queried.call, strict) }
91
+ end
92
+ return -> { native_or_nil(&raw) } unless Raider.strict && Context.web?(bridge)
93
+
94
+ -> { sole(args, Wait.immediately { find_elements(*args) }, true) }
95
+ end
96
+
97
+ def elements_attempt(args, &raw)
98
+ RoleFinder.role_args?(args) ? role_attempt(args) : raw
99
+ end
100
+
101
+ # Validates the role locator and checks the context once, then queries the engine directly.
102
+ def role_attempt(args)
103
+ role, options = RoleFinder.parse(args)
104
+ raise NotSupported, RoleFinder::NATIVE_MESSAGE unless Context.web?(bridge)
105
+
106
+ scope = is_a?(Selenium::WebDriver::Driver) ? nil : self
107
+ -> { RoleQuery.run(bridge, scope, role, options) }
108
+ end
109
+
110
+ def sole(args, found, strict)
111
+ raise AmbiguousMatch.new(Locator.describe(args), found) if strict && found.size > 1
112
+
113
+ found.first
114
+ end
115
+
116
+ def native_or_nil
117
+ yield
118
+ rescue Selenium::WebDriver::Error::NoSuchElementError
119
+ nil
120
+ end
121
+
122
+ def not_found(args)
123
+ "no such element: #{Locator.describe(args)}"
124
+ end
125
+
126
+ def clock
127
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
128
+ end
129
+ end
130
+ end
131
+
132
+ # Selenium aliases `first` to find_element on Driver and Element, which
133
+ # bypasses the prepend above; `find_elements(...).first` stays the non-strict escape.
134
+ module Raider
135
+ # Routes Selenium's `first` alias through the strict find_element.
136
+ module StrictFirst
137
+ def first(*args) = find_element(*args)
138
+ end
139
+ end
140
+
141
+ Selenium::WebDriver::SearchContext.prepend(Raider::StrictFinder)
142
+ [Selenium::WebDriver::Driver, Selenium::WebDriver::Element].each { |klass| klass.prepend(Raider::StrictFirst) }
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'selenium-webdriver'
4
+ require_relative 'config'
5
+ require_relative 'errors'
6
+
7
+ module Raider
8
+ # The runtime wait loop. Nested waits share one deadline through DEADLINE_KEY
9
+ # (the same key the generated wait helper uses).
10
+ module Wait
11
+ DEADLINE_KEY = :raider_wait_deadline
12
+ # Like DEADLINE_KEY, fiber-scoped on purpose (Thread.current[] is per fiber).
13
+ IMMEDIATE_KEY = :raider_wait_immediate
14
+
15
+ class << self
16
+ def deadline
17
+ Thread.current[DEADLINE_KEY]
18
+ end
19
+
20
+ # Runs the block with finds making one attempt each (the caller does the waiting).
21
+ def immediately
22
+ previous = Thread.current[IMMEDIATE_KEY]
23
+ Thread.current[IMMEDIATE_KEY] = true
24
+ yield
25
+ ensure
26
+ Thread.current[IMMEDIATE_KEY] = previous
27
+ end
28
+
29
+ def immediate?
30
+ Thread.current[IMMEDIATE_KEY] == true
31
+ end
32
+
33
+ # Polls until the block returns a truthy value. The block may
34
+ # `throw :not_ready, reason` to say why it is not ready yet.
35
+ def until(timeout: nil, interval: 0.1)
36
+ limit, = limits(timeout)
37
+ within(limit) do
38
+ reason = nil
39
+ loop do
40
+ result = nil
41
+ thrown = catch(:not_ready) do
42
+ result = yield
43
+ nil
44
+ end
45
+ return result if result
46
+
47
+ reason = thrown || reason
48
+ raise TimeoutError, reason || 'condition not met' if now >= limit
49
+
50
+ pause(limit, interval)
51
+ end
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ # Inside an enclosing deadline, a call without timeout waits for what is left;
58
+ # one with timeout waits that long, never past the enclosing deadline.
59
+ def limits(timeout)
60
+ started = now
61
+ enclosing = deadline
62
+ own = started + (timeout || Raider.timeout) if timeout || !enclosing
63
+ limit = [own, enclosing].compact.min
64
+ [limit, limit - started]
65
+ end
66
+
67
+ def within(limit)
68
+ enclosing = deadline
69
+ Thread.current[DEADLINE_KEY] = limit
70
+ yield
71
+ ensure
72
+ Thread.current[DEADLINE_KEY] = enclosing
73
+ end
74
+
75
+ def pause(limit, interval)
76
+ sleep([[interval, limit - now].min, 0].max)
77
+ end
78
+
79
+ def now
80
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'selenium-webdriver'
4
+ require_relative 'runtime/config'
5
+ require_relative 'runtime/errors'
6
+ require_relative 'runtime/by_role'
7
+ require_relative 'runtime/wait'
8
+ require_relative 'runtime/context'
9
+ require_relative 'runtime/selectors'
10
+ require_relative 'runtime/locator_converter'
11
+ require_relative 'runtime/finders'
12
+ require_relative 'runtime/role_finder'
13
+ require_relative 'runtime/observer'
14
+ require_relative 'runtime/actionability'
15
+ require_relative 'runtime/strict'
16
+ require_relative 'runtime/actionable'
17
+
18
+ Raider::Finders.check_api
19
+ Raider::Finders.install
20
+ Raider::Finders.guard
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NameNormalizer
4
- SUFFIXES = %w[_page _spec _steps _helper _test _feature].freeze
4
+ SUFFIXES = %w[_page _spec _helper _test].freeze
5
5
 
6
6
  module_function
7
7