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 @@
+
+
+
+