@@ -132,12 +132,10 @@ class to make it use `LiveServerTestCase`:
132
132
----
133
133
from django.test import LiveServerTestCase
134
134
from selenium import webdriver
135
- from selenium.webdriver.common.keys import Keys
136
- import time
135
+ [...]
137
136
138
137
139
138
class NewVisitorTest(LiveServerTestCase):
140
-
141
139
def setUp(self):
142
140
[...]
143
141
----
@@ -177,9 +175,9 @@ FAIL: test_can_start_a_list_and_retrieve_it_later (functional_tests.tests.NewVi
177
175
sitorTest.test_can_start_a_list_and_retrieve_it_later)
178
176
---------------------------------------------------------------------
179
177
Traceback (most recent call last):
180
- File "...python-tdd-book/functional_tests/tests.py", line 66 , in
178
+ File "...python-tdd-book/functional_tests/tests.py", line 59 , in
181
179
test_can_start_a_list_and_retrieve_it_later
182
- self.fail(' Finish the test!' )
180
+ self.fail(" Finish the test!" )
183
181
AssertionError: Finish the test!
184
182
185
183
---------------------------------------------------------------------
@@ -334,7 +332,7 @@ talk about the `time.sleep` in our FT:
334
332
inputbox.send_keys(Keys.ENTER)
335
333
time.sleep(1)
336
334
337
- self.check_for_row_in_list_table(' 1: Buy peacock feathers' )
335
+ self.check_for_row_in_list_table(" 1: Buy peacock feathers" )
338
336
----
339
337
====
340
338
@@ -382,30 +380,32 @@ polling/retry logic to it:
382
380
====
383
381
[source,python]
384
382
----
383
+ [...]
385
384
from selenium.common.exceptions import WebDriverException
385
+ import time
386
386
387
- MAX_WAIT = 10 #<1>
387
+ MAX_WAIT = 5 # <1>
388
388
[...]
389
389
390
390
def wait_for_row_in_list_table(self, row_text):
391
391
start_time = time.time()
392
- while True: #<2>
392
+ while True: # <2>
393
393
try:
394
- table = self.browser.find_element(By.ID, ' id_list_table' ) #<3>
395
- rows = table.find_elements(By.TAG_NAME, 'tr' )
394
+ table = self.browser.find_element(By.ID, " id_list_table" ) # <3>
395
+ rows = table.find_elements(By.TAG_NAME, "tr" )
396
396
self.assertIn(row_text, [row.text for row in rows])
397
- return #<4>
398
- except (AssertionError, WebDriverException) as e: #<5>
399
- if time.time() - start_time > MAX_WAIT: #<6>
400
- raise e #<6>
401
- time.sleep(0.5) #<5>
397
+ return # <4>
398
+ except (AssertionError, WebDriverException) as e: # <5>
399
+ if time.time() - start_time > MAX_WAIT: # <6>
400
+ raise e # <6>
401
+ time.sleep(0.5) # <5>
402
402
----
403
403
====
404
404
405
405
406
- <1> We'll use a constant called `MAX_WAIT` to set the maximum
407
- amount of time we're prepared to wait. 10 seconds should be more than
408
- enough to catch any glitches or random slowness.
406
+ <1> We'll use a constant called `MAX_WAIT`
407
+ to set the maximum amount of time we're prepared to wait.
408
+ 5 seconds should be enough to catch any glitches or random slowness.
409
409
410
410
<2> Here's the loop, which will keep going forever, unless we get to
411
411
one of two possible exit routes.
@@ -429,8 +429,6 @@ MAX_WAIT = 10 #<1>
429
429
our test, and most likely end up in our traceback, telling us why the test
430
430
failed.
431
431
432
- //TODO: see if we can't reduce this down to eg 5s and still have reliable CI
433
-
434
432
Are you thinking this code is a little ugly, and makes it a bit harder to see
435
433
exactly what we're doing? I agree. <<self.wait-for,Later on>>, we'll refactor
436
434
out a general `wait_for` helper, to separate the timing and re-raising logic
@@ -456,18 +454,18 @@ Now we can rename our method calls, and remove the voodoo ++time.sleep++s:
456
454
# When she hits enter, the page updates, and now the page lists
457
455
# "1: Buy peacock feathers" as an item in a to-do list table
458
456
inputbox.send_keys(Keys.ENTER)
459
- self.wait_for_row_in_list_table(' 1: Buy peacock feathers' )
457
+ self.wait_for_row_in_list_table(" 1: Buy peacock feathers" )
460
458
461
- # There is still a text box inviting her to add another item. She
462
- # enters "Use peacock feathers to make a fly" (Edith is very
463
- # methodical)
464
- inputbox = self.browser.find_element(By.ID, ' id_new_item' )
465
- inputbox.send_keys(' Use peacock feathers to make a fly' )
459
+ # There is still a text box inviting her to add another item.
460
+ # She enters "Use peacock feathers to make a fly"
461
+ # (Edith is very methodical)
462
+ inputbox = self.browser.find_element(By.ID, " id_new_item" )
463
+ inputbox.send_keys(" Use peacock feathers to make a fly" )
466
464
inputbox.send_keys(Keys.ENTER)
467
465
468
466
# The page updates again, and now shows both items on her list
469
- self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly' )
470
- self.wait_for_row_in_list_table('1: Buy peacock feathers' )
467
+ self.wait_for_row_in_list_table("1: Buy peacock feathers" )
468
+ self.wait_for_row_in_list_table("2: Use peacock feathers to make a fly" )
471
469
[...]
472
470
----
473
471
====
@@ -487,9 +485,9 @@ FAIL: test_can_start_a_list_and_retrieve_it_later (functional_tests.tests.NewVi
487
485
sitorTest.test_can_start_a_list_and_retrieve_it_later)
488
486
---------------------------------------------------------------------
489
487
Traceback (most recent call last):
490
- File "...python-tdd-book/functional_tests/tests.py", line 74 , in
488
+ File "...python-tdd-book/functional_tests/tests.py", line 68 , in
491
489
test_can_start_a_list_and_retrieve_it_later
492
- self.fail(' Finish the test!' )
490
+ self.fail(" Finish the test!" )
493
491
AssertionError: Finish the test!
494
492
495
493
---------------------------------------------------------------------
@@ -513,17 +511,17 @@ look for some row text that will never appear, we get the right error:
513
511
====
514
512
[source,python]
515
513
----
516
- rows = table.find_elements(By.TAG_NAME, 'tr' )
517
- self.assertIn(' foo' , [row.text for row in rows])
518
- return
514
+ rows = table.find_elements(By.TAG_NAME, "tr" )
515
+ self.assertIn(" foo" , [row.text for row in rows])
516
+ return
519
517
----
520
518
====
521
519
522
520
We see we still get a nice self-explanatory test failure message:
523
521
524
522
[subs="specialcharacters,macros"]
525
523
----
526
- self.assertIn(' foo' , [row.text for row in rows])
524
+ self.assertIn(" foo" , [row.text for row in rows])
527
525
AssertionError: 'foo' not found in ['1: Buy peacock feathers']
528
526
----
529
527
@@ -537,8 +535,8 @@ Let's put that back the way it was and break something else:
537
535
[source,python]
538
536
----
539
537
try:
540
- table = self.browser.find_element(By.ID, ' id_nothing' )
541
- rows = table.find_elements(By.TAG_NAME, 'tr' )
538
+ table = self.browser.find_element(By.ID, " id_nothing" )
539
+ rows = table.find_elements(By.TAG_NAME, "tr" )
542
540
self.assertIn(row_text, [row.text for row in rows])
543
541
return
544
542
[...]
0 commit comments