diff --git a/django/contrib/admin/tests.py b/django/contrib/admin/tests.py index 248a5798dd..a146539f82 100644 --- a/django/contrib/admin/tests.py +++ b/django/contrib/admin/tests.py @@ -51,8 +51,29 @@ class AdminSeleniumWebDriverTestCase(StaticLiveServerCase): Helper function that blocks until the element with the given tag name is found on the page. """ + self.wait_for(tag_name, timeout) + + def wait_for(self, css_selector, timeout=10): + """ + Helper function that blocks until an css selector is found on the page. + """ + from selenium.common.exceptions import NoSuchElementException, WebDriverException + from selenium.webdriver.common.by import By + from selenium.webdriver.support import expected_conditions as ec self.wait_until( - lambda driver: driver.find_element_by_tag_name(tag_name), + ec.presence_of_element_located((By.CSS_SELECTOR, css_selector)), + timeout + ) + + def wait_for_text(self, css_selector, text, timeout=10): + """ + Helper function that blocks until the text is found in the css selector. + """ + from selenium.webdriver.common.by import By + from selenium.webdriver.support import expected_conditions as ec + self.wait_until( + ec.text_to_be_present_in_element_value( + (By.CSS_SELECTOR, css_selector), text), timeout ) diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 33833f3eff..794cd7924c 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -993,9 +993,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase): # The field now contains the selected band's id self.selenium.switch_to_window(main_window) - self.assertEqual( - self.selenium.find_element_by_id('id_main_band').get_attribute('value'), - '42') + self.wait_for_text('#id_main_band', '42') # Reopen the popup window and click on another band self.selenium.find_element_by_id('lookup_id_main_band').click() @@ -1007,9 +1005,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase): # The field now contains the other selected band's id self.selenium.switch_to_window(main_window) - self.assertEqual( - self.selenium.find_element_by_id('id_main_band').get_attribute('value'), - '98') + self.wait_for_text('#id_main_band', '98') def test_many_to_many(self): self.admin_login(username='super', password='secret', login_url='/') @@ -1032,9 +1028,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase): # The field now contains the selected band's id self.selenium.switch_to_window(main_window) - self.assertEqual( - self.selenium.find_element_by_id('id_supporting_bands').get_attribute('value'), - '42') + self.wait_for_text('#id_supporting_bands', '42') # Reopen the popup window and click on another band self.selenium.find_element_by_id('lookup_id_supporting_bands').click() @@ -1046,9 +1040,7 @@ class AdminRawIdWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase): # The field now contains the two selected bands' ids self.selenium.switch_to_window(main_window) - self.assertEqual( - self.selenium.find_element_by_id('id_supporting_bands').get_attribute('value'), - '42,98') + self.wait_for_text('#id_supporting_bands', '42,98') class AdminRawIdWidgetSeleniumChromeTests(AdminRawIdWidgetSeleniumFirefoxTests): @@ -1087,10 +1079,8 @@ class RelatedFieldWidgetSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase): save_button_css_selector = '.submit-row > input[type=submit]' self.selenium.find_element_by_css_selector(save_button_css_selector).click() self.selenium.switch_to_window(main_window) - # Wait up to 2 seconds for the new option to show up after clicking save in the popup. - self.selenium.implicitly_wait(2) - self.selenium.find_element_by_css_selector('#id_user option[value=newuser]') - self.selenium.implicitly_wait(0) + # The field now contains the new user + self.wait_for('#id_user option[value="newuser"]') # Go ahead and submit the form to make sure it works self.selenium.find_element_by_css_selector(save_button_css_selector).click()