Module: WithClues::Method

Defined in:
lib/with_clues/method.rb

Constant Summary collapse

@@clue_classes =
{
  require_page: [
    WithClues::BrowserLogs,
    WithClues::Html,
  ],
  custom: []
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.use_custom_clue(klass) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/with_clues/method.rb', line 45

def self.use_custom_clue(klass)
  dump_method = klass.instance_method(:dump)
  analysis = WithClues::Private::CustomClueMethodAnalysis.from_method(dump_method)
  if analysis.standard_implementation?
    @@clue_classes[:custom] << klass
  elsif analysis.requires_page_object?
    @@clue_classes[:require_page] << klass
  else
    analysis.raise_exception!
  end
end

Instance Method Details

#with_clues(context = nil, &block) ⇒ Object

Wrap any assertion with this method to get more useful context and diagnostics when a test is unexpectedly failing



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/with_clues/method.rb', line 18

def with_clues(context=nil, &block)
  notifier = WithClues::Notifier.new($stdout)
  captured_logs = []
  if defined?(page) && page.respond_to?(:on)
    begin
      page.on("console", ->(msg) { captured_logs << msg.text })
    rescue => ex
      raise ex
      notifier.notify "'page' was defined and responds to #on, however invoking it generated an exception: #{ex}"
    end
  end
  block.()
  notifier.notify "A passing test has been wrapped with `with_clues`. You should remove the call to `with_clues`"
rescue Exception => ex
  notifier.notify context
  @@clue_classes[:custom].each do |klass|
    klass.new.dump(notifier, context: context)
  end
  if defined?(page)
    notifier.notify "Test failed: #{ex.message}"
    @@clue_classes[:require_page].each do |klass|
      klass.new.dump(notifier, context: context, page: page, captured_logs: captured_logs)
    end
  end
  raise ex
end