1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
# -*- coding: utf-8 -*-
from __future__ import with_statement
import os
import webob
import webtest
from webob import exc
from tests.compat import unittest
from webtest.compat import PY3
from tests.compat import u
#if PY3:
# raise NotImplementedError
files = os.path.dirname(__file__)
def application(environ, start_response):
req = webob.Request(environ)
resp = webob.Response()
if req.method == 'GET':
filename = req.path_info.strip('/') or 'index.html'
if filename in ('302',):
redirect = req.params['redirect']
resp = exc.HTTPFound(location=redirect)
return resp(environ, start_response)
if filename.isdigit():
resp.status = filename
filename = 'index.html'
filename = os.path.join(files, 'html', filename)
if os.path.isfile(filename):
kw = dict(message=req.params.get('message', ''),
redirect=req.params.get('redirect', ''))
resp.unicode_body = u(open(filename).read()) % kw
_, ext = os.path.splitext(filename)
if ext == '.html':
resp.content_type = 'text/html'
elif ext == '.js':
resp.content_type = 'text/javascript'
elif ext == '.json':
resp.content_type = 'application/json'
else:
redirect = req.params.get('redirect', '')
if redirect:
resp = exc.HTTPFound(location=redirect)
else:
resp.body = req.body
return resp(environ, start_response)
class TestApp(unittest.TestCase):
def setUp(self):
self.app = webtest.TestApp(application)
def test_webtest(self):
resp = self.app.get('/', {'redirect': '/message.html?message=submited'})
resp.mustcontain('It Works!')
form = resp.forms['myform']
form.lint()
self.assertEqual(form['mytext'].value, '')
resp.mustcontain(no='Form submited')
with webtest.selenium(resp) as sresp:
if sresp:
sform = sresp.forms['myform']
sform['mytext'] = 'foo'
sresp = sform.submit(name='go', timeout=0)
sresp.mustcontain('Form submited')
if resp.updated:
resp.mustcontain('Form submited')
form = resp.forms['myform']
self.assertEqual(form['mytext'].value, 'foo')
resp = form.submit(name='go')
resp = resp.follow()
resp.mustcontain('<pre>submited</pre>')
@webtest.selenium
def test_selenium(self):
resp = self.app.get('/', {'redirect': '/message.html?message=submited'})
resp.mustcontain('It Works!')
form = resp.forms['myform']
form.lint()
form['mytext'] = 'foo'
self.assertEqual(form['mytext'].value, 'foo')
# file upload are only supported with *firefox *chrome drivers
filename = os.path.join(files, 'html', 'index.html')
file = form['myfile']
file.value = (filename,)
form['myradio'] = 'true'
self.assertEqual(form['myradio'].value, 'true')
check = form.get('mycheckbox', index=0)
check.value = 'true'
self.assertEqual(check.value, 'true')
form['myselect'] = 'value2'
form['myselect'] = 'value2'
self.assertEqual(form['myselect'].value, 'value2')
form['mymultiselect'] = ['value1', 'value3']
self.assertEqual(form['mymultiselect'].value, ['value1', 'value3'])
# there is an ajax hook on the page
resp = form.submit(name='go', timeout=0)
resp.mustcontain('Form submited')
# but we can submit the form to get the non-javascript behavior
resp = form.submit()
resp = resp.follow()
resp.mustcontain('<pre>submited</pre>')
class TestStatus(unittest.TestCase):
@classmethod
def setupClass(cls):
cls.app = webtest.SeleniumApp(application)
def test_302(self):
resp = self.app.get('/302', dict(redirect='/500'))
self.assertRaises(webtest.AppError, resp.follow)
resp.follow(status=500)
resp = self.app.get('/302', dict(redirect='/404.html'))
self.assertRaises(webtest.AppError, resp.follow)
def test_404(self):
self.assertRaises(webtest.AppError, self.app.get, '/404')
self.app.get('/404', status=404)
self.assertRaises(webtest.AppError, self.app.get, '/404.html')
def test_500(self):
self.assertRaises(webtest.AppError, self.app.get, '/500')
self.app.get('/500', status=500)
@classmethod
def teardownClass(cls):
cls.app.close()
TestStatus = webtest.selenium(TestStatus)
class TestJQueryUI(unittest.TestCase):
@classmethod
def setupClass(cls):
cls.app = webtest.SeleniumApp(url='http://jqueryui.com/')
def setUp(self):
self.resp = self.app.get('http://jqueryui.com/demos/')
def test_autocomplete(self):
resp = self.resp.click('Autocomplete')
field = resp.doc.xpath('//input[@id="tags"]')
field.value = 'a'
item = resp.doc.xpath('//ul[@role="listbox"]//a[.="AppleScript"]')
item.wait().fireEvent('mouseover')
field.value = resp.doc.css('#ui-active-menuitem').html()
self.assertEqual(field.value, "AppleScript")
def test_datepicker(self):
resp = self.resp.click('Datepicker')
field = resp.doc.datepicker
field.fireEvent('focus')
resp.doc.link('16').wait_and_click()
self.assertIn('/16/', field.value)
def test_dialog(self):
resp = self.resp.click('Dialog')
close = resp.doc.xpath('//div[@role="dialog"]//span[.="close"]')
close.wait_and_click()
resp.doc.link('Modal form').click()
resp.doc.button('Create new user').wait().click()
form = resp.form
form['name'].value = 'Gael'
form['email'] = 'gael@gawel.org'
create = resp.doc.button('Create an account')
create.click()
pwd = form['password']
self.assertTrue(pwd.hasClass('ui-state-error'))
pwd.value = 'pwd'
create.click()
resp.mustcontain('Length of password must be between 5 and 16.')
pwd.value = 'passwd'
create.click()
resp.mustcontain('<td>Gael</td>')
def test_dropable(self):
resp = self.resp.click('Droppable')
draggable = resp.doc.draggable
droppable = resp.doc.droppable
self.assertFalse(droppable.hasClass('ui-state-highlight'))
draggable.drag_and_drop(droppable)
self.assertTrue(droppable.hasClass('ui-state-highlight'))
resp.doc.link('Shopping Cart').click()
cart = resp.doc.css('#cart ol.ui-droppable')
cart.wait()
item = resp.doc.xpath('//li[.="Lolcat Shirt"]')
self.assertNotIn(item, cart)
item.drag_and_drop(cart)
self.assertIn(item, cart)
@classmethod
def teardownClass(cls):
cls.app.close()
TestJQueryUI = webtest.selenium(TestJQueryUI)
|