diff --git a/course_discovery/apps/tagging/tests/test_views.py b/course_discovery/apps/tagging/tests/test_views.py index 973195038b..7643c3dd87 100644 --- a/course_discovery/apps/tagging/tests/test_views.py +++ b/course_discovery/apps/tagging/tests/test_views.py @@ -1,8 +1,12 @@ from ddt import data, ddt from django.conf import settings from django.contrib.auth.models import Group -from django.test import TestCase +from django.test import LiveServerTestCase, TestCase from django.urls import reverse +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.firefox.options import Options +from selenium.webdriver.support.ui import Select, WebDriverWait from course_discovery.apps.core.tests.factories import UserFactory from course_discovery.apps.course_metadata.tests.factories import CourseFactory @@ -68,6 +72,118 @@ def test_post_invalid_sub_vertical(self): self.assertContains(response, 'Sub-vertical does not belong to the selected vertical.') +class CourseTaggingDetailViewJSTests(LiveServerTestCase): + """ + Functional tests using Selenium to verify the JS script filterSubVerticals behavior in the CourseTaggingDetailView. + """ + + @classmethod + def setUpClass(cls): + super().setUpClass() + firefox_options = Options() + firefox_options.headless = True + cls.driver = webdriver.Firefox(options=firefox_options) + cls.driver.implicitly_wait(10) + + @classmethod + def tearDownClass(cls): + cls.driver.quit() + super().tearDownClass() + + def setUp(self): + """ + Create a superuser, a course, verticals and sub-verticals used for testing. + """ + self.user = UserFactory(username='superuser_func', is_superuser=True, is_staff=True) + self.user.set_password('password') + self.user.save() + + self.course = CourseFactory(title='Advanced Python') + + self.vertical = VerticalFactory(name='AI') + self.sub_vertical = SubVerticalFactory(name='Machine Learning', vertical=self.vertical) + + self.other_vertical = VerticalFactory(name='Business') + self.other_sub_vertical = SubVerticalFactory(name='Innovation', vertical=self.other_vertical) + + self.multi_vertical = VerticalFactory(name='Data') + self.multi_sub_vertical1 = SubVerticalFactory(name='Analytics', vertical=self.multi_vertical) + self.multi_sub_vertical2 = SubVerticalFactory(name='Visualization', vertical=self.multi_vertical) + + self.url = self.live_server_url + reverse('tagging:course_tagging_detail', kwargs={'uuid': self.course.uuid}) + self.login_url = self.live_server_url + reverse('admin:login') + + # Login via the admin login page as it does not share cookies with Django's test client. + self.driver.get(self.login_url) + self.driver.find_element(By.NAME, 'username').send_keys(self.user.username) + self.driver.find_element(By.NAME, 'password').send_keys('password') + self.driver.find_element(By.XPATH, "//input[@type='submit']").click() + + self.driver.get(self.url) + + def get_visible_options(self, select_id): + """ + Returns a list of visible