Source

PunkyBrowster / tests / test_snapshot.py

Full commit
import os
import shutil
import tempfile

from PIL import Image
from nose.tools import eq_, set_trace as st

from tests import browser

root_dir = tempfile.mkdtemp()


def teardown():
    shutil.rmtree(root_dir)


def test_save_snapshot():
    """Does save_snapshot() produce an image of the expected size and format?

    """
    browser.load('tests/pages/test_snapshot.html')

    img_path = get_img_path('snapshot_1.png')
    browser.save_snapshot(img_path)
    img = Image.open(img_path)

    # The real size of the body is 280x400, but the 20-pixel margin increases
    # size of the snapshot.
    eq_(img.size, (300, 440))
    eq_(img.format, 'PNG')


def test_save_snapshot_specific_element():
    """Does passing a specific element to save_snapshot() product an image of
    the expected size and format?

    """
    browser.load('tests/pages/test_snapshot.html')

    img_path = get_img_path('snapshot_2.png')
    browser.save_snapshot(img_path, browser.first('#side-bar'))

    img = Image.open(img_path)

    # Although the actual size is 100x200, there is a 10-pixel padding and
    # automatic two-pixel margin:
    eq_(img.size, (122, 222))
    eq_(img.format, 'PNG')


def test_save_snapshot_large_small():
    """Does screencapturing two successive pages cause the second page's image
    to be larger than necessary?

    Specifically, if the first page is large and the second is small, does
    the small page's image get inflated to the size of the large page?
    """
    browser.load('tests/pages/large_page.html')
    large_img_path = get_img_path('large_page.png')
    browser.save_snapshot(large_img_path)

    browser.load('tests/pages/small_page.html')
    small_img_path = get_img_path('small_page.png')
    browser.save_snapshot(small_img_path)

    large_img = Image.open(large_img_path)
    small_img = Image.open(small_img_path)

    # Width is the same
    assert large_img.size[0] == small_img.size[0]
    # But height is different
    assert large_img.size[1] > small_img.size[1]


##=============================================================================
## HELPER FUNCTIONS
##=============================================================================
def get_img_path(name):
    return os.path.join(root_dir, name)