Refs #29892 -- Made Selenium tests wait for popups to be ready.

This commit is contained in:
Jon Dufresne 2019-11-28 20:06:42 -08:00 committed by Mariusz Felisiak
parent 845042b3d9
commit 65285d1e7d
3 changed files with 19 additions and 29 deletions

View File

@ -35,12 +35,15 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support.wait import WebDriverWait
WebDriverWait(self.selenium, timeout).until(callback) 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 Block until `num_windows` are present and are ready (usually 2, but can
overridden in the case of pop-ups opening other pop-ups). 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.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): def wait_for(self, css_selector, timeout=10):
""" """

View File

@ -4686,8 +4686,7 @@ class SeleniumTests(AdminSeleniumTestCase):
self.selenium.get(self.live_server_url + reverse('admin:admin_views_article_changelist')) self.selenium.get(self.live_server_url + reverse('admin:admin_views_article_changelist'))
# Change popup # Change popup
self.selenium.find_element_by_id('change_id_form-0-section').click() self.selenium.find_element_by_id('change_id_form-0-section').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window(self.selenium.window_handles[-1])
self.wait_for_text('#content h1', 'Change section') self.wait_for_text('#content h1', 'Change section')
name_input = self.selenium.find_element_by_id('id_name') name_input = self.selenium.find_element_by_id('id_name')
name_input.clear() name_input.clear()
@ -4703,8 +4702,7 @@ class SeleniumTests(AdminSeleniumTestCase):
# Add popup # Add popup
self.selenium.find_element_by_id('add_id_form-0-section').click() self.selenium.find_element_by_id('add_id_form-0-section').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window(self.selenium.window_handles[-1])
self.wait_for_text('#content h1', 'Add section') 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_id('id_name').send_keys('new section')
self.selenium.find_element_by_xpath('//input[@value="Save"]').click() 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,)) change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,))
self.selenium.get(self.live_server_url + change_url) self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('change_id_parent').click() self.selenium.find_element_by_id('change_id_parent').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window(self.selenium.window_handles[-1])
self.selenium.find_element_by_xpath('//input[@value="Save"]').click() self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0]) self.selenium.switch_to.window(self.selenium.window_handles[0])
select = Select(self.selenium.find_element_by_id('id_parent')) 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.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.get(self.live_server_url + reverse('admin:admin_views_relatedwithuuidpkmodel_add'))
self.selenium.find_element_by_id('add_id_parent').click() self.selenium.find_element_by_id('add_id_parent').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window(self.selenium.window_handles[-1])
self.selenium.find_element_by_id('id_title').send_keys('test') self.selenium.find_element_by_id('id_title').send_keys('test')
self.selenium.find_element_by_xpath('//input[@value="Save"]').click() self.selenium.find_element_by_xpath('//input[@value="Save"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0]) 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,)) change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,))
self.selenium.get(self.live_server_url + change_url) self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('delete_id_parent').click() self.selenium.find_element_by_id('delete_id_parent').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window(self.selenium.window_handles[-1])
self.selenium.find_element_by_xpath('//input[@value="Yes, Im sure"]').click() self.selenium.find_element_by_xpath('//input[@value="Yes, Im sure"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0]) self.selenium.switch_to.window(self.selenium.window_handles[0])
select = Select(self.selenium.find_element_by_id('id_parent')) 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,)) change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_change', args=(related_with_parent.id,))
self.selenium.get(self.live_server_url + change_url) self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('delete_id_parent').click() self.selenium.find_element_by_id('delete_id_parent').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window(self.selenium.window_handles[-1])
self.selenium.find_element_by_xpath('//a[text()="No, take me back"]').click() self.selenium.find_element_by_xpath('//a[text()="No, take me back"]').click()
self.selenium.switch_to.window(self.selenium.window_handles[0]) self.selenium.switch_to.window(self.selenium.window_handles[0])
self.assertEqual(len(self.selenium.window_handles), 1) 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) change_url = reverse('admin:admin_views_relatedwithuuidpkmodel_changelist', current_app=site2.name)
self.selenium.get(self.live_server_url + change_url) self.selenium.get(self.live_server_url + change_url)
self.selenium.find_element_by_id('lookup_id_form-0-parent').click() self.selenium.find_element_by_id('lookup_id_form-0-parent').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window(self.selenium.window_handles[-1])
# Select "parent2" in the popup. # Select "parent2" in the popup.
self.selenium.find_element_by_link_text(str(parent2.pk)).click() self.selenium.find_element_by_link_text(str(parent2.pk)).click()
self.selenium.switch_to.window(self.selenium.window_handles[0]) self.selenium.switch_to.window(self.selenium.window_handles[0])

