Module: MagicTest::Support

Defined in:
lib/magic_test/support.rb

Instance Method Summary collapse

Instance Method Details

#assert_selected_existsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/magic_test/support.rb', line 3

def assert_selected_exists
  selected_text = page.evaluate_script("window.selectedText()")
  return if selected_text.blank?
  filepath, line = caller.select { |s| s.include?("/test/") }.reject { |s| s.include?("helper") }.first.split(':')
  contents = File.open(filepath).read.lines
  chunks = contents.each_slice(line.to_i - 1 + @test_lines_written).to_a
  indentation = chunks[1].first.match(/^(\s*)/)[0]
  chunks.first << indentation + "assert(page.has_content?('#{selected_text.gsub("'", "\\\\'")}'))" + "\n"
  @test_lines_written += 1
  contents = chunks.flatten.join
  File.open(filepath, 'w') do |file|
    file.puts(contents)
  end
end

#empty_cacheObject



83
84
85
# File 'lib/magic_test/support.rb', line 83

def empty_cache
  page.evaluate_script("sessionStorage.setItem('testingOutput', JSON.stringify([]))")
end

#flushObject



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
# File 'lib/magic_test/support.rb', line 38

def flush
  filepath, line = caller.select { |s| s.include?("/test/") }.reject { |s| s.include?("helper") }.first.split(':')
  contents = File.open(filepath).read.lines
  chunks = contents.each_slice(line.to_i - 1 + @test_lines_written).to_a
  indentation = chunks[1].first.match(/^(\s*)/)[0]
  output = page.evaluate_script("JSON.parse(sessionStorage.getItem('testingOutput'))")
  puts
  puts "javascript recorded on the front-end looks like this:"
  puts output
  puts
  puts "(writing that to `#{filepath}`.)"
  if output
    output.each do |last|
      chunks.first << indentation + "#{last['action']} #{last['target']}#{last['options']}" + "\n"
      @test_lines_written += 1
    end
    contents = chunks.flatten.join
    File.open(filepath, 'w') do |file|
      file.puts(contents)
    end
    # clear the testing output now.
    empty_cache
  else
    puts "`window.testingOutput` was empty in the browser. Something must be wrong on the browser side."
  end
  return true
end

#get_lastObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/magic_test/support.rb', line 22

def get_last
  history_lines = Readline::HISTORY.to_a.last(20)
  i = 2
  last = history_lines.last(2).first
  last_block = [last]
  if last == 'end' || last.first(4) == 'end '
    i += 1
    last_block.unshift(history_lines.last(i).first)
    until !last_block.first.match(/^(\s+)/)&[0] do
      i += 1
      last_block.unshift(history_lines.last(i).first)
    end
  end
  return last_block
end

#magic_testObject



87
88
89
90
91
92
93
94
95
# File 'lib/magic_test/support.rb', line 87

def magic_test
  empty_cache
  @test_lines_written = 0
  begin
    binding.pry
  rescue
    retry
  end
end

#okObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/magic_test/support.rb', line 66

def ok
  filepath, line = caller.select { |s| s.include?("/test/") }.reject { |s| s.include?("helper") }.first.split(':')
  puts "(writing that to `#{filepath}`.)"
  contents = File.open(filepath).read.lines
  chunks = contents.each_slice(line.to_i - 1 + @test_lines_written).to_a
  indentation = chunks[1].first.match(/^(\s*)/)[0]
  get_last.each do |last|
    chunks.first << indentation + last + "\n"
    @test_lines_written += 1
  end
  contents = chunks.flatten.join
  File.open(filepath, 'w') do |file|
    file.puts(contents)
  end
  return true
end

#track_keystrokesObject



18
19
20
# File 'lib/magic_test/support.rb', line 18

def track_keystrokes
  page.evaluate_script("trackKeystrokes()")
end