Source

PunkyBrowster / tests / test_snapshot.py

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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.