Skip to content

Commit

Permalink
[5.0.x] Refs #34995 -- Made Selenium tests more robust for admin_view…
Browse files Browse the repository at this point in the history
…s and admin_widgets suites.

Depending on screen sizes, the selenium tests that would "click" or interact
with an element outside the scope of the visible window would produce test
failures (raising ElementNotInteractableException in CI runs).

This branch switches those to using ActionChains, which ensures that the click
(or other interaction) is performed only after successfully completing the
move to the relevant element.

Co-authored-by: Tom Carrick <tom@carrick.eu>

Backport of af2fd36 from main
  • Loading branch information
sarahboyce authored and nessita committed Nov 30, 2023
1 parent 7f1dc67 commit 9fe12b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
25 changes: 18 additions & 7 deletions tests/admin_views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6246,6 +6246,7 @@ def test_list_editable_popups(self):
self.assertEqual(select2_display.text, \nnew section")

def test_inline_uuid_pk_edit_with_popup(self):
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

Expand All @@ -6258,8 +6259,10 @@ def test_inline_uuid_pk_edit_with_popup(self):
"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()
with self.wait_page_loaded():
self.selenium.get(self.live_server_url + change_url)
change_parent = self.selenium.find_element(By.ID, "change_id_parent")
ActionChains(self.selenium).move_to_element(change_parent).click().perform()
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])
Expand Down Expand Up @@ -6292,6 +6295,7 @@ def test_inline_uuid_pk_add_with_popup(self):
self.assertEqual(select.first_selected_option.get_attribute("value"), uuid_id)

def test_inline_uuid_pk_delete_with_popup(self):
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

Expand All @@ -6304,8 +6308,10 @@ def test_inline_uuid_pk_delete_with_popup(self):
"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()
with self.wait_page_loaded():
self.selenium.get(self.live_server_url + change_url)
delete_parent = self.selenium.find_element(By.ID, "delete_id_parent")
ActionChains(self.selenium).move_to_element(delete_parent).click().perform()
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])
Expand All @@ -6316,6 +6322,7 @@ def test_inline_uuid_pk_delete_with_popup(self):

def test_inline_with_popup_cancel_delete(self):
"""Clicking ""No, take me back" on a delete popup closes the window."""
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

parent = ParentWithUUIDPK.objects.create(title="test")
Expand All @@ -6327,8 +6334,10 @@ def test_inline_with_popup_cancel_delete(self):
"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()
with self.wait_page_loaded():
self.selenium.get(self.live_server_url + change_url)
delete_parent = self.selenium.find_element(By.ID, "delete_id_parent")
ActionChains(self.selenium).move_to_element(delete_parent).click().perform()
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])
Expand Down Expand Up @@ -6519,6 +6528,7 @@ def test_hidden_fields_small_window(self):
self.selenium.set_window_size(current_size["width"], current_size["height"])

def test_updating_related_objects_updates_fk_selects_except_autocompletes(self):
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

Expand Down Expand Up @@ -6580,7 +6590,8 @@ def _get_text_inside_element_by_selector(selector):
)

# Add new Country from the living_country select.
self.selenium.find_element(By.ID, f"add_{living_country_select_id}").click()
element = self.selenium.find_element(By.ID, f"add_{living_country_select_id}")
ActionChains(self.selenium).move_to_element(element).click(element).perform()
self.wait_for_and_switch_to_popup()
self.selenium.find_element(By.ID, "id_name").send_keys("Spain")
continent_select = Select(
Expand Down
11 changes: 7 additions & 4 deletions tests/admin_widgets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1713,13 +1713,15 @@ def test_many_to_many(self):

class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
def test_ForeignKey_using_to_field(self):
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

self.admin_login(username="super", password="secret", login_url="/")
self.selenium.get(
self.live_server_url + reverse("admin:admin_widgets_profile_add")
)
with self.wait_page_loaded():
self.selenium.get(
self.live_server_url + reverse("admin:admin_widgets_profile_add")
)

main_window = self.selenium.current_window_handle
# Click the Add User button to add new
Expand Down Expand Up @@ -1765,7 +1767,8 @@ def test_ForeignKey_using_to_field(self):
By.CSS_SELECTOR, "#id_user option[value=changednewuser]"
)

self.selenium.find_element(By.ID, "view_id_user").click()
element = self.selenium.find_element(By.ID, "view_id_user")
ActionChains(self.selenium).move_to_element(element).click(element).perform()
self.wait_for_value("#id_username", "changednewuser")
self.selenium.back()

Expand Down

0 comments on commit 9fe12b0

Please sign in to comment.