Module: Superbara::DSL

Defined in:
lib/superbara/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



2
3
4
# File 'lib/superbara/dsl.rb', line 2

def self.included(includer)
  puts "Superbara::DSL included in #{includer.inspect}"
end

Instance Method Details

#debug(exception_occurred: false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/superbara/dsl.rb', line 82

def debug(exception_occurred:false)
  return if ENV['SUPERBARA_FRONTEND'] == "noninteractive"
  debug_help = """
  c - continue to the next debug breakpoint
  s - step to the next line
  r - retry running
  h - help on commands available
  q - exit to shell"""

  debug_header_prefix = "== DEBUG "
  debug_header_suffix = "=" * (IO.console.winsize.last - debug_header_prefix.size)

  puts """
#{debug_header_prefix}#{debug_header_suffix}
#{debug_help}
""".colorize(:light_green)

  $supress_pry_whereami = true if exception_occurred
  Pry.start(binding.of_caller(1))
end

#wait(seconds, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/superbara/dsl.rb', line 6

def wait(seconds, &block)
  def wait_formatted_output(status, took_delta)
    word, color = if status
      ["ok", :green]
    else
      ["FAIL", :red]
    end

    puts "#{word.colorize(color)} (took #{(took_delta)}s)"
  end
  seconds = seconds.to_f

  print "--> waiting max #{seconds}s "
  started_at = Time.now

  previous_capybara_default_max_wait_time = Capybara.default_max_wait_time
  block_value = nil
  exception = nil
  timed_out = false
  begin
    Capybara.default_max_wait_time = seconds
    Timeout::timeout (seconds+0.1) do
      block_value = block.call
    end
  rescue Timeout::Error => ex
    timed_out = true
  rescue Exception => ex
    exception = ex
  end

  took_delta = (Time.now - started_at).floor(2)
  Capybara.default_max_wait_time = previous_capybara_default_max_wait_time

  if exception || timed_out
    wait_formatted_output false, took_delta

    additional_started_at = Time.now
    additional_block_value = nil
    additional_exception = nil
    puts "  testing if waiting for 2 seconds more would help.."
    begin
      Capybara.default_max_wait_time = 2
      additional_block_value = block.call
    rescue Exception => ex
      additional_exception = ex
    end
    additional_took_delta = (Time.now - additional_started_at).floor(2)
    suggested_wait_value = (seconds + additional_took_delta).floor(2)

    if additional_exception || additional_block_value.nil?
      puts "  ..did not help."
    else
      puts "  ..setting wait to >#{suggested_wait_value} would help here?"
    end

    if exception
      raise exception
    else
      raise "capybara timed out"
    end
  end

  if block_value == false
    wait_formatted_output false, took_delta
    raise "wait condition was falsy"
  end

  if block_value == nil
    wait_formatted_output false, took_delta
    raise "wait condition was nil"
  end

  self.wait_formatted_output true, took_delta
  return block_value
end

#wait_formatted_output(status, took_delta) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/superbara/dsl.rb', line 7

def wait_formatted_output(status, took_delta)
  word, color = if status
    ["ok", :green]
  else
    ["FAIL", :red]
  end

  puts "#{word.colorize(color)} (took #{(took_delta)}s)"
end