Module: Specbook::Recorders::TurnipGherkinTracker
- Defined in:
- lib/specbook/recorders/screenshot.rb
Overview
Track Gherkin step index for Turnip features — hook into run_step
Instance Method Summary collapse
Instance Method Details
#run_step(feature_file, step) ⇒ Object
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/specbook/recorders/screenshot.rb', line 558 def run_step(feature_file, step) Specbook::Recorders::Screenshot.advance_gherkin_step! # Capture step definition source code for the spec player begin description = step.respond_to?(:description) ? step.description : step.to_s matches = methods.map { |m| m.to_s.start_with?("match: ") ? send(m.to_s, description) : nil }.compact if matches.first method_obj = method(matches.first.method_name) source_file, source_line = method_obj.source_location params = matches.first.params if source_file rel_path = source_file.sub(Specbook.config.feature_root.to_s + "/", "") lines = File.readlines(source_file) # Extract body lines between `step '...' do` and closing `end` first_line = lines[source_line - 1] step_indent = first_line[/^\s*/].length body_lines = [] (source_line).upto(lines.size - 1) do |i| # start AFTER the step line line = lines[i] # Stop at closing end (same indent level as step) break if line.rstrip =~ /\A\s{0,#{step_indent}}end\s*\z/ body_lines << line end body = body_lines.join # Substitute Turnip params into the source so variables show resolved values. # Only replace when the variable is a standalone argument — not after a dot # (method call) or before a colon (hash key). if params.any? param_names = first_line[/\|([^|]+)\|/, 1]&.split(",")&.map(&:strip) || [] param_names.each_with_index do |pname, idx| next unless params[idx] val = params[idx].is_a?(String) ? %("#{params[idx]}") : params[idx].to_s # Negative lookbehind for dot (method call), negative lookahead for colon (hash key) body = body.gsub(/(?<!\.)(?<![:\w])#{Regexp.escape(pname)}(?![\w:])/) { val } end end Specbook::Recorders::Screenshot.record_step_source!(rel_path, source_line, body) end end rescue StandardError # Step source capture is best-effort end super # Capture a screenshot after every Gherkin step so each has at least one image # Skip if no page has been visited yet (background setup = blank screen) begin url = page.current_url rescue nil return if url.blank? || url == "about:blank" || url == "data:," Specbook::Recorders::Screenshot.flush_assertions!(page) rescue nil if Specbook::Recorders::Screenshot.current_steps.blank? || Specbook::Recorders::Screenshot.current_steps.last&.dig(:gi) != Specbook::Recorders::Screenshot.current_gherkin_idx Specbook::Recorders::Screenshot.capture!(page, step.description, action_type: "gherkin") rescue nil end rescue StandardError # Skip end end |