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
5from marionette_driver.by import By
6from marionette_driver.errors import NoSuchElementException
7from marionette_driver.marionette import HTMLElement
8
9from marionette_harness import MarionetteTestCase
10
11
12class TestElementsChrome(MarionetteTestCase):
13    def setUp(self):
14        MarionetteTestCase.setUp(self)
15        self.marionette.set_context("chrome")
16        self.win = self.marionette.current_window_handle
17        self.marionette.execute_script("window.open('chrome://marionette/content/test.xul', 'foo', 'chrome,centerscreen');")
18        self.marionette.switch_to_window('foo')
19        self.assertNotEqual(self.win, self.marionette.current_window_handle)
20
21    def tearDown(self):
22        self.assertNotEqual(self.win, self.marionette.current_window_handle)
23        self.marionette.execute_script("window.close();")
24        self.marionette.switch_to_window(self.win)
25        MarionetteTestCase.tearDown(self)
26
27    def test_id(self):
28        el = self.marionette.execute_script("return window.document.getElementById('textInput');")
29        found_el = self.marionette.find_element(By.ID, "textInput")
30        self.assertEqual(HTMLElement, type(found_el))
31        self.assertEqual(el, found_el)
32
33    def test_that_we_can_find_elements_from_css_selectors(self):
34        el = self.marionette.execute_script("return window.document.getElementById('textInput');")
35        found_el = self.marionette.find_element(By.CSS_SELECTOR, "#textInput")
36        self.assertEqual(HTMLElement, type(found_el))
37        self.assertEqual(el, found_el)
38
39    def test_child_element(self):
40        el = self.marionette.find_element(By.ID, "textInput")
41        parent = self.marionette.find_element(By.ID, "things")
42        found_el = parent.find_element(By.TAG_NAME, "textbox")
43        self.assertEqual(HTMLElement, type(found_el))
44        self.assertEqual(el, found_el)
45
46    def test_child_elements(self):
47        el = self.marionette.find_element(By.ID, "textInput3")
48        parent = self.marionette.find_element(By.ID, "things")
49        found_els = parent.find_elements(By.TAG_NAME, "textbox")
50        self.assertTrue(el.id in [found_el.id for found_el in found_els])
51
52    def test_tag_name(self):
53        el = self.marionette.execute_script("return window.document.getElementsByTagName('vbox')[0];")
54        found_el = self.marionette.find_element(By.TAG_NAME, "vbox")
55        self.assertEquals('vbox', found_el.tag_name)
56        self.assertEqual(HTMLElement, type(found_el))
57        self.assertEqual(el, found_el)
58
59    def test_class_name(self):
60        el = self.marionette.execute_script("return window.document.getElementsByClassName('asdf')[0];")
61        found_el = self.marionette.find_element(By.CLASS_NAME, "asdf")
62        self.assertEqual(HTMLElement, type(found_el))
63        self.assertEqual(el, found_el)
64
65    def test_xpath(self):
66        el = self.marionette.execute_script("return window.document.getElementById('testBox');")
67        found_el = self.marionette.find_element(By.XPATH, "id('testBox')")
68        self.assertEqual(HTMLElement, type(found_el))
69        self.assertEqual(el, found_el)
70
71    def test_not_found(self):
72        self.marionette.timeout.implicit = 1
73        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
74        self.marionette.timeout.implicit = 0
75        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
76
77    def test_timeout(self):
78        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "myid")
79        self.marionette.timeout.implicit = 4
80        self.marionette.execute_script("window.setTimeout(function() {var b = window.document.createElement('button'); b.id = 'myid'; document.getElementById('things').appendChild(b);}, 1000)")
81        self.assertEqual(HTMLElement, type(self.marionette.find_element(By.ID, "myid")))
82        self.marionette.execute_script("window.document.getElementById('things').removeChild(window.document.getElementById('myid'));")
83