Skip to content

Commit

Permalink
add #to_jpg, #to_jpg_base64
Browse files Browse the repository at this point in the history
  • Loading branch information
a0s committed Oct 14, 2016
1 parent 96e5b34 commit a6bafca
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
.idea/
.idea/
.ruby-*
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ browser.goto 'http://for_example.com/page_with_images'

browser.image(:index, 1).to_png_base64
browser.image(:alt, 'Alt text').to_png_base64
browser.image(:index, 1).to_jpg_base64
browser.image(:alt, 'Alt text').to_jpg_base64

File.write('png.png', browser.image(:index, 1).to_png)
File.write('jpg.jpg', browser.image(:index, 1).to_jpg)
```

## Contributing
Expand Down
29 changes: 23 additions & 6 deletions lib/watir-get-image-content/image.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
# encoding: utf-8

require 'base64'

module Watir
class Image

def to_png_base64
def to_x_base64(target)
assert_exists
js = %q{var canvas = document.createElement("canvas");
js = %Q{var canvas = document.createElement("canvas");
canvas.width = arguments[0].width;
canvas.height = arguments[0].height;
var ctx = canvas.getContext("2d");
ctx.drawImage(arguments[0], 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg|gif);base64,/, "");}
var dataURL = canvas.toDataURL("image/#{target}");
return dataURL.replace(/^data:image\\/(png|jpg|gif);base64,/, "");}
driver.execute_script js, @element
end

def to_png_base64
to_x_base64('png')
end

def to_jpg_base64
to_x_base64('jpg')
end

def to_png
Base64.decode64(self.to_png_base64)
end

def to_jpg
Base64.decode64(self.to_jpg_base64)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
<img src="./images/jpg.jpg" alt="jpg"/>
<img src="./images/png.png" alt="png"/>
</body>
</html>
</html>
53 changes: 45 additions & 8 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
require 'RMagick'
require 'rmagick'
require 'rspec/expectations'
require 'watir-get-image-content'
require 'rbconfig'

if RbConfig::CONFIG['host_os'] =~ /linux/
dir = '/usr/lib/chromium-browser/'
unless ENV['PATH'].include?(dir)
if File.exist?(dir)
ENV['PATH'] = "#{ENV['PATH']}#{File::PATH_SEPARATOR}#{dir}"
end
end
end

tmp = File.expand_path(File.join(__FILE__, %w(.. .. tmp)))
FileUtils.mkdir_p(tmp)

RSpec.configure do |spec|
spec.before(:all) do
@browser = Watir::Browser.new
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}"]
@browser = Watir::Browser.new :chrome, desired_capabilities: caps, args: args
@browser.goto "file://#{Dir.pwd}/spec/spec.html"
end

Expand All @@ -17,10 +37,27 @@
end
end

RSpec::Matchers.define :be_similar_to_file do |dst_file|
match do |src_base64|
src_img = Magick::Image.read_inline(src_base64)
dst_img = Magick::Image.read(dst_file)
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
Expand All @@ -29,7 +66,7 @@
@mse = mse
end

failure_message_for_should do |actual|
failure_message do |actual|
"expected that mse #{@diff_metric} would be less or equal #{@mse.to_f}"
end
end
end
71 changes: 55 additions & 16 deletions spec/watir-get-image-content_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
require "spec_helper"
require 'spec_helper'

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))
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

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 '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)
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))
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

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)
end
end

describe "watir-get-image-content" do
describe "#to_png_base64" do
it "return png image similar to the original png" do
src_file = "#{Dir.pwd}/spec/images/png.png".sub("file://", '')
enc_base64 = @browser.image(:alt, 'png').to_png_base64
expect(enc_base64).to be_similar_to_file(src_file)
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))
end

it "return png image similar to the original gif" do
src_file = "#{Dir.pwd}/spec/images/gif.gif".sub("file://", '')
enc_base64 = @browser.image(:alt, 'gif').to_png_base64
expect(enc_base64).to be_similar_to_file(src_file)
it 'from gif' do
expect(@browser.image(:alt, 'gif').to_jpg).to be_similar_to(File.join(Dir.pwd), %w(spec images gif.gif))
end

it "return png image similar to the original jpg with mse <= 0.0000001" do
src_file = "#{Dir.pwd}/spec/images/jpg.jpg".sub("file://", '')
enc_base64 = @browser.image(:alt, 'jpg').to_png_base64
expect(enc_base64).to be_similar_to_file(src_file).with_mse_less_then(0.0000001)
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)
end
end
end
end
4 changes: 2 additions & 2 deletions watir-get-image-content.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "watir-get-image-content"
spec.version = "0.0.2"
spec.version = "0.0.3"
spec.authors = ["orangeudav"]
spec.email = ["[email protected]"]
spec.description = %q{Get Watir::Image's content copy without external download}
Expand All @@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rmagick", '2.13.2'
spec.add_development_dependency "rmagick"
end

0 comments on commit a6bafca

Please sign in to comment.