Bläddra i källkod

Replace explicit wait with polling function

Benoît Hubert 7 år sedan
förälder
incheckning
25323dcc34
1 ändrade filer med 19 tillägg och 9 borttagningar
  1. 19 9
      functional_tests/tests.py

+ 19 - 9
functional_tests/tests.py

@@ -1,9 +1,13 @@
 from django.test import LiveServerTestCase
 from selenium import webdriver
 from selenium.webdriver.common.keys import Keys
+from selenium.common.exceptions import WebDriverException
+
 import time
 import unittest
 
+MAX_WAIT = 10
+
 class NewVisitorTest(LiveServerTestCase):
 
     def setUp(self):
@@ -12,10 +16,18 @@ class NewVisitorTest(LiveServerTestCase):
     def tearDown(self):
         self.browser.quit()
 
-    def check_for_row_in_list_table(self, row_text):
-        table = self.browser.find_element_by_id('movie-table')
-        rows = table.find_elements_by_tag_name('tr')
-        self.assertIn(row_text, [row.text for row in rows])
+    def wait_for_row_in_list_table(self, row_text):
+        start_time = time.time()
+        while True:
+            try:
+                table = self.browser.find_element_by_id('movie-table')
+                rows = table.find_elements_by_tag_name('tr')
+                self.assertIn(row_text, [row.text for row in rows])
+                return
+            except (AssertionError, WebDriverException) as e:
+                if time.time() - start_time > MAX_WAIT:
+                    raise e
+                time.sleep(0.5)
 
     def test_can_start_a_list_and_retrieve_it_later(self):
         # Edith has heard about a cool new online to-do app. She goes
@@ -40,16 +52,14 @@ class NewVisitorTest(LiveServerTestCase):
         # When she hits enter, the page updates, and now the page lists
         # "1: Inception" as an item in a to-do list table
         inputbox.send_keys(Keys.ENTER)
-        time.sleep(1)
-        self.check_for_row_in_list_table('1: Inception')
+        self.wait_for_row_in_list_table('1: Inception')
 
         inputbox = self.browser.find_element_by_id('input-new-movie')
         inputbox.send_keys('The Sixth Sense')
         inputbox.send_keys(Keys.ENTER)
-        time.sleep(1)
 
-        self.check_for_row_in_list_table('1: Inception')
-        self.check_for_row_in_list_table('2: The Sixth Sense')
+        self.wait_for_row_in_list_table('1: Inception')
+        self.wait_for_row_in_list_table('2: The Sixth Sense')
 
         # There is still a text box inviting her to add another item. She
         # enters "Armageddon"