Skip to content

Instantly share code, notes, and snippets.

@mashhoodr
Last active November 22, 2017 19:06
Show Gist options
  • Select an option

  • Save mashhoodr/9512282 to your computer and use it in GitHub Desktop.

Select an option

Save mashhoodr/9512282 to your computer and use it in GitHub Desktop.

Revisions

  1. mashhoodr revised this gist May 29, 2014. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions compare.rb
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,9 @@ def compare_image(image1, image2)
    g(pixel) + g(images.last[x,y]) - 2 * [g(pixel), g(images.last[x,y])].min,
    b(pixel) + b(images.last[x,y]) - 2 * [b(pixel), b(images.last[x,y])].min
    )
    if images.last[x,y] == 255

    # Count all the black pixels
    if r(cropped_images.last[x,y]) == 0 && b(cropped_images.last[x,y]) == 0 && g(cropped_images.last[x,y]) == 0
    count = count + 1
    end
    end
    @@ -93,4 +95,4 @@ def setup_comparison_url(url, percentageVariance)

    Then(/^I expect atmost "(.*?)" difference when comparing with url "(.*?)"$/) do |percentageVariance, url|
    setup_comparison_url(url, percentageVariance.to_i)
    end
    end
  2. mashhoodr revised this gist Mar 12, 2014. 1 changed file with 0 additions and 13 deletions.
    13 changes: 0 additions & 13 deletions compare.rb
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,3 @@
    #
    # needs the oily_png gem as well.
    #
    # Defined macros:
    # Then I compare the screen with "login_screen.png"
    # Then I expect atmost "2%" difference when comparing with "login_screen_fail.png"
    # Then I compare the screen with url "http://dev.marketlytics.com/login_screen.png"
    # Then I expect atmost "2%" difference when comparing with url "http://dev.marketlytics.com/login_screen_fail.png"
    #
    # For local file comparisons place your files under a folder called screens
    #


    require 'oily_png'
    require 'open-uri'
    include ChunkyPNG::Color
  3. mashhoodr revised this gist Mar 12, 2014. 1 changed file with 28 additions and 3 deletions.
    31 changes: 28 additions & 3 deletions compare.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,16 @@
    #
    # needs the oily_png gem as well.
    #
    # Defined macros:
    # Then I compare the screen with "login_screen.png"
    # Then I expect atmost "2%" difference when comparing with "login_screen_fail.png"
    # Then I compare the screen with url "http://dev.marketlytics.com/login_screen.png"
    # Then I expect atmost "2%" difference when comparing with url "http://dev.marketlytics.com/login_screen_fail.png"
    #
    # For local file comparisons place your files under a folder called screens
    #


    require 'oily_png'
    require 'open-uri'
    include ChunkyPNG::Color
    @@ -44,17 +57,25 @@ def get_screenshot_name(folder, fileName)
    foundName
    end

    def setup_comparison(fileName, percentageVariance)
    def setup_comparison(fileName, percentageVariance, forNotCase = false)
    screenshotFileName = "compare_#{fileName}"
    screenshot({ :prefix => "screens/", :name => screenshotFileName })

    screenshotFileName = get_screenshot_name("screens/", screenshotFileName)
    changed = compare_image(fileName, screenshotFileName)
    FileUtils.rm("screens/#{screenshotFileName}")

    if changed.to_i > percentageVariance
    assert = true
    if forNotCase
    assert = changed.to_i < percentageVariance
    else
    assert = changed.to_i > percentageVariance
    end

    if assert
    fail(msg="Error. The screen shot was different from the source file. Difference: #{changed.to_i}%")
    end

    end

    def setup_comparison_url(url, percentageVariance)
    @@ -75,10 +96,14 @@ def setup_comparison_url(url, percentageVariance)
    setup_comparison_url(url, 0)
    end

    Then(/^the screen should not match with "(.*?)"$/) do |fileName|
    setup_comparison(fileName, 0, true)
    end

    Then(/^I expect atmost "(.*?)" difference when comparing with "(.*?)"$/) do |percentageVariance, fileName|
    setup_comparison(fileName, percentageVariance.to_i)
    end

    Then(/^I expect atmost "(.*?)" difference when comparing with url "(.*?)"$/) do |percentageVariance, url|
    setup_comparison_url(url, percentageVariance.to_i)
    end
    end
  4. mashhoodr created this gist Mar 12, 2014.
    84 changes: 84 additions & 0 deletions compare.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    require 'oily_png'
    require 'open-uri'
    include ChunkyPNG::Color

    def starts_with(item, prefix)
    prefix = prefix.to_s
    item[0, prefix.length] == prefix
    end

    # compares two images on disk, returns the % difference
    def compare_image(image1, image2)
    images = [
    ChunkyPNG::Image.from_file("screens/#{image1}"),
    ChunkyPNG::Image.from_file("screens/#{image2}")
    ]
    count=0
    images.first.height.times do |y|
    images.first.row(y).each_with_index do |pixel, x|

    images.last[x,y] = rgb(
    r(pixel) + r(images.last[x,y]) - 2 * [r(pixel), r(images.last[x,y])].min,
    g(pixel) + g(images.last[x,y]) - 2 * [g(pixel), g(images.last[x,y])].min,
    b(pixel) + b(images.last[x,y]) - 2 * [b(pixel), b(images.last[x,y])].min
    )
    if images.last[x,y] == 255
    count = count + 1
    end
    end
    end

    100 - ((count.to_f / images.last.pixels.length.to_f) * 100);
    end

    # find the file
    def get_screenshot_name(folder, fileName)
    foundName = fileName
    Dir.foreach('screens/') do |item|
    next if item == '.' or item == '..'
    if item.start_with? fileName.split('.')[0]
    foundName = item
    end
    end

    foundName
    end

    def setup_comparison(fileName, percentageVariance)
    screenshotFileName = "compare_#{fileName}"
    screenshot({ :prefix => "screens/", :name => screenshotFileName })

    screenshotFileName = get_screenshot_name("screens/", screenshotFileName)
    changed = compare_image(fileName, screenshotFileName)
    FileUtils.rm("screens/#{screenshotFileName}")

    if changed.to_i > percentageVariance
    fail(msg="Error. The screen shot was different from the source file. Difference: #{changed.to_i}%")
    end
    end

    def setup_comparison_url(url, percentageVariance)
    fileName = "tester.png"
    open("screens/#{fileName}", 'wb') do |file|
    file << open(url).read
    end

    setup_comparison(fileName, percentageVariance)
    FileUtils.rm("screens/#{fileName}")
    end

    Then(/^I compare the screen with "(.*?)"$/) do |fileName|
    setup_comparison(fileName, 0)
    end

    Then(/^I compare the screen with url "(.*?)"$/) do |url|
    setup_comparison_url(url, 0)
    end

    Then(/^I expect atmost "(.*?)" difference when comparing with "(.*?)"$/) do |percentageVariance, fileName|
    setup_comparison(fileName, percentageVariance.to_i)
    end

    Then(/^I expect atmost "(.*?)" difference when comparing with url "(.*?)"$/) do |percentageVariance, url|
    setup_comparison_url(url, percentageVariance.to_i)
    end