8000 Updated starter code and section10 tests. · SWCodeG/testing-python-apps@7a97523 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a97523

Browse files
committed
Updated starter code and section10 tests.
1 parent f196ab2 commit 7a97523

File tree

16 files changed

+122
-16
lines changed

16 files changed

+122
-16
lines changed

section10/video_code/templates/new_post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h1>Create post</h1>
88

99
<a href="{{ url_for('blog_page') }}" class="nav-link" id="blog-link">Back to blog</a>
1010

11-
<form method="POST">
11+
<form method="POST" id="post-form">
1212
<div>
1313
<label for="title">Post title:</label>
1414
<input type="text" name="title" id="title" />

section10/video_code/tests/acceptance/page_model/base_page.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
class BasePageLocators:
55
TITLE = By.TAG_NAME, 'h1'
6+
NAV_LINKS = By.CLASS_NAME, 'nav-link'
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1+
from selenium.webdriver.common.by import By
2+
3+
14
class NewPostPageLocators:
2-
pass
5+
NEW_POST_FORM = By.ID, 'post-form'
6+
TITLE_FIELD = By.ID, 'title'
7+
CONTENT_FIELD = By.ID, 'content'
8+
SUBMIT_BUTTON = By.ID, 'create-post'

section10/video_code/tests/acceptance/pages/base_page.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ def find_element(self, by, value):
1616
@property
1717
def title(self):
1818
return self.find_element(*BasePageLocators.TITLE)
19+
20+
@property
21+
def navigation(self):
22+
return self.find_elements(*BasePageLocators.NAV_LINKS)
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
from section10.video_code.tests.acceptance.pages.base_page import BasePage
1+
from selenium.webdriver.common.by import By
2+
3+
from tests.acceptance.locators.new_post_page import NewPostPageLocators
4+
from tests.acceptance.page_model.base_page import BasePage
25

36

47
class NewPostPage(BasePage):
58
@property
69
def url(self):
710
return super(NewPostPage, self).url + '/post'
11+
12+
@property
13+
def form(self):
14+
return self.driver.find_element(*NewPostPageLocators.NEW_POST_FORM)
15+
16+
@property
17+
def submit_button(self):
18+
return self.driver.find_element(*NewPostPageLocators.SUBMIT_BUTTON)
19+
20+
def form_field(self, name):
21+
return self.form.find_element(By.NAME, name)
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
from behave import *
2-
from selenium.webdriver.common.by import By
2+
3+
from tests.acceptance.page_model.base_page import BasePage
4+
from tests.acceptance.page_model.new_post_page import NewPostPage
35

46
use_step_matcher('re')
57

68

7-
@when('I click on the link with id "(.*)"')
8-
def step_impl(context, link_id):
9-
link = context.browser.find_element_by_id(link_id) # find_element_by_link_text can also work
10-
link.click()
9+
@when('I click on the "(.*)" link')
10+
def step_impl(context, link_text):
11+
page = BasePage(context.driver)
12+
links = page.navigation
13+
14+
matching_links = [l for l in links if l.text == link_text]
1115

16+
if len(matching_links) > 0:
17+
matching_links[0].click()
18+
else:
19+
raise RuntimeError()
1220

13-
@step('I enter "(.*)" in the "(.*)" field')
21+
22+
@when('I enter "(.*)" in the "(.*)" field')
1423
def step_impl(context, content, field_name):
15-
field = context.browser.find_element(By.NAME, field_name)
16-
field.send_keys(content)
24+
page = NewPostPage(context.driver)
25+
page.form_field(field_name).send_keys(content)
1726

1827

19-
@step('I press the submit button')
28+
@when('I press the submit button')
2029
def step_impl(context):
21-
button = context.browser.find_element_by_id('create-post')
22-
23-
button.click()
30+
page = NewPostPage(context.driver)
31+
page.submit_button.click()

section5/starter_code/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
app.config['DEBUG'] = True
1111

1212
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
13+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
1314
api = Api(app)
1415

1516
api.add_resource(Item, '/item/<string:name>')

section5/video_code/db.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
from flask_sqlalchemy import SQLAlchemy
21

32
db = SQLAlchemy()

section6/starter_code/tests/__init__.py

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
BaseTest
3+
4+
This class should be the parent class to each non-unit test.
5+
It allows for instantiation of the database dynamically
6+
and makes sure that it is a new, blank database each time.
7+
"""
8+
9+
from unittest import TestCase
10+
from app import app
11+
from db import db
12+
13+
14+
class BaseTest(TestCase):
15+
def setUp(self):
16+
# Make sure database exists
17+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'
18+
with app.app_context():
19+
db.init_app(app)
20+
db.create_all()
21+
# Get a test client
22+
self.app = app.test_client()
23+
self.app_context = app.app_context
24+
25+
def tearDown(self):
26+
# Database is blank
27+
with app.app_context():
28+
db.session.remove()
29+
db.drop_all()

0 commit comments

Comments
 (0)
0