PunkyBrowster / tests / test_interaction.py

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

from tests import browser


def test_click_with_selector():
    """Does calling click() on a button element, specified with a CSS selector,
    cause that button's onclick handler to execute?
    """
    browser.load('tests/pages/test_interaction.html')

    browser.click('button#make-new-div')

    # Give the JavaScript a little time to execute. This test will probably fail
    # unless wait() is invoked.
    browser.wait(0.05)

    # Did clicking the button actually create a new DIV element?
    div = browser.first('div.button-click')
    assert div is not None

def test_click_with_element():
    """Can you invoke click() directly on a QWebElement?"""

    browser.load('tests/pages/test_interaction.html')

    elem = browser.first('button#make-new-div')

    browser.click(elem)
    browser.wait(0.05)

    # Did clicking the button actually create a new DIV element?
    div = browser.first('div.button-click')
    assert div is not None

def test_click_link():
    """Does calling click_link() on an anchor element cause the browser to load another page?
    """
    browser.load('tests/pages/test_interaction.html')

    browser.click_link('a#normal-link')

    p = browser.first('p')
    eq_(p.toPlainText(), 'This is another page entirely.')


def test_css():
    """Does calling css() on an element change its style?
    """
    browser.load('tests/pages/test_interaction.html')

    browser.css('#normal-link', """
    background-color: pink;
    border: 3px dashed purple;
    padding: 10px;
    margin: 60px 10px;
    font-size: 1.5em;
    """)

    el = browser.first('#normal-link')
    values = [str(el.evaluateJavaScript('this.style.%s' % name).toString())
              for name in
              ('backgroundColor', 'border', 'padding', 'margin', 'fontSize')]

    eq_(values, ['rgb(255, 192, 203)', '3px dashed purple', '10px', '60px 10px', '1.5em'])


def test_fill_by_selector():
    """Does calling fill() with a CSS selector cause the element specified
    to receive a new value?
    """
    browser.load('tests/pages/test_interaction.html')

    browser.fill('#text_input', 'vuvuzelas ruined the world cup')

    # Let us grab the value of that element using raw JavaScript
    qvariant = browser.runjs("document.getElementById('text_input').value")
    value = unicode(qvariant.toString())
    eq_(value, "vuvuzelas ruined the world cup")


def test_fill_by_element():
    """Does calling fill() on an input element directly cause
    that element to receive a new value?
    """
    browser.load('tests/pages/test_interaction.html')

    elem = browser.first('#text_input')
    browser.fill(elem, 'vuvuzelas ruined the world cup')

    # Let us grab the value of that element using raw JavaScript
    qvariant = browser.runjs("document.getElementById('text_input').value")
    value = unicode(qvariant.toString())
    eq_(value, "vuvuzelas ruined the world cup")


def test_fill_unicode():
    """Does passing a unicode string to fill() still work?
    """
    browser.load('tests/pages/test_interaction.html')

    test_str = u'''
        "I must fear" in Chinese is: '\u6211\u4e0d\u8bb8\u6015\'
    '''.strip()

    browser.fill('#text_input', test_str)

    # Let us grab the value of that element using raw JavaScript
    qvariant = browser.runjs("document.getElementById('text_input').value")
    value = unicode(qvariant.toString())
    eq_(value, test_str)


def test_submit():
    """Does calling the submit() method on a form element submit the form?
    """
    browser.load('tests/pages/test_interaction.html')

    browser.submit('form', wait_load=True)

    assert '/test_interaction_2.html?text_input=foobar&checked_checkbox=on&select1=77&mood=livid&select2=orange' in browser.url


def test_check_selector():
    browser.load('tests/pages/test_interaction.html')

    browser.check('#unchecked_checkbox')

    qvariant = browser.first('#unchecked_checkbox').evaluateJavaScript('this.checked')
    eq_(qvariant.toBool(), True)


def test_check_element():
    browser.load('tests/pages/test_interaction.html')

    elem = browser.first('#unchecked_checkbox')
    browser.check(elem)

    qvariant = browser.first('#unchecked_checkbox').evaluateJavaScript('this.checked')
    eq_(qvariant.toBool(), True)


def test_uncheck_selector():
    browser.load('tests/pages/test_interaction.html')

    browser.uncheck('#checked_checkbox')

    qvariant = browser.first('#checked_checkbox').evaluateJavaScript('this.checked')
    eq_(qvariant.toBool(), False)


def test_uncheck_element():
    browser.load('tests/pages/test_interaction.html')

    elem = browser.first('#checked_checkbox')
    browser.uncheck(elem)

    qvariant = browser.first('#checked_checkbox').evaluateJavaScript('this.checked')
    eq_(qvariant.toBool(), False)


def test_uncheck_radio_button_with_selector():
    browser.load('tests/pages/test_interaction.html')

    css_selector = 'input[type="radio"][name="mood"][value="livid"]'
    browser.uncheck(css_selector)

    qvariant = browser.first(css_selector).evaluateJavaScript('this.checked')
    eq_(qvariant.toBool(), False)


def test_uncheck_radio_button_element():
    browser.load('tests/pages/test_interaction.html')

    css_selector = 'input[type="radio"][name="mood"][value="livid"]'
    elem = browser.first(css_selector)
    browser.uncheck(elem)

    qvariant = browser.first(css_selector).evaluateJavaScript('this.checked')
    eq_(qvariant.toBool(), False)


def test_select_selector():
    browser.load('tests/pages/test_interaction.html')

    browser.select('#select1 option[value="88"]')

    qvariant = browser.first('#select1').evaluateJavaScript('this.value')
    eq_(str(qvariant.toString()), '88')

    # Was the 'change' event fired? If the event was fired, then the
    # #select1_message element should contain some text.
    xml = browser.first('#select_message').toInnerXml()
    eq_(str(xml), 'Something was selected.')


def test_select_element():
    browser.load('tests/pages/test_interaction.html')

    elem = browser.first('#select1 option[value="88"]')
    browser.select(elem)

    qvariant = browser.first('#select1').evaluateJavaScript('this.value')
    eq_(str(qvariant.toString()), '88')

    # Was the 'change' event fired? If the event was fired, then the
    # #select_message element should contain some text.
    xml = browser.first('#select_message').toInnerXml()
    eq_(str(xml), 'Something was selected.')


def test_unselect_selector():
    browser.load('tests/pages/test_interaction.html')

    browser.unselect('#select2 option[value="orange"]')

    qvariant = browser.first('#select2').evaluateJavaScript('this.value')
    eq_(str(qvariant.toString()), '')


def test_unselect_element():
    browser.load('tests/pages/test_interaction.html')

    elem = browser.first('#select2 option[value="orange"]')
    browser.unselect(elem)

    qvariant = browser.first('#select2').evaluateJavaScript('this.value')
    eq_(str(qvariant.toString()), '')


def test_choose_selector():
    browser.load('tests/pages/test_interaction.html')

    browser.choose('input[name=mood][value=hulk]')

    radio_values = [radio.evaluateJavaScript('this.checked').toBool()
                    for radio in browser.all('input[type=radio]')]

    eq_(radio_values, [False, False, True])


def test_choose_element():
    browser.load('tests/pages/test_interaction.html')

    elem = browser.first('input[name=mood][value=hulk]')
    browser.choose(elem)

    radio_values = [radio.evaluateJavaScript('this.checked').toBool()
                    for radio in browser.all('input[type=radio]')]

    eq_(radio_values, [False, False, True])
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.