@@ -43,33 +43,36 @@ so this isn't an unrealistic cheat at all.
43
43
Here's how you can set it up:
44
44
45
45
[role="sourcecode"]
46
- .src/functional_tests/test_my_lists.py
46
+ .src/functional_tests/test_my_lists.py (ch20l001)
47
47
====
48
48
[source,python]
49
49
----
50
50
from django.conf import settings
51
51
from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
52
52
from django.contrib.sessions.backends.db import SessionStore
53
+
53
54
from .base import FunctionalTest
55
+
54
56
User = get_user_model()
55
57
56
58
57
59
class MyListsTest(FunctionalTest):
58
-
59
60
def create_pre_authenticated_session(self, email):
60
61
user = User.objects.create(email=email)
61
62
session = SessionStore()
62
- session[SESSION_KEY] = user.pk #<1>
63
+ session[SESSION_KEY] = user.pk # <1>
63
64
session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
64
65
session.save()
65
66
## to set a cookie we need to first visit the domain.
66
67
## 404 pages load the quickest!
67
68
self.browser.get(self.live_server_url + "/404_no_such_url/")
68
- self.browser.add_cookie(dict(
69
- name=settings.SESSION_COOKIE_NAME,
70
- value=session.session_key, #<2>
71
- path='/',
72
- ))
69
+ self.browser.add_cookie(
70
+ dict(
71
+ name=settings.SESSION_COOKIE_NAME,
72
+ value=session.session_key, # <2>
73
+ path="/",
74
+ )
75
+ )
73
76
----
74
77
====
75
78
@@ -260,7 +263,7 @@ a good place for a commit:
260
263
261
264
[subs="specialcharacters,quotes"]
262
265
----
263
- $ *git add functional_tests*
266
+ $ *git add src/ functional_tests*
264
267
$ *git commit -m "test_my_lists: precreate sessions, move login checks into base"*
265
268
----
266
269
@@ -305,8 +308,14 @@ TIP: Once you have more than a handful of fields on a model, and/or several
305
308
Our Final Explicit Wait Helper: A Wait Decorator
306
309
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
307
310
308
- ((("decorators", "wait decorator", id="Dwait20")))((("explicit and implicit waits", id="exp20")))((("implicit and explicit waits", id="imp20")))((("helper methods", id="help20")))((("wait_for_row_in_list_table helper method")))((("self.wait_for helper method")))((("wait_to_be_logged_in/out")))We've
309
- used decorators a few times in our code so far, but it's time to learn
311
+ ((("decorators", "wait decorator", id="Dwait20")))
312
+ ((("explicit and implicit waits", id="exp20")))
313
+ ((("implicit and explicit waits", id="imp20")))
314
+ ((("helper methods", id="help20")))
315
+ ((("wait_for_row_in_list_table helper method")))
316
+ ((("self.wait_for helper method")))
317
+ ((("wait_to_be_logged_in/out")))
318
+ We've used decorators a few times in our code so far, but it's time to learn
310
319
how they actually work by making one of our own.
311
320
312
321
First, let's imagine how we might want our decorator to work. It would be
@@ -398,7 +407,8 @@ That's 'almost' right, but not quite; try running it?
398
407
$ pass:quotes[*python src/manage.py test functional_tests.test_my_lists*]
399
408
[...]
400
409
self.wait_to_be_logged_out(email)
401
- TypeError: modified_fn() takes 0 positional arguments but 2 were given
410
+ TypeError: wait.<locals>.modified_fn() takes 0 positional arguments but 2 were
411
+ given
402
412
----
403
413
404
414
@@ -414,7 +424,7 @@ that have [keep-together]#arguments#:
414
424
----
415
425
@wait
416
426
def wait_to_be_logged_in(self, email):
417
- self.browser.find_element(By.LINK_TEXT, "Log out").click()
427
+ self.browser.find_element(By.LINK_TEXT, "Log out")
418
428
----
419
429
====
420
430
0 commit comments