-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for vanilla Django RawID fields
Since we overwrite the Django popup lookup JS, we need to make sure we don't break the vanilla experience.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
""" | ||
Test vanilla Django raw id fields since we overwrite it's Javascript | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
from selenium.webdriver.common.by import By | ||
|
||
if TYPE_CHECKING: | ||
from selenium.webdriver.firefox.webdriver import WebDriver | ||
|
||
|
||
@pytest.mark.usefixtures("_add_page", "_sample_primary_keys") | ||
def test_widgets(selenium: WebDriver) -> None: | ||
""" | ||
Test the variations of the Dynamic Raw ID Widget. | ||
The tests select the "Lookup" icon, choose the first element in the result set, | ||
and test that the selected element name appears next to the field. | ||
Doing this in a all-in-one testcase, rather a parameterized test, so it's much | ||
faster. | ||
""" | ||
|
||
# Input Field ID, Select Value, Test Value | ||
tests = ( | ||
("rawid_fk", "user1", "user1"), | ||
("rawid_fk_limited", "admin", "admin"), | ||
("rawid_many", "user1", None), | ||
("rawid_many", "user2", None), # Second click on Many2Many | ||
) | ||
|
||
for id_value, select_value, _ in tests: | ||
# Select the "Lookup" icon next to the field | ||
selenium.find_element(By.ID, f"lookup_id_{id_value}").click() | ||
|
||
# Activate the popup window with the `window.name = <window_id>` | ||
selenium.switch_to.window(selenium.window_handles[1]) | ||
|
||
# Select the result element matching the link text | ||
selenium.find_element(By.LINK_TEXT, select_value).click() | ||
|
||
# Activate the default window | ||
selenium.switch_to.window(selenium.window_handles[0]) | ||
|
||
# Click the save button and wait for page reload | ||
selenium.find_element(By.NAME, "_continue").click() | ||
selenium.implicitly_wait(0.5) # Wait a bit for page reload | ||
|
||
# Test that all selected ForeignKeys are now displayed after reload. | ||
for id_value, _, test_value in tests: | ||
# ManyToMany fields don't display a label in Vanilla Django raw id fields. | ||
if test_value is None: | ||
continue | ||
|
||
label = selenium.find_element(By.CSS_SELECTOR, f"#id_{id_value} ~ strong") | ||
assert label.text == test_value |