Made some Selenium tests less flaky by adding explicit wait.

This commit is contained in:
sarahboyce 2023-07-04 11:10:37 +02:00 committed by Mariusz Felisiak
parent 42b4f81e6e
commit 649262a406
2 changed files with 21 additions and 12 deletions

View File

@ -431,16 +431,19 @@ class SeleniumTests(AdminSeleniumTestCase):
self.live_server_url + reverse("autocomplete_admin:admin_views_answer_add")
)
elem = self.selenium.find_element(By.CSS_SELECTOR, ".select2-selection")
with self.select2_ajax_wait():
elem.click() # Open the autocomplete dropdown.
results = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results")
self.assertTrue(results.is_displayed())
option = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results__option")
self.assertEqual(option.text, "No results found")
with self.select2_ajax_wait():
elem.click() # Close the autocomplete dropdown.
q1 = Question.objects.create(question="Who am I?")
Question.objects.bulk_create(
Question(question=str(i)) for i in range(PAGINATOR_SIZE + 10)
)
with self.select2_ajax_wait():
elem.click() # Reopen the dropdown now that some objects exist.
result_container = self.selenium.find_element(
By.CSS_SELECTOR, ".select2-results"
@ -478,6 +481,7 @@ class SeleniumTests(AdminSeleniumTestCase):
".select2-results__option", 1, root_element=result_container
)
# Select the result.
with self.select2_ajax_wait():
search.send_keys(Keys.RETURN)
select = Select(self.selenium.find_element(By.ID, "id_question"))
self.assertEqual(
@ -500,11 +504,13 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertTrue(results.is_displayed())
option = self.selenium.find_element(By.CSS_SELECTOR, ".select2-results__option")
self.assertEqual(option.text, "No results found")
with self.select2_ajax_wait():
elem.click() # Close the autocomplete dropdown.
Question.objects.create(question="Who am I?")
Question.objects.bulk_create(
Question(question=str(i)) for i in range(PAGINATOR_SIZE + 10)
)
with self.select2_ajax_wait():
elem.click() # Reopen the dropdown now that some objects exist.
result_container = self.selenium.find_element(
By.CSS_SELECTOR, ".select2-results"
@ -536,6 +542,7 @@ class SeleniumTests(AdminSeleniumTestCase):
self.assertCountSeleniumElements(
".select2-results__option", 1, root_element=result_container
)
with self.select2_ajax_wait():
# Select the result.
search.send_keys(Keys.RETURN)
# Reopen the dropdown and add the first result to the selection.

View File

@ -6111,6 +6111,7 @@ class SeleniumTests(AdminSeleniumTestCase):
name_input.clear()
name_input.send_keys("<i>edited section</i>")
self.selenium.find_element(By.XPATH, '//input[@value="Save"]').click()
self.wait_until(lambda d: len(d.window_handles) == 1, 1)
self.selenium.switch_to.window(self.selenium.window_handles[0])
# Hide sidebar.
toggle_button = self.selenium.find_element(
@ -6133,6 +6134,7 @@ class SeleniumTests(AdminSeleniumTestCase):
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()
self.wait_until(lambda d: len(d.window_handles) == 1, 1)
self.selenium.switch_to.window(self.selenium.window_handles[0])
select = Select(self.selenium.find_element(By.ID, "id_form-0-section"))
self.assertEqual(select.first_selected_option.text, "new section")