def test_html_returns_unicode(): body_text = html.div( html.p(u"£")).encode('utf-8') agent = TestAgent(wz.Response([body_text])) page = agent.get(u'/') assert page.html() == body_text.decode('utf-8') assert page.html('utf-8') == body_text
def test_lxml_attr_is_consistent(): body_text = html.div( html.p(u"foo")).encode('utf-8') agent = TestAgent(wz.Response([body_text])) page = agent.get(u'/') div_element = page.one('//div') assert page.lxml == div_element.lxml
def test_click_404_raises_error(): page = TestAgent(TestApp()).get('/bad-link') link = page.one("//a[text()='A Bad Link']") assert_raises( testino.PageNotFound, link.click )
def test_one(): page = TestAgent(TestApp()).get('/page1') assert_raises( testino.MultipleMatchesError, page.one, "//a" ) assert_raises( testino.NoMatchesError, page.one, "//h1" ) for href in ['page1', 'page2']: element = page.one("//a[@href=$href]", href=href) assert element.element.attrib['href'] == href page = TestAgent(TestApp()).get('/form-mixed') form = page.form data = [ dict(name="a", value="A", type="text"), dict(name="b", value="B", type="text")] for datum in data: xpath = "input[@name=$name and @value=$value and @type=$type]" element = form.one(xpath, **datum) for key, value in datum.items(): assert element.element.attrib[key] == value
def test_form_submit_follows_redirect(): form_page = TestAgent(TestApp()).get('/form-text') form_page.one('//form').attrib['method'] = 'get' form_page.one('//form').attrib['action'] = '/redirect1' assert_equal( form_page.one('//form').submit(follow=True).request.path, '/page1' )
def test_form_setitem(): form_page = TestAgent(TestApp()).get('/form-checkbox') form = form_page.one('//form') assert_raises( AssertionError, form.__setitem__, 'a', ['1', '2', '3'])
def test_click_ignores_fragment(): class UrlFragmentApp(MockApp): page1 = (u"GET", lambda r: wz.Response(['<a href="/page2#fragment">link to page 1</a>'])) page2 = (u"GET", lambda r: wz.Response(['This is page2'])) agent = TestAgent(UrlFragmentApp) assert_equal( agent.get('/page1').one("//a").click().request.path, '/page2')
def test_click_many(): body = """<p><a href="page1">page 1</a><a href="page1">page 1</a></p>""" agent = TestAgent(wz.Response(body)).get('/') assert_raises( testino.MultipleMatchesError, agent.click, text="page 1" ) assert agent.click(text="page 1", many=True)
def test_form_getitem_doesnt_match(): form_text = html.body( html.form( html.input(name="foo", value="a")), html.input(name="foo", value="b")) agent = TestAgent(wz.Response([form_text])) form_page = agent.get(u'/') form = form_page.one(u"//form") assert form[u"foo"] == u"a"
def test_cookie_paths_are_observed(): response = TestAgent(TestApp()).get('/setcookie?name=doobedo&value=dowop&path=/') response = response.get('/setcookie?name=dowahdowah&value=beebeebo&path=/private') response = response.get('/cookies') assert_equal(response.body, 'doobedo:<dowop>') response = response.get('/private/cookies') assert_equal(response.body, 'doobedo:<dowop>; dowahdowah:<beebeebo>')
def test_click_follows_redirect(): page = TestAgent(TestApp()).get('/page1') link = page.one("//a[text()='redirect']") response = link.click(follow=False) assert_equal(response.request.path, '/redirect1') page = TestAgent(TestApp()).get('/page1') link = page.one("//a[text()='redirect']") response = link.click(follow=True) assert_equal(response.request.path, '/page1')
def test_has_class(): body = '''<p class="foo bar">Mr Flibble says Hello!</p>''' agent = TestAgent(wz.Response(body)) page = agent.get('/') p = page.one("//p") assert p.has_class("foo") assert p.has_class("bar") assert not p.has_class("f") assert not p.has_class("fo") assert not p.has_class("Flibble")
def test_unicode_chars(): body_text = html.div( html.p(u"£")).encode('utf-8') agent = TestAgent(wz.Response([body_text])) page = agent.get(u'/') assert page.body == body_text assert tostring(page.lxml) == body_text assert page.html().encode('utf-8') == body_text div_element = page.one('//div') assert div_element.html().encode('utf-8') == body_text assert tostring(div_element.lxml) == body_text
def test_empty_rows(): body = """ <table> <tr> <td></td><td></td><td></td> </tr> </table> """ agent = TestAgent(wz.Response(body)).get(u'/') row = agent.one(u'//tr') row.assert_is([None, '', u''])
def test_get_allows_relative_uri(): agent = TestAgent(wz.Response(['<html><p>salt</p><p>pepper</p><p>pickle</p>'])) try: agent.get('../') except AssertionError: # Expect an AssertionError, as we haven't made an initial request to be # relative to pass else: raise AssertionError("Didn't expect relative GET request to work") agent = agent.get('/rhubarb/custard/') agent = agent.get('../') assert_equal(agent.request.url, 'http://localhost/rhubarb/')
def test_lxml_attr_doesnt_reset_forms(): form_page = TestAgent(TestApp()).get('/form-text') form = form_page.one('//form') # Set field values form.one('//input[@name="a"][1]').value = 'do' form.one('//input[@name="a"][2]').value = 're' form.one('//input[@name="b"][1]').value = 'mi' # Check page body assert "form" in tostring(form_page.lxml) # Check form values assert form.one('//input[@name="a"][1]').value == 'do' assert form.one('//input[@name="a"][2]').value == 're' assert form.one('//input[@name="b"][1]').value == 'mi'
def test_button_form_property(): html_form = ''' <html><body> <form method="POST" id="flibble" action="/flibble"> <input type="text" name="foo" value=""> <input type="submit" name="submit" value="Save"> </form> <button form="flibble" name="another">Another Button</button> </body></html>''' page = TestAgent(wz.Response(html_form)).get('/') form = page.form button = page.one("//button") assert button.form == form assert ("another", "") in button.submit_data()
def test_rows_to_dict(): body_1 = """ <table> <thead> <tr> <th>foo</th> <th>bar</th> <th>baz</th> </tr> </thead> <tbody> <tr> <td> 1 </td> <td> 2 </td> <td> 3 </td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </tbody> </table> """ body_2 = """ <table> <thead> <tr> <th><a href="">foo</a></th> <th>bar</th> <th>baz</th> </tr> </thead> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>4</td> <td>5</td> <td>6</td> </tr> </table> """ for body in [body_1, body_2]: agent = TestAgent(wz.Response(body)).get('/') row = agent.one(u'//tr[td][1]') assert row.headers() == ["foo", "bar", "baz"] expected = dict(foo='1', bar='2', baz='3') for key in expected: assert_equal(expected[key], row.to_dict()[key])
def test_tables(): header_values = ["foo", "bar", "baz"] table_text = html.div( html.table( html.thead( html.tr( *[html.th(html.span(i+" ")) for i in header_values])), html.tbody( html.tr( *[html.td(i) for i in [1, 2, 3]]), html.tr( *[html.td(i) for i in [4, 5, 6]]))), html.table( html.thead( html.tr( *[html.th(html.span(i+" ")) for i in header_values])), html.tbody( html.tr( *[html.td(i) for i in [1, 2, 3]]), html.tr( *[html.td(i) for i in [4, 5, 6]])))) agent = TestAgent(wz.Response([table_text])).get(u'/') table = agent.all(u"//table")[0] rows = [row.to_dict() for row in table.rows()] headers = table.headers() assert len(headers) == 3 assert headers == header_values assert len(rows) == 2 for i, row in enumerate(rows): for j, header in enumerate(header_values): index = (i * 3) + (j + 1) assert row[header] == str(index) assert row[header] == type(row[header])(index) for j, cell in enumerate(row.values()): index = (i * 3) + (j + 1) assert cell == str(index) lists = [ ['1', '2', '3'], [4, 5, 6], ] for row, l in zip(table.rows(), lists): row.assert_is(l) assert_raises( AssertionError, table.rows()[0].assert_is, ['flim', 'flam', 'flooble'] )
def test_form_text(): form_page = TestAgent(TestApp()).get('/form-text') form = form_page.one('//form') # Check defaults are submitted assert_equal( form.submit().body, "a:<>; a:<a>; b:<>" ) # Now set field values form.one('//input[@name="a"][1]').value = 'do' form.one('//input[@name="a"][2]').value = 're' form.one('//input[@name="b"][1]').value = 'mi' assert_equal( form.submit().body, "a:<do>; a:<re>; b:<mi>" )
def test_form_radio_value_property(): form = TestAgent(FormApp(""" <input name="a" value="1" type="radio"/> <input name="a" value="2" type="radio"/> """)).get('/').form assert form['a'] == None form.one('//input[@name="a"][2]').checked = True assert form['a'] == '2' assert form.one('//input[@name="a"][1]').value == '2' assert form.one('//input[@name="a"][2]').value == '2' form['a'] = '1' assert form.one('//input[@name="a"][1]').checked == True assert form.one('//input[@name="a"][2]').checked == False assert form.one('//input[@name="a"][1]').value == '1' assert form.one('//input[@name="a"][2]').value == '1' form['a'] = '2' assert form.one('//input[@name="a"][1]').checked == False assert form.one('//input[@name="a"][2]').checked == True form['a'] = None assert form.one('//input[@name="a"][1]').checked == False assert form.one('//input[@name="a"][2]').checked == False
def test_form_file_input_value_requires_3tuple(): r = TestAgent(FormApp('<input name="upload" type="file" />')).get('/') try: r.one('//input').value = 'photo.jpg' except ValueError: pass else: raise AssertionError("Expecting a ValueError") r = TestAgent(FormApp('<input name="upload" type="file" />')).get('/') try: r.one('//input').value = ('photo.jpg', '123123') except ValueError: pass else: raise AssertionError("Expecting a ValueError") r.one('//input').value = ('photo.jpg', 'text/jpeg', '123123')
def test_form_textarea(): form_page = TestAgent(FormApp('<textarea name="t"></textarea>')).get('/') # Test empty submission form = form_page.form data = form.submit_data() assert data == [("t", "")] el = form_page.one('//textarea') assert el.submit_value == "" # Test non empty submission el.value = 'test' assert_equal( form_page.one('//textarea').form.submit().body, 't:<test>' ) assert el.submit_value == 'test' form = form_page.form form['t'] = "Mr Flibble says hello!" assert form.submit_data() == [("t", "Mr Flibble says hello!")]
def test_all(): page = TestAgent(TestApp()).get('/form-checkbox') for name in ['a', 'b']: elements = page.all("//input[@name=$name]", name=name) for element in elements: assert element.element.attrib['name'] == name page = TestAgent(TestApp()).get('/form-checkbox') form = page.form data = [ dict(name="a", type="checkbox"), dict(name="b", type="checkbox")] for datum in data: xpath = "input[@name=$name and @type=$type]" elements = form.all(xpath, **datum) for element in elements: for key, value in datum.items(): assert element.element.attrib[key] == value
def test_reset(): agent = TestAgent(TestApp()) assert_raises( testino.NoRequestMadeError, agent.reset ) page = agent.get(u'/form-text') assert page.one(u"//input[@name='a'][1]").value == u'a' page.one(u"//input[@name='a'][1]").value = u'foobar' assert page.one(u"//input[@name='a'][1]").value == u'foobar' page.reset() assert page.one(u"//input[@name='a'][1]").value == u'a' input_b = page.one(u"//input[@name='b']") assert input_b.value == u'' input_b.value = u'flibble' assert input_b.value == u'flibble' page.reset() assert input_b.value == u''
def test_form_checkbox(): form_page = TestAgent(TestApp()).get('/form-checkbox') form = form_page.one('//form') # Check defaults are submitted assert_equal( form.submit().body, "b:<A>" ) # Now set field values form.one('//input[@name="a"][1]').checked = True form.one('//input[@name="a"][2]').checked = True form.one('//input[@name="b"][1]').checked = False form.one('//input[@name="b"][2]').checked = True assert form.one('//input[@name="a"][1]').checked == True assert form.one('//input[@name="a"][2]').checked == True assert form.one('//input[@name="b"][1]').checked == False assert form.one('//input[@name="b"][2]').checked == True assert_equal( form.submit().body, "a:<1>; a:<2>; b:<B>" )
def test_form_file_input_submits_file_data(): class TestApp(FormApp): def POST(self, environ, start_response): req = wz.Request(environ) fu = req.files['upload'] assert isinstance(fu, wz.FileStorage) assert fu.read() == '123123' return wz.Response(['ok'])(environ, start_response) r = TestAgent(TestApp('<input name="upload" type="file" />', enctype="multipart/form-data")).get('/') r.one('//input').value = ('photo.jpg', 'text/jpeg', '123123') r.one('//form').submit() r = TestAgent(TestApp('<input name="upload" type="file" />', enctype="multipart/form-data")).get('/') r.one('//input').value = ('photo.jpg', 'text/jpeg', StringIO('123123')) r.one('//form').submit()
def test_form_getitem(): form_text = html.div( html.p( html.input(type="text", name="foo", value="flam")), html.p( html.select( html.option(value="a", selected=True), html.option(value="b"), name="bar")) ) form_page = TestAgent(FormApp(form_text)).get(u'/') form = form_page.one(u'//form') assert form['foo'] == "flam" assert form['bar'] == "a" form["foo"] = u"flibble" form["bar"] = u"a" assert form.one(u'//input').value == u'flibble' assert form.one(u'//select').value == u'a' # Test checkboxes form_page = TestAgent(TestApp()).get('/form-checkbox') form = form_page.one('//form') assert form['a'] == [] assert form['b'] == ["A"]
def test_form_radio(): app = FormApp(""" <input name="a" value="1" type="radio"/> <input name="a" value="2" type="radio"/> <input name="b" value="3" type="radio"/> <input name="b" value="4" type="radio"/> """) r = TestAgent(app).get('/') r.all('//*[@name="a"]')[0].checked = True r.all('//*[@name="b"]')[0].checked = True assert_equal(r.one('//form').submit().body, 'a:<1>; b:<3>') r = TestAgent(app).get('/') r.one('//*[@name="a"][1]').checked = True r.one('//*[@name="a"][2]').checked = True assert_equal(r.one('//form').submit().body, 'a:<2>')
def test_form_select_multiple(): app = FormApp(""" <select name="s" multiple=""> <option value="o1"></option> <option value="o2"></option> <option value="o3"></option> </select> """) r = TestAgent(app).get('/') r.one('//select').value = ['o1', 'o3'] assert_equal(r.one('//form').submit().body, 's:<o1>; s:<o3>') r = TestAgent(app).get('/') r.one('//select/option[3]').selected = True r.one('//select/option[2]').selected = True assert_equal(r.one('//form').submit().body, 's:<o2>; s:<o3>')