A Big Find and Replace
One thing we have done, though, is changed our form—it no longer uses the same
id
and
name
attributes. You’ll see if we run our functional tests that they fail the first time
they try and find the input box:
selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate
element: {"method":"id","selector":"id_new_item"}' ; Stacktrace:
We’ll need to fix this, and it’s going to involve a big find and replace. Before we do that,
let’s do a commit, to keep the rename separate from the logic change:
$
git diff
# review changes in home.html, views.py and its tests
$
git commit -am "use new form in home_page, simplify tests. NB breaks stuff"
Let’s fix the functional tests. A quick
grep
shows us there are several places where we’re
using
id_new_item
:
$
grep id_new_item functional_tests/test*
functional_tests/test_layout_and_styling.py: inputbox =
self.browser.find_element_by_id('id_new_item')
functional_tests/test_layout_and_styling.py: inputbox =
self.browser.find_element_by_id('id_new_item')
functional_tests/test_list_item_validation.py:
self.browser.find_element_by_id('id_new_item').send_keys('\n')
[...]
That’s a good call for a refactor. Let’s make a new helper method in
base.py
:
functional_tests/base.py (ch11l018).
class
FunctionalTest
(
StaticLiveServerCase
):
[
...
]
def
get_item_input_box
(
self
):
return
self
.
browser
.
find_element_by_id
(
'id_text'
)
And then we use it throughout—I had to make three changes in
test_simple_list_cre‐
ation.py
, two in
test_layout_and_styling.py
, and four in
test_list_item_validation.py
, eg:
functional_tests/test_simple_list_creation.py.
# She is invited to enter a to-do item straight away
inputbox
=
self
.
get_item_input_box
()
Or:
functional_tests/test_list_item_validation.py.
# an empty list item. She hits Enter on the empty input box
self
.
browser
.
get
(
self
.
server_url
)
self
.
get_item_input_box
()
.
send_keys
(
'
\n
'
)
I won’t show you every single one, I’m sure you can manage this for yourself! You can
redo the
grep
to check you’ve caught them all.
Using the Form in Our Views
|
201