Skip to content

Commit 2dbaf5e

Browse files
committed
blackify explicit waits chap
1 parent d5b8d7a commit 2dbaf5e

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

chapter_explicit_waits_1.asciidoc

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,10 @@ class to make it use `LiveServerTestCase`:
132132
----
133133
from django.test import LiveServerTestCase
134134
from selenium import webdriver
135-
from selenium.webdriver.common.keys import Keys
136-
import time
135+
[...]
137136
138137
139138
class NewVisitorTest(LiveServerTestCase):
140-
141139
def setUp(self):
142140
[...]
143141
----
@@ -177,9 +175,9 @@ FAIL: test_can_start_a_list_and_retrieve_it_later (functional_tests.tests.NewVi
177175
sitorTest.test_can_start_a_list_and_retrieve_it_later)
178176
---------------------------------------------------------------------
179177
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
181179
test_can_start_a_list_and_retrieve_it_later
182-
self.fail('Finish the test!')
180+
self.fail("Finish the test!")
183181
AssertionError: Finish the test!
184182
185183
---------------------------------------------------------------------
@@ -334,7 +332,7 @@ talk about the `time.sleep` in our FT:
334332
inputbox.send_keys(Keys.ENTER)
335333
time.sleep(1)
336334
337-
self.check_for_row_in_list_table('1: Buy peacock feathers')
335+
self.check_for_row_in_list_table("1: Buy peacock feathers")
338336
----
339337
====
340338

@@ -382,30 +380,32 @@ polling/retry logic to it:
382380
====
383381
[source,python]
384382
----
383+
[...]
385384
from selenium.common.exceptions import WebDriverException
385+
import time
386386
387-
MAX_WAIT = 10 #<1>
387+
MAX_WAIT = 5 # <1>
388388
[...]
389389
390390
def wait_for_row_in_list_table(self, row_text):
391391
start_time = time.time()
392-
while True: #<2>
392+
while True: # <2>
393393
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")
396396
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>
402402
----
403403
====
404404

405405

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.
409409

410410
<2> Here's the loop, which will keep going forever, unless we get to
411411
one of two possible exit routes.
@@ -429,8 +429,6 @@ MAX_WAIT = 10 #<1>
429429
our test, and most likely end up in our traceback, telling us why the test
430430
failed.
431431

432-
//TODO: see if we can't reduce this down to eg 5s and still have reliable CI
433-
434432
Are you thinking this code is a little ugly, and makes it a bit harder to see
435433
exactly what we're doing? I agree. <<self.wait-for,Later on>>, we'll refactor
436434
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:
456454
# When she hits enter, the page updates, and now the page lists
457455
# "1: Buy peacock feathers" as an item in a to-do list table
458456
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")
460458
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")
466464
inputbox.send_keys(Keys.ENTER)
467465
468466
# 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")
471469
[...]
472470
----
473471
====
@@ -487,9 +485,9 @@ FAIL: test_can_start_a_list_and_retrieve_it_later (functional_tests.tests.NewVi
487485
sitorTest.test_can_start_a_list_and_retrieve_it_later)
488486
---------------------------------------------------------------------
489487
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
491489
test_can_start_a_list_and_retrieve_it_later
492-
self.fail('Finish the test!')
490+
self.fail("Finish the test!")
493491
AssertionError: Finish the test!
494492
495493
---------------------------------------------------------------------
@@ -513,17 +511,17 @@ look for some row text that will never appear, we get the right error:
513511
====
514512
[source,python]
515513
----
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
519517
----
520518
====
521519

522520
We see we still get a nice self-explanatory test failure message:
523521

524522
[subs="specialcharacters,macros"]
525523
----
526-
self.assertIn('foo', [row.text for row in rows])
524+
self.assertIn("foo", [row.text for row in rows])
527525
AssertionError: 'foo' not found in ['1: Buy peacock feathers']
528526
----
529527

@@ -537,8 +535,8 @@ Let's put that back the way it was and break something else:
537535
[source,python]
538536
----
539537
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")
542540
self.assertIn(row_text, [row.text for row in rows])
543541
return
544542
[...]

0 commit comments

Comments
 (0)