View File

@ -1295,8 +1295,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
# Open the popup window and click on a band # Open the popup window and click on a band
self.selenium.find_element_by_id('lookup_id_main_band').click() self.selenium.find_element_by_id('lookup_id_main_band').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window('id_main_band')
link = self.selenium.find_element_by_link_text('Bogey Blues') link = self.selenium.find_element_by_link_text('Bogey Blues')
self.assertIn('/band/42/', link.get_attribute('href')) self.assertIn('/band/42/', link.get_attribute('href'))
link.click() link.click()
@ -1307,8 +1306,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
# Reopen the popup window and click on another band # Reopen the popup window and click on another band
self.selenium.find_element_by_id('lookup_id_main_band').click() self.selenium.find_element_by_id('lookup_id_main_band').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window('id_main_band')
link = self.selenium.find_element_by_link_text('Green Potatoes') link = self.selenium.find_element_by_link_text('Green Potatoes')
self.assertIn('/band/98/', link.get_attribute('href')) self.assertIn('/band/98/', link.get_attribute('href'))
link.click() link.click()
@ -1333,8 +1331,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
# Open the popup window and click on a band # Open the popup window and click on a band
self.selenium.find_element_by_id('lookup_id_supporting_bands').click() self.selenium.find_element_by_id('lookup_id_supporting_bands').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window('id_supporting_bands')
link = self.selenium.find_element_by_link_text('Bogey Blues') link = self.selenium.find_element_by_link_text('Bogey Blues')
self.assertIn('/band/42/', link.get_attribute('href')) self.assertIn('/band/42/', link.get_attribute('href'))
link.click() link.click()
@ -1345,8 +1342,7 @@ class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
# Reopen the popup window and click on another band # Reopen the popup window and click on another band
self.selenium.find_element_by_id('lookup_id_supporting_bands').click() self.selenium.find_element_by_id('lookup_id_supporting_bands').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window('id_supporting_bands')
link = self.selenium.find_element_by_link_text('Green Potatoes') link = self.selenium.find_element_by_link_text('Green Potatoes')
self.assertIn('/band/98/', link.get_attribute('href')) self.assertIn('/band/98/', link.get_attribute('href'))
link.click() link.click()
@ -1365,8 +1361,7 @@ class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
main_window = self.selenium.current_window_handle main_window = self.selenium.current_window_handle
# Click the Add User button to add new # Click the Add User button to add new
self.selenium.find_element_by_id('add_id_user').click() self.selenium.find_element_by_id('add_id_user').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window('id_user')
password_field = self.selenium.find_element_by_id('id_password') password_field = self.selenium.find_element_by_id('id_password')
password_field.send_keys('password') password_field.send_keys('password')
@ -1382,8 +1377,7 @@ class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
# Click the Change User button to change it # Click the Change User button to change it
self.selenium.find_element_by_id('change_id_user').click() self.selenium.find_element_by_id('change_id_user').click()
self.wait_for_popup() self.wait_for_and_switch_to_popup()
self.selenium.switch_to.window('id_user')
username_field = self.selenium.find_element_by_id('id_username') username_field = self.selenium.find_element_by_id('id_username')
username_value = 'changednewuser' username_value = 'changednewuser'