tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_select.py (6251B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this
      3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 from urllib.parse import quote
      6 
      7 from marionette_driver.by import By
      8 
      9 from marionette_harness import MarionetteTestCase
     10 
     11 
     12 def inline(doc):
     13    return "data:text/html;charset=utf-8,{}".format(quote(doc))
     14 
     15 
     16 class SelectTestCase(MarionetteTestCase):
     17    def assertSelected(self, option_element):
     18        self.assertTrue(option_element.is_selected(), "<option> element not selected")
     19        self.assertTrue(
     20            self.marionette.execute_script(
     21                "return arguments[0].selected",
     22                script_args=[option_element],
     23                sandbox=None,
     24            ),
     25            "<option> selected attribute not updated",
     26        )
     27 
     28    def assertNotSelected(self, option_element):
     29        self.assertFalse(option_element.is_selected(), "<option> is selected")
     30        self.assertFalse(
     31            self.marionette.execute_script(
     32                "return arguments[0].selected",
     33                script_args=[option_element],
     34                sandbox=None,
     35            ),
     36            "<option> selected attribute not updated",
     37        )
     38 
     39 
     40 class TestSelect(SelectTestCase):
     41    def test_single(self):
     42        self.marionette.navigate(
     43            inline(
     44                """
     45            <select>
     46              <option>first
     47              <option>second
     48            </select>"""
     49            )
     50        )
     51        select = self.marionette.find_element(By.TAG_NAME, "select")
     52        options = self.marionette.find_elements(By.TAG_NAME, "option")
     53 
     54        self.assertSelected(options[0])
     55        options[1].click()
     56        self.assertSelected(options[1])
     57 
     58    def test_deselect_others(self):
     59        self.marionette.navigate(
     60            inline(
     61                """
     62          <select>
     63            <option>first
     64            <option>second
     65            <option>third
     66          </select>"""
     67            )
     68        )
     69        select = self.marionette.find_element(By.TAG_NAME, "select")
     70        options = self.marionette.find_elements(By.TAG_NAME, "option")
     71 
     72        options[0].click()
     73        self.assertSelected(options[0])
     74        options[1].click()
     75        self.assertSelected(options[1])
     76        options[2].click()
     77        self.assertSelected(options[2])
     78        options[0].click()
     79        self.assertSelected(options[0])
     80 
     81    def test_select_self(self):
     82        self.marionette.navigate(
     83            inline(
     84                """
     85          <select>
     86            <option>first
     87            <option>second
     88          </select>"""
     89            )
     90        )
     91        select = self.marionette.find_element(By.TAG_NAME, "select")
     92        options = self.marionette.find_elements(By.TAG_NAME, "option")
     93        self.assertSelected(options[0])
     94        self.assertNotSelected(options[1])
     95 
     96        options[1].click()
     97        self.assertSelected(options[1])
     98        options[1].click()
     99        self.assertSelected(options[1])
    100 
    101    def test_out_of_view(self):
    102        self.marionette.navigate(
    103            inline(
    104                """
    105          <select>
    106            <option>1
    107            <option>2
    108            <option>3
    109            <option>4
    110            <option>5
    111            <option>6
    112            <option>7
    113            <option>8
    114            <option>9
    115            <option>10
    116            <option>11
    117            <option>12
    118            <option>13
    119            <option>14
    120            <option>15
    121            <option>16
    122            <option>17
    123            <option>18
    124            <option>19
    125            <option>20
    126          </select>"""
    127            )
    128        )
    129        select = self.marionette.find_element(By.TAG_NAME, "select")
    130        options = self.marionette.find_elements(By.TAG_NAME, "option")
    131 
    132        options[14].click()
    133        self.assertSelected(options[14])
    134 
    135 
    136 class TestSelectMultiple(SelectTestCase):
    137    def test_single(self):
    138        self.marionette.navigate(inline("<select multiple> <option>first </select>"))
    139        option = self.marionette.find_element(By.TAG_NAME, "option")
    140        option.click()
    141        self.assertSelected(option)
    142 
    143    def test_multiple(self):
    144        self.marionette.navigate(
    145            inline(
    146                """
    147          <select multiple>
    148            <option>first
    149            <option>second
    150            <option>third
    151          </select>"""
    152            )
    153        )
    154        select = self.marionette.find_element(By.TAG_NAME, "select")
    155        options = select.find_elements(By.TAG_NAME, "option")
    156 
    157        options[1].click()
    158        self.assertSelected(options[1])
    159 
    160        options[2].click()
    161        self.assertSelected(options[2])
    162        self.assertSelected(options[1])
    163 
    164    def test_deselect_selected(self):
    165        self.marionette.navigate(inline("<select multiple> <option>first </select>"))
    166        option = self.marionette.find_element(By.TAG_NAME, "option")
    167        option.click()
    168        self.assertSelected(option)
    169        option.click()
    170        self.assertNotSelected(option)
    171 
    172    def test_deselect_preselected(self):
    173        self.marionette.navigate(
    174            inline(
    175                """
    176          <select multiple>
    177            <option selected>first
    178          </select>"""
    179            )
    180        )
    181        option = self.marionette.find_element(By.TAG_NAME, "option")
    182        self.assertSelected(option)
    183        option.click()
    184        self.assertNotSelected(option)
    185 
    186    def test_out_of_view(self):
    187        self.marionette.navigate(
    188            inline(
    189                """
    190          <select multiple>
    191            <option>1
    192            <option>2
    193            <option>3
    194            <option>4
    195            <option>5
    196            <option>6
    197            <option>7
    198            <option>8
    199            <option>9
    200            <option>10
    201            <option>11
    202            <option>12
    203            <option>13
    204            <option>14
    205            <option>15
    206            <option>16
    207            <option>17
    208            <option>18
    209            <option>19
    210            <option>20
    211          </select>"""
    212            )
    213        )
    214        select = self.marionette.find_element(By.TAG_NAME, "select")
    215        options = self.marionette.find_elements(By.TAG_NAME, "option")
    216 
    217        options[-1].click()
    218        self.assertSelected(options[-1])