tor-browser

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

test_chrome_findelement.py (6519B)


      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 import os
      6 import sys
      7 
      8 from marionette_driver.by import By
      9 from marionette_driver.errors import NoSuchElementException
     10 from marionette_driver.marionette import WebElement, WEB_ELEMENT_KEY
     11 from marionette_harness import MarionetteTestCase, parameterized, WindowManagerMixin
     12 
     13 # add this directory to the path
     14 sys.path.append(os.path.dirname(__file__))
     15 
     16 from chrome_handler_mixin import ChromeHandlerMixin
     17 
     18 
     19 PAGE_XHTML = "test.xhtml"
     20 PAGE_XUL = "test_xul.xhtml"
     21 
     22 
     23 class TestElementsChrome(ChromeHandlerMixin, WindowManagerMixin, MarionetteTestCase):
     24    def setUp(self):
     25        super(TestElementsChrome, self).setUp()
     26 
     27        self.marionette.set_context("chrome")
     28 
     29    def tearDown(self):
     30        self.close_all_windows()
     31 
     32        super(TestElementsChrome, self).tearDown()
     33 
     34    @parameterized("XUL", PAGE_XUL)
     35    @parameterized("XHTML", PAGE_XHTML)
     36    def test_id(self, chrome_url):
     37        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
     38        self.marionette.switch_to_window(win)
     39 
     40        el = self.marionette.execute_script(
     41            "return window.document.getElementById('textInput');"
     42        )
     43        found_el = self.marionette.find_element(By.ID, "textInput")
     44        self.assertEqual(WebElement, type(found_el))
     45        self.assertEqual(WEB_ELEMENT_KEY, found_el.kind)
     46        self.assertEqual(el, found_el)
     47 
     48    @parameterized("XUL", PAGE_XUL)
     49    @parameterized("XHTML", PAGE_XHTML)
     50    def test_that_we_can_find_elements_from_css_selectors(self, chrome_url):
     51        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
     52        self.marionette.switch_to_window(win)
     53 
     54        el = self.marionette.execute_script(
     55            "return window.document.getElementById('textInput');"
     56        )
     57        found_el = self.marionette.find_element(By.CSS_SELECTOR, "#textInput")
     58        self.assertEqual(WebElement, type(found_el))
     59        self.assertEqual(WEB_ELEMENT_KEY, found_el.kind)
     60        self.assertEqual(el, found_el)
     61 
     62    @parameterized("XUL", PAGE_XUL)
     63    @parameterized("XHTML", PAGE_XHTML)
     64    def test_child_element(self, chrome_url):
     65        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
     66        self.marionette.switch_to_window(win)
     67 
     68        el = self.marionette.find_element(By.ID, "button")
     69        parent = self.marionette.find_element(By.ID, "types")
     70        found_el = parent.find_element(By.TAG_NAME, "button")
     71        self.assertEqual(WebElement, type(found_el))
     72        self.assertEqual(WEB_ELEMENT_KEY, found_el.kind)
     73        self.assertEqual(el, found_el)
     74 
     75    @parameterized("XUL", PAGE_XUL)
     76    @parameterized("XHTML", PAGE_XHTML)
     77    def test_child_elements(self, chrome_url):
     78        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
     79        self.marionette.switch_to_window(win)
     80 
     81        el = self.marionette.find_element(By.ID, "button")
     82        parent = self.marionette.find_element(By.ID, "types")
     83        found_els = parent.find_elements(By.TAG_NAME, "button")
     84        self.assertIn(el.id, [found_el.id for found_el in found_els])
     85 
     86    @parameterized("XUL", PAGE_XUL)
     87    @parameterized("XHTML", PAGE_XHTML)
     88    def test_tag_name(self, chrome_url):
     89        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
     90        self.marionette.switch_to_window(win)
     91 
     92        el = self.marionette.execute_script(
     93            "return window.document.getElementsByTagName('button')[0];"
     94        )
     95        found_el = self.marionette.find_element(By.TAG_NAME, "button")
     96        self.assertEqual("button", found_el.tag_name)
     97        self.assertEqual(WebElement, type(found_el))
     98        self.assertEqual(WEB_ELEMENT_KEY, found_el.kind)
     99        self.assertEqual(el, found_el)
    100 
    101    @parameterized("XUL", PAGE_XUL)
    102    @parameterized("XHTML", PAGE_XHTML)
    103    def test_class_name(self, chrome_url):
    104        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
    105        self.marionette.switch_to_window(win)
    106 
    107        el = self.marionette.execute_script(
    108            "return window.document.getElementsByClassName('foo')[0];"
    109        )
    110        found_el = self.marionette.find_element(By.CLASS_NAME, "foo")
    111        self.assertEqual(WebElement, type(found_el))
    112        self.assertEqual(WEB_ELEMENT_KEY, found_el.kind)
    113        self.assertEqual(el, found_el)
    114 
    115    @parameterized("XUL", PAGE_XUL)
    116    @parameterized("XHTML", PAGE_XHTML)
    117    def test_xpath(self, chrome_url):
    118        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
    119        self.marionette.switch_to_window(win)
    120 
    121        el = self.marionette.execute_script(
    122            "return window.document.getElementById('testBox');"
    123        )
    124        found_el = self.marionette.find_element(By.XPATH, "id('testBox')")
    125        self.assertEqual(WebElement, type(found_el))
    126        self.assertEqual(WEB_ELEMENT_KEY, found_el.kind)
    127        self.assertEqual(el, found_el)
    128 
    129    @parameterized("XUL", PAGE_XUL)
    130    @parameterized("XHTML", PAGE_XHTML)
    131    def test_not_found(self, chrome_url):
    132        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
    133        self.marionette.switch_to_window(win)
    134 
    135        self.assertRaises(
    136            NoSuchElementException,
    137            self.marionette.find_element,
    138            By.ID,
    139            "I'm not on the page",
    140        )
    141 
    142    @parameterized("XUL", PAGE_XUL)
    143    @parameterized("XHTML", PAGE_XHTML)
    144    def test_timeout(self, chrome_url):
    145        win = self.open_chrome_window(self.chrome_base_url + chrome_url)
    146        self.marionette.switch_to_window(win)
    147 
    148        self.assertRaises(
    149            NoSuchElementException, self.marionette.find_element, By.ID, "myid"
    150        )
    151        self.marionette.timeout.implicit = 2
    152        self.marionette.execute_script(
    153            """
    154            window.setTimeout(function () {
    155              var b = window.document.createXULElement('button');
    156              b.id = 'myid';
    157              document.getElementById('types').appendChild(b);
    158            }, 500); """
    159        )
    160        found_el = self.marionette.find_element(By.ID, "myid")
    161        self.assertEqual(WebElement, type(found_el))
    162        self.assertEqual(WEB_ELEMENT_KEY, found_el.kind)
    163 
    164        self.marionette.execute_script(
    165            """
    166            var elem = window.document.getElementById('types');
    167            elem.removeChild(window.document.getElementById('myid')); """
    168        )