Skip to content

Commit

Permalink
Chapter 15: Unit tests with Selenium (15d)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 26, 2014
1 parent 6e27193 commit 9b1b0c2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
from ..decorators import admin_required, permission_required


@main.route('/shutdown')
def server_shutdown():
if not current_app.testing:
abort(404)
shutdown = request.environ.get('werkzeug.server.shutdown')
if not shutdown:
abort(500)
shutdown()
return 'Shutting down...'


@main.route('/', methods=['GET', 'POST'])
def index():
form = PostForm()
Expand Down
1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ colorama==0.2.7
coverage==3.7.1
httpie==0.7.2
requests==2.1.0
selenium==2.39.0
89 changes: 89 additions & 0 deletions tests/test_selenium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import re
import threading
import unittest
from selenium import webdriver
from app import create_app, db
from app.models import Role, User, Post


class SeleniumTestCase(unittest.TestCase):
client = None

@classmethod
def setUpClass(cls):
# start Firefox
try:
cls.client = webdriver.Firefox()
except:
pass

# skip these tests if the browser could not be started
if cls.client:
# create the application
cls.app = create_app('testing')
cls.app_context = cls.app.app_context()
cls.app_context.push()

# suppress logging to keep unittest output clean
import logging
logger = logging.getLogger('werkzeug')
logger.setLevel("ERROR")

# create the database and populate with some fake data
db.create_all()
Role.insert_roles()
User.generate_fake(10)
Post.generate_fake(10)

# add an administrator user
admin_role = Role.query.filter_by(permissions=0xff).first()
admin = User(email='[email protected]',
username='john', password='cat',
role=admin_role, confirmed=True)
db.session.add(admin)
db.session.commit()

# start the Flask server in a thread
threading.Thread(target=cls.app.run).start()

@classmethod
def tearDownClass(cls):
if cls.client:
# stop the flask server and the browser
cls.client.get('http://localhost:5000/shutdown')
cls.client.close()

# destroy database
db.drop_all()
db.session.remove()

# remove application context
cls.app_context.pop()

def setUp(self):
if not self.client:
self.skipTest('Web browser not available')

def tearDown(self):
pass

def test_admin_home_page(self):
# navigate to home page
self.client.get('http://localhost:5000/')
self.assertTrue(re.search('Hello,\s+Stranger!',
self.client.page_source))

# navigate to login page
self.client.find_element_by_link_text('Log In').click()
self.assertTrue('<h1>Login</h1>' in self.client.page_source)

# login
self.client.find_element_by_name('email').\
send_keys('[email protected]')
self.client.find_element_by_name('password').send_keys('cat')
self.client.find_element_by_name('submit').click()
self.assertTrue(re.search('Hello,\s+john!', self.client.page_source))

# navigate to the user's profile page
self.client.find_element_by_link_text('Profile').click()
self.assertTrue('<h1>john</h1>' in self.client.page_source)

0 comments on commit 9b1b0c2

Please sign in to comment.