Source

PunkyBrowster / tests / test_extract_values.py

import re

from nose.tools import eq_, set_trace as st

from tests import browser
from punky.browster import PunkyError


def test_value_checked_selected_by_css():
    """Do the value(), checked() and selected() methods return the expected
    values?
    """
    browser.load('tests/pages/test_extract_values.html')

    eq_(browser.value('form input[type=text]'), 'antelope')
    eq_(browser.value('form textarea'), 'banana')

    eq_(browser.checked('form input[type=checkbox]'), True)
    eq_(browser.checked('form input[type=radio]'), False)

    eq_(browser.selected('form option:first-child'), False)
    eq_(browser.selected('form option:nth-child(3)'), True)

def test_value_checked_selected_by_element():
    """Do the value(), checked() and selected() methods return the expected
    values when called directly on elements?
    """
    browser.load('tests/pages/test_extract_values.html')

    elem1 = browser.first('form input[type=text]')
    eq_(browser.value(elem1), 'antelope')

    elem2 = browser.first('form textarea')
    eq_(browser.value(elem2), 'banana')

    elem3 = browser.first('form input[type=checkbox]')
    eq_(browser.checked(elem3), True)

    elem4 = browser.first('form input[type=radio]')
    eq_(browser.checked(elem4), False)

    elem5 = browser.first('form option:first-child')
    eq_(browser.selected(elem5), False)

    elem6 = browser.first('form option:nth-child(3)')
    eq_(browser.selected(elem6), True)


def test_value_checked_selected_with_exceptions():
    """Do the value(), checked() and selected() methods raise the expected
    exceptions when they receive bad selectors?
    """
    browser.load('tests/pages/test_extract_values.html')

    try:
        browser.value('form input[type=checkbox]')
        assert False, 'unreachable line'
    except PunkyError, e:
        eq_(str(e), 'value() expected input[type=text] or textarea')

    try:
        browser.checked('form textarea')
        assert False, 'unreachable line'
    except PunkyError, e:
        eq_(str(e), 'checked() expected input[type=radio] or input[type=checkbox]')

    try:
        browser.selected('form select')
        assert False, 'unreachable line'
    except PunkyError, e:
        eq_(str(e), 'selected() expected option')


def test_text():
    """Does the text() method return the expected value?

    NOTE: Even if the text is dynamically added to the document?

    """
    browser.load('tests/pages/test_extract_values.html')
    text = browser.text('#boring-content')
    # FIXME: Whitespace can be handled slightly erratically, for reasons I
    # FIXME: cannot determine.
    text = re.sub(r'\s+', ' ', text)
    eq_(text, "The quick brown fox jumps over the lazy dog. The lazy dog "
              "shoots the fox. The fox's head is blown off.")