diff --git a/django/contrib/admin/tests.py b/django/contrib/admin/tests.py index 748e67fd3f..941d030d30 100644 --- a/django/contrib/admin/tests.py +++ b/django/contrib/admin/tests.py @@ -35,12 +35,15 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase): from selenium.webdriver.support.wait import WebDriverWait WebDriverWait(self.selenium, timeout).until(callback) - def wait_for_popup(self, num_windows=2, timeout=10): + def wait_for_and_switch_to_popup(self, num_windows=2, timeout=10): """ - Block until `num_windows` are present (usually 2, but can be - overridden in the case of pop-ups opening other pop-ups). + Block until `num_windows` are present and are ready (usually 2, but can + be overridden in the case of pop-ups opening other pop-ups). Switch the + current window to the new pop-up. """ self.wait_until(lambda d: len(d.window_handles) == num_windows, timeout) + self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_page_ready() def wait_for(self, css_selector, timeout=10): """ diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 1c1c3ab918..71db79ae2e 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -4686,8 +4686,7 @@ class SeleniumTests(AdminSeleniumTestCase): self.selenium.get(self.live_server_url + reverse('admin:admin_views_article_changelist')) # Change popup self.selenium.find_element_by_id('change_id_form-0-section').click() - self.wait_for_popup() - self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_for_and_switch_to_popup() self.wait_for_text('#content h1', 'Change section') name_input = self.selenium.find_element_by_id('id_name') name_input.clear() @@ -4703,8 +4702,7 @@ class SeleniumTests(AdminSeleniumTestCase): # Add popup self.selenium.find_element_by_id('add_id_form-0-section').click() - self.wait_for_popup() - self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_for_and_switch_to_popup() self.wait_for_text('#content h1', 'Add section') self.selenium.find_element_by_id('id_name').send_keys('new section') self.selenium.find_element_by_xpath('//input[@value="Save"]').click() @@ -4723,8 +4721,7 @@ class SeleniumTests(AdminSeleniumTestCase): change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,)) self.selenium.get(self.live_server_url + change_url) self.selenium.find_element_by_id('change_id_parent').click() - self.wait_for_popup() - self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_for_and_switch_to_popup() self.selenium.find_element_by_xpath('//input[@value="Save"]').click() self.selenium.switch_to.window(self.selenium.window_handles[0]) select = Select(self.selenium.find_element_by_id('id_parent')) @@ -4736,8 +4733,7 @@ class SeleniumTests(AdminSeleniumTestCase): self.admin_login(username='super', password='secret', login_url=reverse('admin:index')) self.selenium.get(self.live_server_url + reverse('admin:admin_views_relatedwithuuidpkmodel_add')) self.selenium.find_element_by_id('add_id_parent').click() - self.wait_for_popup() - self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_for_and_switch_to_popup() self.selenium.find_element_by_id('id_title').send_keys('test') self.selenium.find_element_by_xpath('//input[@value="Save"]').click() self.selenium.switch_to.window(self.selenium.window_handles[0]) @@ -4754,8 +4750,7 @@ class SeleniumTests(AdminSeleniumTestCase): change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,)) self.selenium.get(self.live_server_url + change_url) self.selenium.find_element_by_id('delete_id_parent').click() - self.wait_for_popup() - self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_for_and_switch_to_popup() self.selenium.find_element_by_xpath('//input[@value="Yes, I’m sure"]').click() self.selenium.switch_to.window(self.selenium.window_handles[0]) select = Select(self.selenium.find_element_by_id('id_parent')) @@ -4771,8 +4766,7 @@ class SeleniumTests(AdminSeleniumTestCase): change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,)) self.selenium.get(self.live_server_url + change_url) self.selenium.find_element_by_id('delete_id_parent').click() - self.wait_for_popup() - self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_for_and_switch_to_popup() self.selenium.find_element_by_xpath('//a[text()="No, take me back"]').click() self.selenium.switch_to.window(self.selenium.window_handles[0]) self.assertEqual(len(self.selenium.window_handles), 1) @@ -4785,8 +4779,7 @@ class SeleniumTests(AdminSeleniumTestCase): change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_changelist', current_app=site2.name) self.selenium.get(self.live_server_url + change_url) self.selenium.find_element_by_id('lookup_id_form-0-parent').click() - self.wait_for_popup() - self.selenium.switch_to.window(self.selenium.window_handles[-1]) + self.wait_for_and_switch_to_popup() # Select "parent2" in the popup. self.selenium.find_element_by_link_text(str(parent2.pk)).click() self.selenium.switch_to.window(self.selenium.window_handles[0]) diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 851e7eb123..719d0d8095 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -1295,8 +1295,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase): # Open the popup window and click on a band self.selenium.find_element_by_id('lookup_id_main_band').click() - self.wait_for_popup() - self.selenium.switch_to.window('id_main_band') + self.wait_for_and_switch_to_popup() link = self.selenium.find_element_by_link_text('Bogey Blues') self.assertIn('/band/42/', link.get_attribute('href')) link.click() @@ -1307,8 +1306,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase): # Reopen the popup window and click on another band self.selenium.find_element_by_id('lookup_id_main_band').click() - self.wait_for_popup() - self.selenium.switch_to.window('id_main_band') + self.wait_for_and_switch_to_popup() link = self.selenium.find_element_by_link_text('Green Potatoes') self.assertIn('/band/98/', link.get_attribute('href')) link.click() @@ -1333,8 +1331,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase): # Open the popup window and click on a band self.selenium.find_element_by_id('lookup_id_supporting_bands').click() - self.wait_for_popup() - self.selenium.switch_to.window('id_supporting_bands') + self.wait_for_and_switch_to_popup() link = self.selenium.find_element_by_link_text('Bogey Blues') self.assertIn('/band/42/', link.get_attribute('href')) link.click() @@ -1345,8 +1342,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase): # Reopen the popup window and click on another band self.selenium.find_element_by_id('lookup_id_supporting_bands').click() - self.wait_for_popup() - self.selenium.switch_to.window('id_supporting_bands') + self.wait_for_and_switch_to_popup() link = self.selenium.find_element_by_link_text('Green Potatoes') self.assertIn('/band/98/', link.get_attribute('href')) link.click() @@ -1365,8 +1361,7 @@ class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase): main_window = self.selenium.current_window_handle # Click the Add User button to add new self.selenium.find_element_by_id('add_id_user').click() - self.wait_for_popup() - self.selenium.switch_to.window('id_user') + self.wait_for_and_switch_to_popup() password_field = self.selenium.find_element_by_id('id_password') password_field.send_keys('password') @@ -1382,8 +1377,7 @@ class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase): # Click the Change User button to change it self.selenium.find_element_by_id('change_id_user').click() - self.wait_for_popup() - self.selenium.switch_to.window('id_user') + self.wait_for_and_switch_to_popup() username_field = self.selenium.find_element_by_id('id_username') username_value = 'changednewuser'