From c463fdc8a77d49c11a7ae55134e3adc5fba0916f Mon Sep 17 00:00:00 2001 From: Gabriel Zayas Date: Tue, 27 Jun 2023 19:58:11 +0900 Subject: [PATCH] Refactor the flush method --- lib/magic_test/support.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/magic_test/support.rb b/lib/magic_test/support.rb index 9b1e3eb..0d71b82 100644 --- a/lib/magic_test/support.rb +++ b/lib/magic_test/support.rb @@ -28,12 +28,14 @@ def flush 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 + + if output.any? + puts + puts "JavaScript recorded on the front-end looks like this:" + puts output + puts + puts "(writing that to `#{filepath}`.)" + output.each do |last| chunks.first << indentation + "#{last["action"]} #{last["target"]}#{last["options"]}" + "\n" @test_lines_written += 1 @@ -42,12 +44,16 @@ def flush File.open(filepath, "w") do |file| file.puts(contents) end + # clear the testing output now. empty_cache + true else - puts "`window.testingOutput` was empty in the browser. Something must be wrong on the browser side." + puts + puts "No Javascript was recorded, please try again." + puts + false end - true end def ok