Skip to content

Commit

Permalink
Merge branch 'HubTran-use_natural_dimensions'
Browse files Browse the repository at this point in the history
  • Loading branch information
a0o committed Jun 18, 2018
2 parents 8559800 + 4b96bfc commit 15df347
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 74 deletions.
6 changes: 3 additions & 3 deletions lib/watir-get-image-content/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions spec/matchers/image_similarity.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions spec/matchers/image_size.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
<img src="./images/gif.gif" alt="gif"/>
<img src="./images/jpg.jpg" alt="jpg"/>
<img src="./images/png.png" alt="png"/>
<br/>
<img src="./images/gif.gif" width="260px" height="260px" alt="gif2"/>
<img src="./images/jpg.jpg" width="260px" height="260px" alt="jpg2"/>
<img src="./images/png.png" width="260px" height="260px" alt="png2"/>
</body>
</html>
51 changes: 11 additions & 40 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
Expand All @@ -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
Expand All @@ -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
61 changes: 30 additions & 31 deletions spec/watir-get-image-content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

0 comments on commit 15df347

Please sign in to comment.