diff --git a/lib/watir-get-image-content/image.rb b/lib/watir-get-image-content/image.rb index ebff8dc..53a8bdd 100644 --- a/lib/watir-get-image-content/image.rb +++ b/lib/watir-get-image-content/image.rb @@ -7,13 +7,13 @@ class Image def to_x_base64(target) assert_exists js = %Q{var canvas = document.createElement("canvas"); - canvas.width = arguments[0].width; - canvas.height = arguments[0].height; + canvas.width = arguments[1]; + canvas.height = arguments[2]; var ctx = canvas.getContext("2d"); ctx.drawImage(arguments[0], 0, 0); var dataURL = canvas.toDataURL("image/#{target}"); return dataURL.replace(/^data:image\\/(png|jpg|gif);base64,/, "");} - driver.execute_script js, @element + driver.execute_script js, @element, naturalwidth, naturalheight end def to_png_base64 diff --git a/spec/matchers/image_similarity.rb b/spec/matchers/image_similarity.rb new file mode 100644 index 0000000..b603ad1 --- /dev/null +++ b/spec/matchers/image_similarity.rb @@ -0,0 +1,36 @@ +module ImageSimilarity + RSpec::Matchers.define :be_similar_to do |dst| + match do |src| + base64_re = %r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}=+)$} + + src_img, dst_img = [src, dst].map do |data| + if src =~ base64_re + Magick::Image.read_inline(src) + elsif (File.exist?(src) rescue nil) + Magick::Image.read(src) + else + tmp = Tempfile.new + tmp.write(src) + tmp.flush + begin + Magick::Image.read(tmp.path) + ensure + tmp.close + tmp.unlink + end + end + end + + @diff_img, @diff_metric = src_img[0].compare_channel(dst_img[0], Magick::MeanSquaredErrorMetric) + @diff_metric <= @mse.to_f + end + + chain :with_mse_less_then do |mse| + @mse = mse + end + + failure_message do |actual| + "expected that mse #{@diff_metric} would be less or equal #{@mse.to_f}" + end + end +end diff --git a/spec/matchers/image_size.rb b/spec/matchers/image_size.rb new file mode 100644 index 0000000..c8662a6 --- /dev/null +++ b/spec/matchers/image_size.rb @@ -0,0 +1,33 @@ +module ImageSize + RSpec::Matchers.define :be_same_size_as do |dst| + match do |src| + base64_re = %r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}=+)$} + + src_img, dst_img = [src, dst].map do |data| + if src =~ base64_re + Magick::Image.read_inline(src) + elsif (File.exist?(src) rescue nil) + Magick::Image.read(src) + else + tmp = Tempfile.new + tmp.write(src) + tmp.flush + begin + Magick::Image.read(tmp.path) + ensure + tmp.close + tmp.unlink + end + end + end + + @src_width, @src_height = src_img[0].columns, src_img[0].rows + @dst_width, @dst_height = dst_img[0].columns, dst_img[0].rows + @src_width == @dst_width && @src_height == @dst_height + end + + failure_message do |actual| + "expected that size of src image (#{@src_width},#{@src_height}) would be same as of dst image (#{@dst_width},#{@dst_height})" + end + end +end diff --git a/spec/spec.html b/spec/spec.html index a2e50ad..31cef2f 100644 --- a/spec/spec.html +++ b/spec/spec.html @@ -5,5 +5,9 @@ gif jpg png +
+gif2 +jpg2 +png2 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 560a031..c377909 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,8 @@ require 'rspec/expectations' require 'watir-get-image-content' require 'rbconfig' +require 'matchers/image_similarity' +require 'matchers/image_size' if RbConfig::CONFIG['host_os'] =~ /linux/ dir = '/usr/lib/chromium-browser/' @@ -16,14 +18,17 @@ FileUtils.mkdir_p(tmp) RSpec.configure do |spec| + include ImageSimilarity + include ImageSize + spec.before(:all) do caps = Selenium::WebDriver::Remote::Capabilities.chrome - args =['--disable-notifications', - '--unsafely-treat-insecure-origin-as-secure', - '--disable-web-security', - '--allow-file-access', - '--allow-file-access-from-files', - "--user-data-dir=#{tmp}"] + args = ['--disable-notifications', + '--unsafely-treat-insecure-origin-as-secure', + '--disable-web-security', + '--allow-file-access', + '--allow-file-access-from-files', + "--user-data-dir=#{tmp}"] @browser = Watir::Browser.new :chrome, desired_capabilities: caps, args: args @browser.goto "file://#{Dir.pwd}/spec/spec.html" end @@ -36,37 +41,3 @@ @browser.refresh end end - -RSpec::Matchers.define :be_similar_to do |dst| - match do |src| - base64_re = %r{^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}=+)$} - src_img, dst_img = [src, dst].map do |data| - if src =~ base64_re - Magick::Image.read_inline(src) - elsif (File.exist?(src) rescue nil) - Magick::Image.read(src) - else - tmp = Tempfile.new - tmp.write(src) - tmp.flush - begin - Magick::Image.read(tmp.path) - ensure - tmp.close - tmp.unlink - end - end - end - - @diff_img, @diff_metric = src_img[0].compare_channel(dst_img[0], Magick::MeanSquaredErrorMetric) - @diff_metric <= @mse.to_f - end - - chain :with_mse_less_then do |mse| - @mse = mse - end - - failure_message do |actual| - "expected that mse #{@diff_metric} would be less or equal #{@mse.to_f}" - end -end diff --git a/spec/watir-get-image-content_spec.rb b/spec/watir-get-image-content_spec.rb index 3188865..3f94676 100644 --- a/spec/watir-get-image-content_spec.rb +++ b/spec/watir-get-image-content_spec.rb @@ -2,50 +2,49 @@ describe 'watir-get-image-content' do describe '#to_png_base64' do - it 'from png' do - expect(@browser.image(:alt, 'png').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) - end - - it 'from gif' do - expect(@browser.image(:alt, 'gif').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) + describe 'original size' do + it { expect(@browser.image(:alt, 'png').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) } + it { expect(@browser.image(:alt, 'gif').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) } + it { expect(@browser.image(:alt, 'jpg').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) } end - it 'from jpg' do - expect(@browser.image(:alt, 'jpg').to_png_base64). - to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) - end - end - - describe '#to_png' do - it 'from png' do - expect(@browser.image(:alt, 'png').to_png).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) - end + describe 'resized' do + it { expect(@browser.image(:alt, 'png2').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) } + it { expect(@browser.image(:alt, 'png2').to_png_base64).to be_same_size_as(File.join(Dir.pwd), %w(spec images png.png)) } - it 'from gif' do - expect(@browser.image(:alt, 'gif').to_png).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) - end + it { expect(@browser.image(:alt, 'gif2').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) } + it { expect(@browser.image(:alt, 'gif2').to_png_base64).to be_same_size_as(File.join(Dir.pwd), %w(spec images gif.gif)) } - it 'from jpg' do - expect(@browser.image(:alt, 'jpg').to_png). - to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) + it { expect(@browser.image(:alt, 'jpg2').to_png_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) } + it { expect(@browser.image(:alt, 'jpg2').to_png_base64).to be_same_size_as(File.join(Dir.pwd), %w(spec images jpg.jpg)) } end end describe '#to_jpg_base64' do - it 'from png' do - expect(@browser.image(:alt, 'png').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) + describe 'original size' do + it { expect(@browser.image(:alt, 'png').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) } + it { expect(@browser.image(:alt, 'gif').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) } + it { expect(@browser.image(:alt, 'jpg').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) } end - it 'from gif' do - expect(@browser.image(:alt, 'gif').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) - end + describe 'resized' do + it { expect(@browser.image(:alt, 'png').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) } + it { expect(@browser.image(:alt, 'png').to_jpg_base64).to be_same_size_as(File.join(Dir.pwd), %w(spec images png.png)) } - it 'from jpg' do - expect(@browser.image(:alt, 'jpg').to_jpg_base64). - to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) + it { expect(@browser.image(:alt, 'gif').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) } + it { expect(@browser.image(:alt, 'gif').to_jpg_base64).to be_same_size_as(File.join(Dir.pwd), %w(spec images gif.gif)) } + + it { expect(@browser.image(:alt, 'jpg').to_jpg_base64).to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) } + it { expect(@browser.image(:alt, 'jpg').to_jpg_base64).to be_same_size_as(File.join(Dir.pwd), %w(spec images jpg.jpg)) } end end + describe '#to_png' do + it { expect(@browser.image(:alt, 'png').to_png).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) } + it { expect(@browser.image(:alt, 'gif').to_png).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif)) } + it { expect(@browser.image(:alt, 'jpg').to_png).to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) } + end + describe '#to_jpg' do it 'from png' do expect(@browser.image(:alt, 'png').to_jpg).to be_similar_to(File.join(Dir.pwd), %w(spec images png.png)) @@ -57,7 +56,7 @@ it 'from jpg' do expect(@browser.image(:alt, 'jpg').to_jpg). - to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) + to be_similar_to(File.join(Dir.pwd), %w(spec images jpg.jpg)).with_mse_less_then(0.0000001) end end end