fromnose.toolsimporteq_,set_traceasstfromPyQt4.QtWebKitimportQWebElementfromtestsimportbrowserdeftest_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')assertdivisnotNonedeftest_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')assertdivisnotNonedeftest_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.')deftest_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())fornamein('backgroundColor','border','padding','margin','fontSize')]eq_(values,['rgb(255, 192, 203)','3px dashed purple','10px','60px 10px','1.5em'])deftest_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 JavaScriptqvariant=browser.runjs("document.getElementById('text_input').value")value=unicode(qvariant.toString())eq_(value,"vuvuzelas ruined the world cup")deftest_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 JavaScriptqvariant=browser.runjs("document.getElementById('text_input').value")value=unicode(qvariant.toString())eq_(value,"vuvuzelas ruined the world cup")deftest_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 JavaScriptqvariant=browser.runjs("document.getElementById('text_input').value")value=unicode(qvariant.toString())eq_(value,test_str)deftest_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'tests/pages/test_interaction_2.html?text_input=foobar&checked_checkbox=on&select=77&mood=livid'inbrowser.urldeftest_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)deftest_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)deftest_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)deftest_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)deftest_select_selector():browser.load('tests/pages/test_interaction.html')browser.select('#select option[value="88"]')qvariant=browser.first('#select').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.')deftest_select_element():browser.load('tests/pages/test_interaction.html')elem=browser.first('#select option[value="88"]')browser.select(elem)qvariant=browser.first('#select').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.')deftest_choose_selector():browser.load('tests/pages/test_interaction.html')browser.choose('input[name=mood][value=hulk]')radio_values=[radio.evaluateJavaScript('this.checked').toBool()forradioinbrowser.all('input[type=radio]')]eq_(radio_values,[False,False,True])deftest_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()forradioinbrowser.all('input[type=radio]')]eq_(radio_values,[False,False,True])