lib/magic_test/support.rb in magic_test-0.0.3 vs lib/magic_test/support.rb in magic_test-0.0.4
- old
+ new
@@ -1,11 +1,14 @@
module MagicTest
module Support
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(':')
+
+ # TODO this feels like it's going to end up burning people who have other support files in `test` or `spec` that don't include `helper` in the name.
+ filepath, line = caller.select { |s| s.include?("/test/") || s.include?("/spec/") }.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
@@ -79,10 +82,15 @@
end
return true
end
def empty_cache
- page.evaluate_script("sessionStorage.setItem('testingOutput', JSON.stringify([]))")
+ begin
+ page.evaluate_script("sessionStorage.setItem('testingOutput', JSON.stringify([]))")
+ rescue Capybara::NotSupportedByDriverError => e
+ # TODO we need to add more robust instructions for this.
+ raise "You need to configure this test (or your test suite) to run in a real browser (Chrome, Firefox, etc.) in order for Magic Test to work. It also needs to run in non-headless mode if `ENV['MAGIC_TEST'].present?`"
+ end
end
def magic_test
empty_cache
@test_lines_written = 0