Class: Screenpress::Saver

Inherits:
Object
  • Object
show all
Defined in:
lib/screenpress/saver.rb

Defined Under Namespace

Classes: Proxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, capybara = nil, page = nil) ⇒ Saver

Returns a new instance of Saver.



7
8
9
10
11
# File 'lib/screenpress/saver.rb', line 7

def initialize(filename, capybara = nil, page = nil)
  @capybara = capybara || Capybara
  @page     = page || @capybara.page
  @filename = filename
end

Instance Attribute Details

#capybaraObject (readonly)

Returns the value of attribute capybara.



5
6
7
# File 'lib/screenpress/saver.rb', line 5

def capybara
  @capybara
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/screenpress/saver.rb', line 5

def filename
  @filename
end

#pageObject (readonly)

Returns the value of attribute page.



5
6
7
# File 'lib/screenpress/saver.rb', line 5

def page
  @page
end

Instance Method Details

#ensure_directoryObject



44
45
46
47
48
# File 'lib/screenpress/saver.rb', line 44

def ensure_directory
  folder = File.dirname(filename)
  return if File.directory?(folder)
  FileUtils.mkdir_p(folder)
end

#saveObject



13
14
15
16
17
# File 'lib/screenpress/saver.rb', line 13

def save
  # if current_path empty then nothing to screen shot as browser has not loaded any URL
  return false if capybara.current_path.to_s.empty?
  save_screenshot
end

#save_screenshotObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/screenpress/saver.rb', line 19

def save_screenshot
  tmp_file = self.tmp_filename

  # settings to nil because of issues in some version of capybara
  old_path = Capybara.save_and_open_page_path
  Capybara.save_and_open_page_path = nil

  ensure_directory

  return false unless Screenpress::Saver::Proxy.save!(capybara.current_driver, page.driver, tmp_file)

  # is it different?
  compare = Screenpress::Compare.new(filename, tmp_file, Screenpress.config.threshold)
  if compare.same?
    File.delete(tmp_file)
  else
    FileUtils.mv(tmp_file, filename)
  end
  return true

rescue Exception => e
  File.delete(tmp_file) if tmp_file && File.exists?(tmp_file)
  raise e
end

#tmp_filenameObject



50
51
52
53
54
# File 'lib/screenpress/saver.rb', line 50

def tmp_filename
  tmp_dir  = Screenpress.config.full_tmp_path.to_s
  tmp_file = "#{tmp_dir}/screenpress_#{File.basename(filename)}"
  tmp_file
end