PunkyBrowster / tests /

from import eq_, set_trace as st
from PyQt4.QtWebKit import QWebElement

from tests import browser

def test_all():
    """Does the all() method return a list of QWebElement objects?

    NOTE: Even if they are dynamically created by JavaScript?

    paras = browser.all('p')
    assert isinstance(paras, list)
    assert isinstance(paras[0], QWebElement)
    eq_(len(paras), 20)

def test_all_return_empty_list():
    """Does the all() method return an empty list when no elements are found?

    imgs = browser.all('img')       # there are no images on this page
    assert isinstance(imgs, list)
    eq_(len(imgs), 0)

def test_first():
    """Does the first() method return a QWebElement object?

    # Look for an H2 element whose title attribute contains the word "description"
    h2 = browser.first('h2[title*="description"]')

    # Did we get the right element?
    eq_(unicode(h2.toPlainText()), 'This page dynamically creates a lot of paragraph elements.')

    assert isinstance(h2, QWebElement)

def test_first_return_none():
    """Does the first() method return None if no element is found?

    h1 = browser.first('h1[title$="wacky"]')  # there are no h1 elements on this page
    eq_(h1, None)

def test_count():
    """Does the count() method return the right value?
    eq_(browser.count('p'), 20)
    eq_(browser.count('h2[title]'), 1)

def test_count_return_zero():
    """Does count() return 0 when no elements are found?
    eq_(browser.count('h1'), 0)