|
| 1 | +import os |
1 | 2 | from http import HTTPStatus |
2 | 3 | from io import StringIO |
3 | 4 |
|
4 | 5 | from django.conf import settings |
| 6 | +from django.contrib.staticfiles.testing import StaticLiveServerTestCase |
5 | 7 | from django.core.management import call_command |
6 | 8 | from django.test import TestCase |
7 | 9 | from django.urls import NoReverseMatch, get_resolver |
8 | 10 | from django.utils.translation import activate, gettext as _ |
9 | 11 | from django_hosts.resolvers import reverse |
| 12 | +from playwright.sync_api import expect, sync_playwright |
10 | 13 |
|
11 | 14 | from docs.models import DocumentRelease, Release |
12 | 15 |
|
@@ -229,3 +232,53 @@ class SiteMapTests(TestCase): |
229 | 232 | def test_sitemap_renders(self): |
230 | 233 | response = self.client.get(reverse("sitemap")) |
231 | 234 | self.assertEqual(response.status_code, 200) |
| 235 | + |
| 236 | + |
| 237 | +class EndToEndTests(ReleaseMixin, StaticLiveServerTestCase): |
| 238 | + @classmethod |
| 239 | + def setUpClass(cls): |
| 240 | + os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" |
| 241 | + super().setUpClass() |
| 242 | + cls.playwright = sync_playwright().start() |
| 243 | + cls.browser = cls.playwright.chromium.launch() |
| 244 | + cls.mac_user_agent = "Mozilla/5.0 (Macintosh) AppleWebKit" |
| 245 | + cls.windows_user_agent = "Mozilla/5.0 (Windows NT 10.0)" |
| 246 | + cls.mobile_linux_user_agent = "Mozilla/5.0 (Linux; Android 10; Mobile)" |
| 247 | + |
| 248 | + @classmethod |
| 249 | + def tearDownClass(cls): |
| 250 | + super().tearDownClass() |
| 251 | + cls.browser.close() |
| 252 | + cls.playwright.stop() |
| 253 | + |
| 254 | + def setUp(self): |
| 255 | + super().setUp() |
| 256 | + self.setUpTestData() |
| 257 | + |
| 258 | + def test_search_ctrl_k_hotkey(self): |
| 259 | + page1 = self.browser.new_page(user_agent=self.windows_user_agent) |
| 260 | + page2 = self.browser.new_page( |
| 261 | + user_agent=self.mobile_linux_user_agent, |
| 262 | + viewport={"width": 375, "height": 812}, |
| 263 | + ) |
| 264 | + for page in [page1, page2]: |
| 265 | + with self.subTest(page=page): |
| 266 | + page.goto(self.live_server_url) |
| 267 | + search_bar = page.locator("#id_q") |
| 268 | + expect(search_bar).to_have_attribute("placeholder", "Search (Ctrl+K)") |
| 269 | + is_focused = page.evaluate("document.activeElement.id === 'id_q'") |
| 270 | + self.assertFalse(is_focused) |
| 271 | + |
| 272 | + page.keyboard.press("Control+KeyK") |
| 273 | + is_focused = page.evaluate("document.activeElement.id === 'id_q'") |
| 274 | + self.assertTrue(is_focused) |
| 275 | + page.close() |
| 276 | + |
| 277 | + def test_search_placeholder_mac_mode(self): |
| 278 | + page = self.browser.new_page(user_agent=self.mac_user_agent) |
| 279 | + page.goto(self.live_server_url) |
| 280 | + |
| 281 | + desktop_search_bar = page.locator("#id_q") |
| 282 | + expect(desktop_search_bar).to_have_attribute("placeholder", "Search (⌘\u200aK)") |
| 283 | + |
| 284 | + page.close() |
0 commit comments