Просмотр исходного кода

Fixed #30975 -- Replaced custom get_select_option with Selenium's select_by_value.

Johannes Hoppe 5 лет назад
Родитель
Сommit
249a6190ae
3 измененных файлов с 44 добавлено и 34 удалено
  1. 14 8
      django/contrib/admin/tests.py
  2. 5 5
      tests/admin_views/tests.py
  3. 25 21
      tests/admin_widgets/tests.py

+ 14 - 8
django/contrib/admin/tests.py

@@ -141,17 +141,23 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
         return self.selenium.execute_script(
             'return django.jQuery("%s").css("%s")' % (selector, attribute))
 
-    def get_select_option(self, selector, value):
+    def select_option(self, selector, value):
         """
-        Return the <OPTION> with the value `value` inside the <SELECT> widget
+        Select the <OPTION> with the value `value` inside the <SELECT> widget
         identified by the CSS selector `selector`.
         """
-        from selenium.common.exceptions import NoSuchElementException
-        options = self.selenium.find_elements_by_css_selector('%s > option' % selector)
-        for option in options:
-            if option.get_attribute('value') == value:
-                return option
-        raise NoSuchElementException('Option "%s" not found in "%s"' % (value, selector))
+        from selenium.webdriver.support.ui import Select
+        select = Select(self.selenium.find_element_by_css_selector(selector))
+        select.select_by_value(value)
+
+    def deselect_option(self, selector, value):
+        """
+        Deselect the <OPTION> with the value `value` inside the <SELECT> widget
+        identified by the CSS selector `selector`.
+        """
+        from selenium.webdriver.support.ui import Select
+        select = Select(self.selenium.find_element_by_css_selector(selector))
+        select.deselect_by_value(value)
 
     def _assertOptionsValues(self, options_selector, values):
         if values:

+ 5 - 5
tests/admin_views/tests.py

@@ -4503,7 +4503,7 @@ class SeleniumTests(AdminSeleniumTestCase):
 
         # Main form ----------------------------------------------------------
         self.selenium.find_element_by_id('id_pubdate').send_keys('2012-02-18')
-        self.get_select_option('#id_status', 'option two').click()
+        self.select_option('#id_status', 'option two')
         self.selenium.find_element_by_id('id_name').send_keys(' this is the mAin nÀMë and it\'s awεšomeııı')
         slug1 = self.selenium.find_element_by_id('id_slug1').get_attribute('value')
         slug2 = self.selenium.find_element_by_id('id_slug2').get_attribute('value')
@@ -4515,7 +4515,7 @@ class SeleniumTests(AdminSeleniumTestCase):
         # Stacked inlines ----------------------------------------------------
         # Initial inline
         self.selenium.find_element_by_id('id_relatedprepopulated_set-0-pubdate').send_keys('2011-12-17')
-        self.get_select_option('#id_relatedprepopulated_set-0-status', 'option one').click()
+        self.select_option('#id_relatedprepopulated_set-0-status', 'option one')
         self.selenium.find_element_by_id('id_relatedprepopulated_set-0-name').send_keys(
             ' here is a sŤāÇkeð   inline !  '
         )
@@ -4536,7 +4536,7 @@ class SeleniumTests(AdminSeleniumTestCase):
             num_initial_select2_inputs + 2
         )
         self.selenium.find_element_by_id('id_relatedprepopulated_set-1-pubdate').send_keys('1999-01-25')
-        self.get_select_option('#id_relatedprepopulated_set-1-status', 'option two').click()
+        self.select_option('#id_relatedprepopulated_set-1-status', 'option two')
         self.selenium.find_element_by_id('id_relatedprepopulated_set-1-name').send_keys(
             ' now you haVe anöther   sŤāÇkeð  inline with a very ... '
             'loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog text... '
@@ -4551,7 +4551,7 @@ class SeleniumTests(AdminSeleniumTestCase):
         # Tabular inlines ----------------------------------------------------
         # Initial inline
         self.selenium.find_element_by_id('id_relatedprepopulated_set-2-0-pubdate').send_keys('1234-12-07')
-        self.get_select_option('#id_relatedprepopulated_set-2-0-status', 'option two').click()
+        self.select_option('#id_relatedprepopulated_set-2-0-status', 'option two')
         self.selenium.find_element_by_id('id_relatedprepopulated_set-2-0-name').send_keys(
             'And now, with a tÃbűlaŘ inline !!!'
         )
@@ -4567,7 +4567,7 @@ class SeleniumTests(AdminSeleniumTestCase):
             num_initial_select2_inputs + 4
         )
         self.selenium.find_element_by_id('id_relatedprepopulated_set-2-1-pubdate').send_keys('1981-08-22')
-        self.get_select_option('#id_relatedprepopulated_set-2-1-status', 'option one').click()
+        self.select_option('#id_relatedprepopulated_set-2-1-status', 'option one')
         self.selenium.find_element_by_id('id_relatedprepopulated_set-2-1-name').send_keys(
             r'a tÃbűlaŘ inline with ignored ;"&*^\%$#@-/`~ characters'
         )

+ 25 - 21
tests/admin_widgets/tests.py

@@ -1038,15 +1038,17 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
         self.assertActiveButtons(mode, field_name, False, False, True, False)
 
         # Choose some options ------------------------------------------------
-        from_lisa_select_option = self.get_select_option(from_box, str(self.lisa.id))
+        from_lisa_select_option = self.selenium.find_element_by_css_selector(
+            '{} > option[value="{}"]'.format(from_box, self.lisa.id)
+        )
 
         # Check the title attribute is there for tool tips: ticket #20821
         self.assertEqual(from_lisa_select_option.get_attribute('title'), from_lisa_select_option.get_attribute('text'))
 
