Module: Symilaa::ComparisonSupport

Defined in:
lib/symilaa/comparison_support.rb

Instance Method Summary collapse

Instance Method Details

#add_text_to_image(text, image) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/symilaa/comparison_support.rb', line 14

def add_text_to_image text, image
  txt = Magick::Draw.new
  image.annotate(txt, 0, 0, 0, 50, text) {
    txt.gravity = Magick::SouthGravity
    txt.pointsize = 25
    txt.stroke = '#ff0000'
    txt.fill = '#ff0000'
    txt.font_weight = Magick::BoldWeight
  }
end

#auto_updating_browser?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/symilaa/comparison_support.rb', line 87

def auto_updating_browser?
  %w[chrome firefox safari].include? browser_name
end

#base_dirObject



10
11
12
# File 'lib/symilaa/comparison_support.rb', line 10

def base_dir
  @base_dir || 'artifacts'
end

#base_dir=(dir) ⇒ Object



6
7
8
# File 'lib/symilaa/comparison_support.rb', line 6

def base_dir= dir
  @base_dir = dir
end

#browser_infoObject



55
56
57
# File 'lib/symilaa/comparison_support.rb', line 55

def browser_info
  @browser_info ||= page.driver.browser.capabilities
end

#browser_nameObject



59
60
61
# File 'lib/symilaa/comparison_support.rb', line 59

def browser_name
  browser_info.browser_name
end

#browser_versionObject



63
64
65
# File 'lib/symilaa/comparison_support.rb', line 63

def browser_version
  browser_info.version
end

#check_screen_against_reference_shot(target_sub_directory, unique_filename_required = true, retries = 1) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/symilaa/comparison_support.rb', line 132

def check_screen_against_reference_shot target_sub_directory, unique_filename_required = true, retries = 1
  create_directories %w[screenshots_generated_this_run differences_in_screenshots_this_run]
  create_screenshot_directory target_sub_directory

  generated_screenshot_path = create_screenshot_filename target_sub_directory, unique_filename_required
  screenshot_name           = File.basename generated_screenshot_path
  reference_path            = File.join reference_screenshots, target_sub_directory, scenario_name, screenshot_name
  difference_file           = difference_file_path generated_screenshot_path

  retries.times do 
    save_screenshot generated_screenshot_path
    break if same? reference_path, generated_screenshot_path
    sleep 1
  end

  unless same? reference_path, generated_screenshot_path
    FileUtils.mkdir_p File.dirname(difference_file)
    produce_gif_showing_the_difference_between reference_path, generated_screenshot_path, difference_file

    fail "Some of the screenshots did not match"
  end
end

#create_directories(directories) ⇒ Object



49
50
51
52
53
# File 'lib/symilaa/comparison_support.rb', line 49

def create_directories directories
  directories.each do |dir|
    FileUtils.mkdir_p File.join(base_dir, dir)
  end
end

#create_screenshot_directory(sub_directory) ⇒ Object



111
112
113
# File 'lib/symilaa/comparison_support.rb', line 111

def create_screenshot_directory sub_directory
  FileUtils.mkdir_p(File.join screenshots_generated_this_run_dir, sub_directory, scenario_name)
end

#create_screenshot_filename(sub_directory, unique_filename_required) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/symilaa/comparison_support.rb', line 115

def create_screenshot_filename sub_directory, unique_filename_required
  file_name = "#{browser_info.browser_name.capitalize.parameterize}#{version_if_required}_#{browser_info.platform.capitalize}_#{title}"
  if unique_filename_required
    get_unique_file_name "#{screenshots_generated_this_run_dir}/#{sub_directory}#{scenario_name}/#{file_name}", '.png'
  else
    get_file_name "#{screenshots_generated_this_run_dir}/#{sub_directory}#{scenario_name}/#{file_name}", '.png'
  end
end

#difference_file_path(generated_screenshot_path) ⇒ Object



128
129
130
# File 'lib/symilaa/comparison_support.rb', line 128

def difference_file_path generated_screenshot_path
  "#{generated_screenshot_path}_difference.gif".gsub('screenshots_generated_this_run', 'differences_in_screenshots_this_run')
end

#differences_in_screenshots_this_run_dirObject



103
104
105
# File 'lib/symilaa/comparison_support.rb', line 103

def differences_in_screenshots_this_run_dir
  File.join base_dir, 'differences_in_screenshots_this_run'
end

#get_file_name(path, extension) ⇒ Object



44
45
46
# File 'lib/symilaa/comparison_support.rb', line 44

def get_file_name path, extension
  "#{path}#{extension}"
end

#get_unique_file_name(path, extension, inc = 0) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/symilaa/comparison_support.rb', line 32

def get_unique_file_name path, extension, inc=0
  inc += 1

  new_path = "#{path}#{inc}#{extension}"

  if File.exists?("#{new_path}")
    get_unique_file_name path, extension, inc
  else
    new_path
  end
end

#produce_gif_showing_the_difference_between(reference_shot, test_shot, output_file) ⇒ Object



25
26
27
28
29
30
# File 'lib/symilaa/comparison_support.rb', line 25

def produce_gif_showing_the_difference_between reference_shot, test_shot, output_file
  gif = Magick::ImageList.new(reference_shot, test_shot)
  add_text_to_image "Test Screenshot", gif[1]
  gif.delay = 100
  gif.write(output_file)
end

#reference_screenshotsObject



95
96
97
# File 'lib/symilaa/comparison_support.rb', line 95

def reference_screenshots
  File.join base_dir, 'reference_screenshots'
end

#same?(reference_file, test_file) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/symilaa/comparison_support.rb', line 80

def same? reference_file, test_file
  reference_screenshot = Image.new reference_file
  generated_screenshot = Image.new test_file

  reference_screenshot.similar_to? generated_screenshot
end

#save_screenshot(screenshot_path) ⇒ Object



124
125
126
# File 'lib/symilaa/comparison_support.rb', line 124

def save_screenshot screenshot_path
  page.driver.browser.save_screenshot screenshot_path
end

#scenario_nameObject



107
108
109
# File 'lib/symilaa/comparison_support.rb', line 107

def scenario_name
  @gherkin_statement.parameterize.underscore if @gherkin_statement
end

#screenshots_generated_this_run_dirObject



99
100
101
# File 'lib/symilaa/comparison_support.rb', line 99

def screenshots_generated_this_run_dir
  File.join base_dir, 'screenshots_generated_this_run'
end

#titleObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/symilaa/comparison_support.rb', line 67

def title
  if page.respond_to?(:title)
    page.title.parameterize.underscore
  else
    find('title').text.parameterize.underscore
  end
rescue Capybara::ElementNotFound => e
  Capybara.ignore_hidden_elements = false
  value = find('title').native.text.parameterize.underscore
  Capybara.ignore_hidden_elements = true
  value
end

#version_if_requiredObject



91
92
93
# File 'lib/symilaa/comparison_support.rb', line 91

def version_if_required
  "_#{browser_info.version.gsub('.', '-')}" unless auto_updating_browser? 
end