Class: Waterpig::BrowserConsoleLogger
- Inherits:
-
Object
- Object
- Waterpig::BrowserConsoleLogger
- Includes:
- Singleton
- Defined in:
- lib/waterpig/browser-console-logger.rb
Instance Attribute Summary collapse
- #file ⇒ Object
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #bold(string) ⇒ Object
-
#emit_complex_table(hash, table) ⇒ Object
When they are a hash of hashes, used as follows: * The keys of the top-level hashes will be the leftmost column, “index”.
- #emit_header(string) ⇒ Object
- #emit_log(entry) ⇒ Object
-
#emit_simple_table(hash, table) ⇒ Object
Simple hashes emit as a two-column table, with keys making up the index column: { a: 1, b: 4, c: ‘foo’}.
-
#emit_table(hash) ⇒ Object
Tables are either a simple hash, or a hash of hashes.
- #handle_example(page, example) ⇒ Object
Instance Attribute Details
#file ⇒ Object
10 11 12 |
# File 'lib/waterpig/browser-console-logger.rb', line 10 def file @file ||= File.new(path, "w") end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/waterpig/browser-console-logger.rb', line 8 def path @path end |
Class Method Details
.configure(config) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/waterpig/browser-console-logger.rb', line 102 def configure(config) config.add_setting :waterpig_browser_console_log_path, :default => nil config.add_setting :waterpig_log_browser_console, :default => ENV['LOG_BROWSER_CONSOLE'] config.before(:suite) do if config.waterpig_log_browser_console config.waterpig_browser_console_log_path ||= Rails.root.join("log/#{Rails.env}_browser_console.log") config.waterpig_clearable_logs << 'test_browser_console' end end config.after(:each,:type => proc{|value| config.waterpig_log_types && config.waterpig_log_types.include?(value) }) do |example| logger = Waterpig::BrowserConsoleLogger.instance logger.path = config.waterpig_browser_console_log_path logger.handle_example(page, example) if config.waterpig_log_browser_console end end |
Instance Method Details
#bold(string) ⇒ Object
97 98 99 |
# File 'lib/waterpig/browser-console-logger.rb', line 97 def bold(string) "\e[1m#{string}\e[0m" end |
#emit_complex_table(hash, table) ⇒ Object
When they are a hash of hashes, used as follows:
* The keys of the top-level hashes will be the leftmost column, "index".
* The merged keys of the inner hashes will be the column headers
* The values of the inner hashes will fill the table cells.
So for example:
{ foo: { a: 1, b: 2 },
bar: { a: 5, c: 3 }}
Will render as:
+-------+---+---+---+
| index | a | b | c |
+-------+---+---+---+
| foo | 1 | 2 | |
+-------+---+---+---+
| bar | 5 | | 3 |
+-------+---+---+---+
78 79 80 81 82 83 84 |
# File 'lib/waterpig/browser-console-logger.rb', line 78 def emit_complex_table(hash, table) keys = hash.reduce([]){ |memo, arr| memo + arr[1].keys }.uniq table.head = [ "index" ] + keys hash.each do |name, row| table.rows << [ name ] + keys.map{ |key| row.fetch(key, nil)} end end |
#emit_header(string) ⇒ Object
14 15 16 |
# File 'lib/waterpig/browser-console-logger.rb', line 14 def emit_header(string) file.write("#{bold(string)}\n") end |
#emit_log(entry) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/waterpig/browser-console-logger.rb', line 18 def emit_log(entry) file.write( entry['time'] + "\n") if entry['type'] == 'table' emit_table(entry['value']) else file.write(entry['value'].to_s + "\n\n") end end |
#emit_simple_table(hash, table) ⇒ Object
Simple hashes emit as a two-column table, with keys making up the index column:
{ a: 1, b: 4, c: 'foo'}
Will render as:
+-------+--------+
| index | Values |
+-------+--------+
| a | 1 |
+-------+--------+
| b | 4 |
+-------+--------+
| c | 'foo' |
+-------+--------+
54 55 56 57 58 59 |
# File 'lib/waterpig/browser-console-logger.rb', line 54 def emit_simple_table(hash, table) table.head = [ "index", "values" ] hash.each do | key, val | table.rows << [ key, val ] end end |
#emit_table(hash) ⇒ Object
Tables are either a simple hash, or a hash of hashes. See documentation on emit_simple_table and emit_complex_table.
29 30 31 32 33 34 35 36 37 |
# File 'lib/waterpig/browser-console-logger.rb', line 29 def emit_table(hash) table = Text::Table.new if hash.values.any?{ |val| val.is_a?(Hash) } emit_complex_table(hash, table) else emit_simple_table(hash, table) end @file.write(table.to_s + "\n\n") end |
#handle_example(page, example) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/waterpig/browser-console-logger.rb', line 86 def handle_example(page, example) emit_header "Browser console for #{example.full_description}" console_entries = page.evaluate_script("console.history"); if console_entries console_entries.each do |entry| logger.emit_log(entry) end end end |