8000 Removed hardcoded pks in admin selenium tests. · feanil/django@f5197be · GitHub
[go: up one dir, main page]

Skip to content

Commit f5197be

Browse files
authored
Removed hardcoded pks in admin selenium tests.
1 parent 84e9126 commit f5197be

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

tests/admin_views/tests.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
Collector,
7070
Color,
7171
ComplexSortedPerson,
72+
Country,
7273
CoverLetter,
7374
CustomArticle,
7475
CyclicOne,
@@ -6698,11 +6699,12 @@ def _get_text_inside_element_by_selector(selector):
66986699
self.wait_until(lambda d: len(d.window_handles) == 1, 1)
66996700
self.selenium.switch_to.window(self.selenium.window_handles[0])
67006701

6702+
argentina = Country.objects.get(name="Argentina")
67016703
self.assertHTMLEqual(
67026704
_get_HTML_inside_element_by_id(born_country_select_id),
6703-
"""
6705+
f"""
67046706
<option value="" selected="">---------</option>
6705-
<option value="1" selected="">Argentina</option>
6707+
<option value="{argentina.pk}" selected="">Argentina</option>
67066708
""",
67076709
)
67086710
# Argentina isn't added to the living_country select nor selected by
@@ -6736,12 +6738,13 @@ def _get_text_inside_element_by_selector(selector):
67366738
self.wait_until(lambda d: len(d.window_handles) == 1, 1)
67376739
self.selenium.switch_to.window(self.selenium.window_handles[0])
67386740

6741+
spain = Country.objects.get(name="Spain")
67396742
self.assertHTMLEqual(
67406743
_get_HTML_inside_element_by_id(born_country_select_id),
6741-
"""
6744+
f"""
67426745
<option value="" selected="">---------</option>
6743-
<option value="1" selected="">Argentina</option>
6744-
<option value="2">Spain</option>
6746+
<option value="{argentina.pk}" selected="">Argentina</option>
6747+
<option value="{spain.pk}">Spain</option>
67456748
""",
67466749
)
67476750

@@ -6778,12 +6781,13 @@ def _get_text_inside_element_by_selector(selector):
67786781
self.wait_until(lambda d: len(d.window_handles) == 1, 1)
67796782
self.selenium.switch_to.window(self.selenium.window_handles[0])
67806783

6784+
italy = spain
67816785
self.assertHTMLEqual(
67826786
_get_HTML_inside_element_by_id(born_country_select_id),
6783-
"""
6787+
f"""
67846788
<option value="" selected="">---------</option>
6785-
<option value="1" selected="">Argentina</option>
6786-
<option value="2">Italy</option>
6789+
<option value="{argentina.pk}" selected="">Argentina</option>
6790+
<option value="{italy.pk}">Italy</option>
67876791
""",
67886792
)
67896793
# Italy is added to the living_country select and it's also selected by

tests/admin_widgets/tests.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,8 +1740,8 @@ def test_refresh_page(self):
17401740
class AdminRawIdWidgetSeleniumTests(AdminWidgetSeleniumTestCase):
17411741
def setUp(self):
17421742
super().setUp()
1743-
Band.objects.create(id=42, name="Bogey Blues")
1744-
Band.objects.create(id=98, name="Green Potatoes")
1743+
self.blues = Band.objects.create(name="Bogey Blues")
1744+
self.potatoes = Band.objects.create(name="Green Potatoes")
17451745

17461746
@screenshot_cases(["desktop_size", "mobile_size", "rtl", "dark", "high_contrast"])
17471747
def test_ForeignKey(self):
@@ -1763,23 +1763,23 @@ def test_ForeignKey(self):
17631763
self.selenium.find_element(By.ID, "lookup_id_main_band").click()
17641764
self.wait_for_and_switch_to_popup()
17651765
link = self.selenium.find_element(By.LINK_TEXT, "Bogey Blues")
1766-
self.assertIn("/band/42/", link.get_attribute("href"))
1766+
self.assertIn(f"/band/{self.blues.pk}/", link.get_attribute("href"))
17671767
link.click()
17681768

17691769
# The field now contains the selected band's id
17701770
self.selenium.switch_to.window(main_window)
1771-
self.wait_for_value("#id_main_band", "42")
1771+
self.wait_for_value("#id_main_band", str(self.blues.pk))
17721772

17731773
# Reopen the popup window and click on another band
17741774
self.selenium.find_element(By.ID, "lookup_id_main_band").click()
17751775
self.wait_for_and_switch_to_popup()
17761776
link = self.selenium.find_element(By.LINK_TEXT, "Green Potatoes")
1777-
self.assertIn("/band/98/", link.get_attribute("href"))
1777+
self.assertIn(f"/band/{self.potatoes.pk}/", link.get_attribute("href"))
17781778
link.click()
17791779

17801780
# The field now contains the other selected band's id
17811781
self.selenium.switch_to.window(main_window)
1782-
self.wait_for_value("#id_main_band", "98")
1782+
self.wait_for_value("#id_main_band", str(self.potatoes.pk))
17831783

17841784
def test_many_to_many(self):
17851785
from selenium.webdriver.common.by import By
@@ -1810,23 +1810,25 @@ def test_many_to_many(self):
18101810
self.selenium.find_element(By.ID, "lookup_id_supporting_bands").click()
18111811
self.wait_for_and_switch_to_popup()
18121812
link = self.selenium.find_element(By.LINK_TEXT, "Bogey Blues")
1813-
self.assertIn("/band/42/", link.get_attribute("href"))
1813+
self.assertIn(f"/band/{self.blues.pk}/", link.get_attribute("href"))
18141814
link.click()
18151815

18161816
# The field now contains the selected band's id
18171817
self.selenium.switch_to.window(main_window)
1818-
self.wait_for_value("#id_supporting_bands", "42")
1818+
self.wait_for_value("#id_supporting_bands", str(self.blues.pk))
18191819

18201820
# Reopen the popup window and click on another band
18211821
self.selenium.find_element(By.ID, "lookup_id_supporting_bands").click()
18221822
self.wait_for_and_switch_to_popup()
18231823
link = self.selenium.find_element(By.LINK_TEXT, "Green Potatoes")
1824-
self.assertIn("/band/98/", link.get_attribute("href"))
1824+
self.assertIn(f"/band/{self.potatoes.pk}/", link.get_attribute("href"))
18251825
link.click()
18261826

18271827
# The field now contains the two selected bands' ids
18281828
self.selenium.switch_to.window(main_window)
1829-
self.wait_for_value("#id_supporting_bands", "42,98")
1829+
self.wait_for_value(
1830+
"#id_supporting_bands", f"{self.blues.pk},{self.potatoes.pk}"
1831+
)
18301832

18311833

18321834
class RelatedFieldWidgetSeleniumTests(AdminWidgetSeleniumTestCase):

0 commit comments

Comments
 (0)
0