forked from miguelgrinberg/flasky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chapter 15: Unit tests with Selenium (15d)
- Loading branch information
1 parent
6e27193
commit 9b1b0c2
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ colorama==0.2.7 | |
coverage==3.7.1 | ||
httpie==0.7.2 | ||
requests==2.1.0 | ||
selenium==2.39.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |