Is there a way I can compare screenshots automatically

1,715 views
Skip to first unread message

ankur jain

unread,
Aug 28, 2012, 6:43:48 AM8/28/12
to calaba...@googlegroups.com
Hi,

I want to ensure that the screen that is being displayed at the time of running automation cases is exactly same to what I received when I actually wrote down the scripts.

I assume this can be done by image comparison. However, not sure as to how this can be achieved using calabash?

Please provide details if this is achievable. 

Thanks!! 

JitG

unread,
Aug 28, 2012, 12:08:05 PM8/28/12
to calaba...@googlegroups.com

Hi,

I've ha some success using ChunkyPNG and the inbuilt screenshot functionality of calabash:

http://jeffkreeftmeijer.com/2011/comparing-images-and-creating-image-diffs/

I’ve managed to use it to compare two pngs and the results are fairly accurate.

I only had to come up with 4 lines of code and that was just to generate the images to compare.

But be warned its pretty slow...

JitG

Krukow

unread,
Aug 28, 2012, 12:30:01 PM8/28/12
to calaba...@googlegroups.com

On Tuesday, August 28, 2012 6:08:05 PM UTC+2, JitG wrote:

Hi,

I've ha some success using ChunkyPNG and the inbuilt screenshot functionality of calabash:

http://jeffkreeftmeijer.com/2011/comparing-images-and-creating-image-diffs/

I’ve managed to use it to compare two pngs and the results are fairly accurate.


Cool, This sounds interesting. We will definitely check this out.
 

Krukow

unread,
Aug 28, 2012, 12:32:24 PM8/28/12
to calaba...@googlegroups.com
Is it really necessary to display the exact same screen? Image comparison is quite fragile.

Normally I would recommend image diff as a last resort. You could check for existence of texts, UI components, and so on, and generate an image for reference. But perhaps this is not enough in your case?

Peter Hodgson

unread,
Aug 28, 2012, 1:01:06 PM8/28/12
to calaba...@googlegroups.com
Sounds similar to Zucchini: http://www.zucchiniframework.org/ You might want to take a look at their approach for additional inspiration. They seem to have reasonable success with it.

Karl Krukow

unread,
Aug 28, 2012, 1:07:42 PM8/28/12
to calaba...@googlegroups.com
Yes, I remember looking at this file before:


Just never got round to trying it out. Eventually, we like to add some kind of image comparison support.

/K

Nicholas Albion

unread,
Aug 29, 2012, 2:28:07 AM8/29/12
to calaba...@googlegroups.com
I'd like to be able to compare a specific region of the screen to a known-good screenshot, allowing for the image to be offset by a few pixels horizontally/vertically.

Nicholas Albion

unread,
Aug 30, 2012, 1:30:47 AM8/30/12
to calaba...@googlegroups.com

This is working for me (the partial search is a lot slower and I'm not that sure that it is actually working):


# Uses imagemagik to find the compare the current screenshot against a known-good

Then /^the current screen should resemble the screenshot "([^\"]+)"$/ do | known_good |

  new_screenshot = screenshot()

  diff_img = new_screenshot.sub(/\.png$/, '_diff.png')

  out = `compare -metric AE \"#{new_screenshot}\" \"iphone/features/support/#{known_good}\" \"#{diff_img}\" 2>&1`

  FileUtils.rm( new_screenshot )  

  if( out == "0\n" )

    FileUtils.rm( diff_img )

  else

    embed( File.basename(diff_img), 'image/png')

    raise('Current screen differs from known-good')

  end

end


# Uses imagemagik to find the specified subimage within the current screenshot

# This is much slower than the full-screen comparison    

Then /^I should see the partial screen shot "([^\"]+)"$/ do | partial |

  new_screenshot = screenshot()

  diff_img = new_screenshot.sub(/\.png$/, '_diff.png')

  out = `compare -metric AE -subimage-search \"#{new_screenshot}\" \"iphone/features/support/#{partial}\" \"#{diff_img}\" 2>&1`

  FileUtils.rm( diff_img ) if File.exists?(diff_img)      # (It probably won't)

  if( out == "0\n" )

    FileUtils.rm( new_screenshot )  

  else

    embed( File.basename(new_screenshot), 'image/png')

    raise("Can not find partial screenshot within the current screen")

  end

end

Krukow

unread,
Aug 30, 2012, 2:35:23 PM8/30/12
to calaba...@googlegroups.com
Nice. I'll check this out at some point!

venkat b

unread,
Feb 12, 2013, 9:52:22 AM2/12/13
to calaba...@googlegroups.com, nal...@gmail.com
Hi All Good morning,
Can some body help me to solve the compare image problem.
I am using the following Code ChunkyPNG.If any difference in the Images,then it is working Perfectly,if there both image are same ,then I am getting an error.
 pixels  (total):     357120, pixels changed:     0
      undefined method `/' for nil:NilClass (NoMethodError)
      C:/features/step_definitions/compareimage.rb:32:in `/^CompareImages$/'
      features\banner.feature:15:in `Then CompareImages'

If both images are not identical then it is returning 
pixels (total):     357120, pixels changed:     105, image changed (%): 0.018231911940403384%.Please guide me to solve this


require 'chunky_png'
include ChunkyPNG::Color

Given /^CompareImages$/ do
new_screenshot = screenshot(options={:prefix=>"C:/features/", :name=>"suni"})
images = [
  ChunkyPNG::Image.from_file("#{new_screenshot}"),
  ChunkyPNG::Image.from_file('venkat123_0.png')
]

output = ChunkyPNG::Image.new(images.first.width, images.last.width, WHITE)

diff = []

images.first.height.times do |y|
  images.first.row(y).each_with_index do |pixel, x|
    unless pixel == images.last[x,y]
      score = Math.sqrt(
        (r(images.last[x,y]) - r(pixel)) ** 2 +
        (g(images.last[x,y]) - g(pixel)) ** 2 +
        (b(images.last[x,y]) - b(pixel)) ** 2
      ) / Math.sqrt(MAX ** 2 * 3)

      output[x,y] = grayscale(MAX - (score * MAX).round)
      diff << score
    end
  end
end

puts "pixels (total):     #{images.first.pixels.length}"
puts "pixels changed:     #{diff.length}"
puts "image changed (%): #{(diff.inject {|sum, value| sum + value} / images.first.pixels.length) * 100}%"

output.save('diff.png')

end
Reply all
Reply to author
Forward
0 new messages