More testing of Add Course form
authorCharles Connell <charles@connells.org>
Mon, 16 Dec 2013 20:05:28 +0000 (15:05 -0500)
committerCharles Connell <charles@connells.org>
Tue, 17 Dec 2013 04:27:37 +0000 (23:27 -0500)
karmaworld/assets/js/add-course.js
test/selenium/add_course.py

index d46e1f6e216e05bf6cd96dc01c901fc473fc0445..f00567098fb418a98f2de052ccc89a266695a656 100644 (file)
@@ -10,9 +10,11 @@ $(function() {
   function fieldEdited() {
     if (schoolSelected && courseNameSelected && instructorSelected) {
       $('#save-btn').hide();
+      $('#save-btn').addClass('disabled');
       $('#existing-course-msg').show();
     } else {
       $('#save-btn').show();
+      $('#save-btn').removeClass('disabled');
       $('#existing-course-msg').hide();
     }
   }
index 2a931eeebfcfd7e7f6f1529eb297e44e7578da57..679edeeef79746d56b734ac07e69fd0320fbac5d 100644 (file)
 from selenium import webdriver
+from selenium.webdriver.support.wait import WebDriverWait
 from selenium.webdriver.common.by import By
 from selenium.webdriver.common.keys import Keys
 from selenium.webdriver.support import expected_conditions as EC
+import uuid
 import unittest
 
-# Create a new instance of the Firefox driver
-from selenium.webdriver.support.wait import WebDriverWait
 
 class AddCourseTest(unittest.TestCase):
+    """Tests the Add Course form. Requires a copy of KarmaNotes
+    be available at localhost:8000. This will modify your database."""
+    
+    def setUp(self):
+        self.driver = webdriver.Firefox()
+        self.driver.implicitly_wait(3)
+        self.wait = WebDriverWait(self.driver, 10)
+
+    def tearDown(self):
+        self.driver.close()
+
+    def selectAutocomplete(self, inputId, keys, fieldIndex):
+        input = self.driver.find_element_by_id(inputId)
+        input.send_keys(keys)
+        self.wait.until(EC.element_to_be_clickable((By.XPATH, "//ul[contains(@class,'ui-autocomplete')][" + str(fieldIndex) + "]/li")))
+        input.send_keys(Keys.DOWN)
+        autocompleteMenuItem = self.driver.find_element_by_id("ui-active-menuitem")
+        autocompleteMenuItem.click()
+
+    def testSchoolName(self):
+        self.driver.get("http://localhost:8000/")
+
+        # Click "Add Course"
+        addCourseButton = self.driver.find_element_by_id("add-course-btn")
+        addCourseButton.click()
+
+        # Scroll down so the autocomplete menu is in view
+        # This works around some weird failures
+        self.driver.execute_script("javascript:window.scrollBy(0,200)")
+
+        # Type in part of a school name
+        schoolInput = self.driver.find_element_by_id("str_school")
+        schoolInput.send_keys("harvard u")
+
+        # Wait for autocomplete menu to appear
+        self.wait.until(EC.element_to_be_clickable((By.XPATH, "//ul[contains(@class,'ui-autocomplete')][1]/li")))
+
+        # Choose the first suggestion
+        schoolInput.send_keys(Keys.DOWN)
+        activeItem = self.driver.find_element_by_id("ui-active-menuitem")
+        activeItem.click()
+
+        self.assertEqual(schoolInput.get_attribute("value"), "Harvard University")
+
+        schoolId = self.driver.find_element_by_id("id_school")
+        self.assertEqual(schoolId.get_attribute("value"), "1817")
+
+    def testCreateCourse(self):
+        self.driver.get("http://localhost:8000/")
+
+        # Click "Add Course"
+        addCourseButton = self.driver.find_element_by_id("add-course-btn")
+        addCourseButton.click()
+        self.driver.execute_script("javascript:window.scrollBy(0,200)")
+
+        # We shouldn't be able to save it yet
+        saveButton = self.driver.find_element_by_id("save-btn")
+        self.assertIn("disabled", saveButton.get_attribute("class"))
+
+        # Choose a school
+        self.selectAutocomplete("str_school", "northeastern u", 1)
+
+        # Check that save button is now enabled
+        self.assertNotIn("disabled", saveButton.get_attribute("class"))
+
+        # Course name
+        newCourseName = "SELENIUM TEST COURSE " + uuid.uuid4().hex
+        courseNameInput = self.driver.find_element_by_id("id_name")
+        courseNameInput.send_keys(newCourseName)
+
+        # Instructor name
+        newInstructorName = "SELENIUM TEST INSTRUCTOR " + uuid.uuid4().hex
+        instructorNameInput = self.driver.find_element_by_id("id_instructor_name")
+        instructorNameInput.send_keys(newInstructorName)
+
+        # Click "Save"
+        saveButton = self.driver.find_element_by_id("save-btn")
+        saveButton.click()
+
+        # See if we are taken to the new course page
+        self.wait.until(EC.title_contains(newCourseName))
+
+
+    def testCreateExistingCourse(self):
+        self.driver.get("http://localhost:8000/")
+
+        # Click "Add Course"
+        addCourseButton = self.driver.find_element_by_id("add-course-btn")
+        addCourseButton.click()
+        self.driver.execute_script("javascript:window.scrollBy(0,200)")
+
+        # We shouldn't be able to save it yet
+        saveButton = self.driver.find_element_by_id("save-btn")
+        self.assertIn("disabled", saveButton.get_attribute("class"))
+
+        # Choose a school
+        self.selectAutocomplete("str_school", "northeastern u", 1)
+
+        # Check that save button is now enabled
+        self.assertNotIn("disabled", saveButton.get_attribute("class"))
+
+        # Course name
+        newCourseName = "SELENIUM TEST COURSE " + uuid.uuid4().hex
+        courseNameInput = self.driver.find_element_by_id("id_name")
+        courseNameInput.send_keys(newCourseName)
+
+        # Instructor name
+        newInstructorName = "SELENIUM TEST INSTRUCTOR " + uuid.uuid4().hex
+        instructorNameInput = self.driver.find_element_by_id("id_instructor_name")
+        instructorNameInput.send_keys(newInstructorName)
 
-  def setUp(self):
-    self.driver = webdriver.Firefox()
+        # Click "Save"
+        saveButton = self.driver.find_element_by_id("save-btn")
+        saveButton.click()
 
-  def tearDown(self):
-    self.driver.close()
+        # See if we are taken to the new course page
+        self.wait.until(EC.title_contains(newCourseName))
 
-  def testSchoolName(self):
-    self.driver.get("http://localhost:8000")
+        # Now go back to the home page
+        self.driver.get("http://localhost:8000/")
 
-    addCourseButton = self.driver.find_element_by_id("add-course-btn")
-    addCourseButton.click()
+        # Click "Add Course"
+        addCourseButton = self.driver.find_element_by_id("add-course-btn")
+        addCourseButton.click()
+        self.driver.execute_script("javascript:window.scrollBy(0,200)")
 
-    schoolInput = self.driver.find_element_by_id("str_school")
+        # Choose the SAME school
+        self.selectAutocomplete("str_school", "northeastern u", 1)
 
-    schoolInput.send_keys("harvard u")
+        # The SAME course name
+        self.selectAutocomplete("id_name", newCourseName, 2)
 
-    wait = WebDriverWait(self.driver, 10)
-    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "ui-menu-item")))
+        # The SAME instructor name
+        self.selectAutocomplete("id_instructor_name", newInstructorName, 3)
 
-    schoolInput.send_keys(Keys.DOWN)
+        # Make sure Save button is disabled and hidden
+        saveButton = self.driver.find_element_by_id("save-btn")
+        self.assertIn("disabled", saveButton.get_attribute("class"))
+        self.wait.until_not(EC.visibility_of(saveButton))
 
-    autocompleteMenuItem = self.driver.find_element_by_id("ui-active-menuitem")
-    autocompleteMenuItem.click()
+        # Make sure Existing Course link is shown
+        existingCourse = self.driver.find_element_by_id("existing-course-btn")
+        self.wait.until(EC.visibility_of(existingCourse))
+        existingCourse.click()
 
-    self.assertEqual(schoolInput.get_attribute("value"), "Harvard University")
+        # See if we are taken to the new course page
+        self.wait.until(EC.title_contains(newCourseName))
 
-    schoolId = self.driver.find_element_by_id("id_school")
+if __name__ == "__main__":
+    unittest.main()
 
-    self.assertEqual(schoolId.get_attribute("value"), "1817")