-        from_lisa_select_option.click()
-        self.get_select_option(from_box, str(self.jason.id)).click()
-        self.get_select_option(from_box, str(self.bob.id)).click()
-        self.get_select_option(from_box, str(self.john.id)).click()
+        self.select_option(from_box, str(self.lisa.id))
+        self.select_option(from_box, str(self.jason.id))
+        self.select_option(from_box, str(self.bob.id))
+        self.select_option(from_box, str(self.john.id))
         self.assertActiveButtons(mode, field_name, True, False, True, False)
         self.selenium.find_element_by_id(choose_link).click()
         self.assertActiveButtons(mode, field_name, False, False, True, True)
@@ -1061,12 +1063,14 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
         ])
 
         # Check the tooltip is still there after moving: ticket #20821
-        to_lisa_select_option = self.get_select_option(to_box, str(self.lisa.id))
+        to_lisa_select_option = self.selenium.find_element_by_css_selector(
+            '{} > option[value="{}"]'.format(to_box, self.lisa.id)
+        )
         self.assertEqual(to_lisa_select_option.get_attribute('title'), to_lisa_select_option.get_attribute('text'))
 
         # Remove some options -------------------------------------------------
-        self.get_select_option(to_box, str(self.lisa.id)).click()
-        self.get_select_option(to_box, str(self.bob.id)).click()
+        self.select_option(to_box, str(self.lisa.id))
+        self.select_option(to_box, str(self.bob.id))
         self.assertActiveButtons(mode, field_name, False, True, True, True)
         self.selenium.find_element_by_id(remove_link).click()
         self.assertActiveButtons(mode, field_name, False, False, True, True)
@@ -1079,8 +1083,8 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
         self.assertSelectOptions(to_box, [str(self.jason.id), str(self.john.id)])
 
         # Choose some more options --------------------------------------------
-        self.get_select_option(from_box, str(self.arthur.id)).click()
-        self.get_select_option(from_box, str(self.cliff.id)).click()
+        self.select_option(from_box, str(self.arthur.id))
+        self.select_option(from_box, str(self.cliff.id))
         self.selenium.find_element_by_id(choose_link).click()
 
         self.assertSelectOptions(from_box, [
@@ -1093,8 +1097,8 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
         ])
 
         # Choose some more options --------------------------------------------
-        self.get_select_option(from_box, str(self.peter.id)).click()
-        self.get_select_option(from_box, str(self.lisa.id)).click()
+        self.select_option(from_box, str(self.peter.id))
+        self.select_option(from_box, str(self.lisa.id))
 
         # Confirm they're selected after clicking inactive buttons: ticket #26575
         self.assertSelectedOptions(from_box, [str(self.peter.id), str(self.lisa.id)])
@@ -1102,12 +1106,12 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
         self.assertSelectedOptions(from_box, [str(self.peter.id), str(self.lisa.id)])
 
         # Unselect the options ------------------------------------------------
-        self.get_select_option(from_box, str(self.peter.id)).click()
-        self.get_select_option(from_box, str(self.lisa.id)).click()
+        self.deselect_option(from_box, str(self.peter.id))
+        self.deselect_option(from_box, str(self.lisa.id))
 
         # Choose some more options --------------------------------------------
-        self.get_select_option(to_box, str(self.jason.id)).click()
-        self.get_select_option(to_box, str(self.john.id)).click()
+        self.select_option(to_box, str(self.jason.id))
+        self.select_option(to_box, str(self.john.id))
 
         # Confirm they're selected after clicking inactive buttons: ticket #26575
         self.assertSelectedOptions(to_box, [str(self.jason.id), str(self.john.id)])
@@ -1115,8 +1119,8 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
         self.assertSelectedOptions(to_box, [str(self.jason.id), str(self.john.id)])
 
         # Unselect the options ------------------------------------------------
-        self.get_select_option(to_box, str(self.jason.id)).click()
-        self.get_select_option(to_box, str(self.john.id)).click()
+        self.deselect_option(to_box, str(self.jason.id))
+        self.deselect_option(to_box, str(self.john.id))
 
         # Pressing buttons shouldn't change the URL.
         self.assertEqual(self.selenium.current_url, original_url)
@@ -1186,14 +1190,14 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
             # Choosing a filtered option sends it properly to the 'to' box.
             input.send_keys('a')
             self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.jason.id)])
-            self.get_select_option(from_box, str(self.jason.id)).click()
+            self.select_option(from_box, str(self.jason.id))
             self.selenium.find_element_by_id(choose_link).click()
             self.assertSelectOptions(from_box, [str(self.arthur.id)])
             self.assertSelectOptions(to_box, [
                 str(self.lisa.id), str(self.peter.id), str(self.jason.id),
             ])
 
-            self.get_select_option(to_box, str(self.lisa.id)).click()
+            self.select_option(to_box, str(self.lisa.id))
             self.selenium.find_element_by_id(remove_link).click()
             self.assertSelectOptions(from_box, [str(self.arthur.id), str(self.lisa.id)])
             self.assertSelectOptions(to_box, [str(self.peter.id), str(self.jason.id)])
@@ -1209,7 +1213,7 @@ class HorizontalVerticalFilterSeleniumTests(AdminWidgetSeleniumTestCase):
             # -----------------------------------------------------------------
             # Pressing enter on a filtered option sends it properly to
             # the 'to' box.
-            self.get_select_option(to_box, str(self.jason.id)).click()
+            self.select_option(to_box, str(self.jason.id))
             self.selenium.find_element_by_id(remove_link).click()
             input.send_keys('ja')
             self.assertSelectOptions(from_box, [str(self.jason.id)])