A few new things here. We start by setting the window size to a fixed size. We then find
the input element, look at its size and location, and do a little maths to check whether
it seems to be positioned in the middle of the page.
assertAlmostEqual
helps us to deal
with rounding errors by letting us specify that we want our arithmetic to work to within
plus or minus five pixels.
If we run the functional tests, we get:
$
python3 manage.py test functional_tests
Creating test database for alias 'default'...
.F
======================================================================
FAIL: test_layout_and_styling (functional_tests.tests.NewVisitorTest)
---------------------------------------------------------------------
Traceback (most recent call last):
File "/workspace/superlists/functional_tests/tests.py", line 104, in
test_layout_and_styling
delta=5
AssertionError: 110.0 != 512 within 5 delta
---------------------------------------------------------------------
Ran 2 tests in 9.188s
FAILED (failures=1)
Destroying test database for alias 'default'...
That’s the expected failure. Still, this kind of FT is easy to get wrong, so let’s use a quick-
and-dirty “cheat” solution, to check that the FT also passes when the input box is cen‐
tered. We’ll delete this code again almost as soon as we’ve used it to check the FT:
lists/templates/home.html (ch07l002).
<form
method=
"POST"
action=
"/lists/new"
>
<p
style=
"text-align: center;"
>
<input
name=
"item_text"
id=
"id_new_item"
placeholder=
"Enter a to-do item"
/>
</p>
{% csrf_token %}
</form>
That passes, which means the FT works. Let’s extend it to make sure that the input box
is also center-aligned on the page for a new list:
functional_tests/tests.py (ch07l003).
# She starts a new list and sees the input is nicely
# centered there too
inputbox
.
send_keys
(
'testing
\n
'
)
inputbox
=
self
.
browser
.
find_element_by_id
(
'id_new_item'
)
self
.
assertAlmostEqual
(
inputbox
.
location
[
'x'
]
+
inputbox
.
size
[
'width'
]
/
2
,
512
,
delta
=
5
)
That gives us another test failure:
What to Functionally Test About Layout and Style
|
117