-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
.idea/ | ||
.idea/ | ||
.ruby-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
<img src="./images/jpg.jpg" alt="jpg"/> | ||
<img src="./images/png.png" alt="png"/> | ||
</body> | ||
</html> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} | ||
|
@@ -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 |