edith_browser
=
self
.
browser
self
.
addCleanup
(
lambda
:
quit_if_possible
(
edith_browser
))
# Her friend Oniciferous is also hanging out on the lists site
oni_browser
=
webdriver
.
Firefox
()
self
.
addCleanup
(
lambda
:
quit_if_possible
(
oni_browser
))
self
.
browser
=
oni_browser
self
.
create_pre_authenticated_session
(
'oniciferous@example.com'
)
# Edith goes to the home page and starts a list
self
.
browser
=
edith_browser
self
.
browser
.
get
(
self
.
server_url
)
self
.
get_item_input_box
()
.
send_keys
(
'Get help
\n
'
)
# She notices a "Share this list" option
share_box
=
self
.
browser
.
find_element_by_css_selector
(
'input[name=email]'
)
self
.
assertEqual
(
share_box
.
get_attribute
(
'placeholder'
),
'your-friend@example.com'
)
The interesting feature to note about this section is the
addCleanup
function, whose
documentation you can find
here. It can be used as an alternative to the
tearDown
function as a way of cleaning up resources used during the test. It’s most useful when
the resource is only allocated halfway through a test, so you don’t have to spend time in
tearDown
figuring out what does or doesn’t need cleaning up.
addCleanup
is run after
tearDown
, which is why we need that
try/except
formulation
for
quit_if_possible
—whichever of
edith_browser
and
oni_browser
is also assigned
to
self.browser
at the point at which the test ends will already have been quit by the
tearDown
function.
We’ll also need to move
create_pre_authenticated_session
from
test_my_lists.py
into
base.py
.
OK, let’s see if that all works:
$
python3 manage.py test functional_tests.test_sharing
[...]
Traceback (most recent call last):
File "/workspace/superlists/functional_tests/test_sharing.py", line 29, in
test_logged_in_users_lists_are_saved_as_my_lists
share_box = self.browser.find_element_by_css_selector('input[name=email]')
[...]
selenium.common.exceptions.NoSuchElementException: Message: 'Unable to locate
element: {"method":"css selector","selector":"input[name=email]"}' ;
Great! It seems to have got through creating the two user sessions, and it gets onto an
expected failure—there is no input for an email address of a person to share a list with
on the page.
388
|
Chapter 21: The Token Social Bit, the Page Pattern, and an Exercise for the Reader