Module: Specbook::Recorders::CapybaraSessionAssertionPatch

Defined in:
lib/specbook/recorders/screenshot.rb

Overview

Prepend onto Capybara::Session to intercept assertions. After each assertion passes, use Capybara’s own finders to locate the matched element and get its bounding box. No independent JS DOM searches.

Instance Method Summary collapse

Instance Method Details

#assert_no_selector(*args, **opts) ⇒ Object



383
384
385
386
387
388
389
390
# File 'lib/specbook/recorders/screenshot.rb', line 383

def assert_no_selector(*args, **opts)
  if Specbook::Recorders::Screenshot.current_example_name
    desc = humanize_selector(args, opts)
    # Passing NOT assertion = success, show green
    Specbook::Recorders::Screenshot.record_assertion!("Confirmed no #{desc}")
  end
  super
end

#assert_no_text(*args, **opts) ⇒ Object



364
365
366
367
368
369
370
371
# File 'lib/specbook/recorders/screenshot.rb', line 364

def assert_no_text(*args, **opts)
  if Specbook::Recorders::Screenshot.current_example_name
    actual_text = args.find { |a| a.is_a?(String) || a.is_a?(Regexp) }
    # Passing NOT assertion = success, show green
    Specbook::Recorders::Screenshot.record_assertion!("Confirmed no text: '#{actual_text.to_s.truncate(60)}'")
  end
  super
end

#assert_selector(*args, **opts) ⇒ Object



373
374
375
376
377
378
379
380
381
# File 'lib/specbook/recorders/screenshot.rb', line 373

def assert_selector(*args, **opts)
  super.tap do
    if Specbook::Recorders::Screenshot.current_example_name
      desc = humanize_selector(args, opts)
      bboxes = capybara_element_bbox(*args, **opts)
      Specbook::Recorders::Screenshot.record_assertion!("Has #{desc}", bboxes: bboxes)
    end
  end
end

#assert_text(*args, **opts) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/specbook/recorders/screenshot.rb', line 350

def assert_text(*args, **opts)
  super.tap do
    if Specbook::Recorders::Screenshot.current_example_name
      actual_text = args.find { |a| a.is_a?(String) || a.is_a?(Regexp) }
      if actual_text
        text_str = actual_text.to_s
        # Capybara confirmed this text exists — find the element containing it
        bboxes = capybara_text_bbox(text_str)
        Specbook::Recorders::Screenshot.record_assertion!("Has text: '#{text_str.truncate(60)}'", bboxes: bboxes)
      end
    end
  end
end