class Tests(unittest.TestCase): @classmethod def setUpClass(self): self.d = webdriver.Firefox() self.w = WebApp(self.d, GUINEA_PIG) self.w.go_to() @classmethod def tearDownClass(self): self.d.quit() def test_wait_visible(self): cA = SimpleContainerA(self.w) Element(self.w, ID, "timing-displayed-button").click() self.assertFalse(cA.wait_until_visible(2)) self.assertTrue(cA.wait_until_visible(2)) self.assertFalse(cA.wait_until_not_visible(2)) self.assertTrue(cA.wait_until_not_visible(2)) # also test wait_visible when element isn't in DOM cB = SimpleContainerB(self.w) Element(self.w, ID, "timing-exists-button").click() self.assertFalse(cB.wait_until_visible(2)) self.assertTrue(cB.wait_until_visible(2)) self.assertFalse(cB.wait_until_not_visible(2)) self.assertTrue(cB.wait_until_not_visible(2))
class Tests(unittest.TestCase): @classmethod def setUpClass(self): self.d = webdriver.Firefox() self.w = WebApp(self.d, GUINEA_PIG) self.w.go_to() @classmethod def tearDownClass(self): self.d.quit() def test_base_lookup(self): labels = ElementList(self.w, TAG, "label") self.assertEquals(4, labels.count())
class Tests(unittest.TestCase): @classmethod def setUpClass(self): self.d = webdriver.Firefox() self.w = WebApp(self.d, "file:///home/dionyses/projects/korlat/tests/guineapig.html") self.w.go_to() @classmethod def tearDownClass(self): self.d.quit() def test_checkbox(self): c = Checkbox(self.w, ID, "checkbox-input-1") c.check_appearance() c.check_behaviour()
class Tests(unittest.TestCase): @classmethod def setUpClass(self): self.d = webdriver.Firefox() self.w = WebApp(self.d, GUINEA_PIG) self.w.go_to() def setUp(self): self.root_div_id = Element(self.w, ID, "root") self.root_div_xpath = Element(self.w, XPATH, "//div[contains(@class, 'root-class')]") self.text_input_id = Element(self.w, ID, "text-input") self.button_input_id = Element(self.w, ID, "button-input") self.change_label_id = Element(self.w, ID, "change-label") self.label_xpath_a = Element(self.w, XPATH, "//label[@class='label-class']") self.label_xpath_b = Element(self.w, XPATH, "//label[contains(@class, 'label-class')]") self.label_xpath_c = Element(self.w, XPATH, "//*[@class='label-class' and not(starts-with(@id, 'bob'))]") self.label_relative_xpath = Element(self.w, XPATH, "/label[@class='label-class']") self.checkbox_input_1 = Element(self.w, ID, "checkbox-input-1") self.checkbox_input_2 = Element(self.w, ID, "checkbox-input-2") self.hidden_label_id = Element(self.w, ID, "hidden-label") self.non_existent_id = Element(self.w, ID, "non-existent") self.bad_xpath = Element(self.w, XPATH, "//input[type='button']") @classmethod def tearDownClass(self): self.d.quit() def test_base_lookup(self): self.assertTrue(self.root_div_id.exists()) self.assertTrue(self.root_div_xpath.exists()) self.assertTrue(self.text_input_id.exists()) self.assertTrue(self.label_xpath_a.exists()) self.assertTrue(self.label_xpath_b.exists()) self.assertTrue(self.label_xpath_c.exists()) self.assertFalse(self.label_relative_xpath.exists()) self.assertFalse(self.non_existent_id.exists()) self.assertFalse(self.bad_xpath.exists()) def test_parent_lookup(self): self.text_input_id.set_parent(self.root_div_id) self.assertTrue(self.text_input_id.exists()) self.text_input_id.set_parent(self.root_div_xpath) self.assertTrue(self.text_input_id.exists()) self.label_xpath_a.set_parent(self.root_div_id) self.assertTrue(self.label_xpath_a.exists()) self.label_xpath_a.set_parent(self.root_div_xpath) self.assertTrue(self.label_xpath_a.exists()) self.label_xpath_b.set_parent(self.root_div_id) self.assertTrue(self.label_xpath_b.exists()) self.label_xpath_b.set_parent(self.root_div_xpath) self.assertTrue(self.label_xpath_b.exists()) self.label_xpath_c.set_parent(self.root_div_id) self.assertTrue(self.label_xpath_c.exists()) self.label_xpath_c.set_parent(self.root_div_xpath) self.assertTrue(self.label_xpath_c.exists()) self.label_relative_xpath.set_parent(self.root_div_id) self.assertTrue(self.label_relative_xpath.exists()) self.label_relative_xpath.set_parent(self.root_div_xpath) self.assertTrue(self.label_relative_xpath.exists()) self.non_existent_id.set_parent(self.root_div_id) self.assertFalse(self.non_existent_id.exists()) self.non_existent_id.set_parent(self.root_div_xpath) self.assertFalse(self.non_existent_id.exists()) self.bad_xpath.set_parent(self.root_div_id) self.assertFalse(self.bad_xpath.exists()) self.bad_xpath.set_parent(self.root_div_xpath) self.assertFalse(self.bad_xpath.exists()) def test_gets(self): # get_value self.assertEquals("button", self.button_input_id.get_value()) # get_text self.assertEquals("Test Label", self.label_xpath_a.get_text()) # get_attribute self.assertEquals("checkbox", self.checkbox_input_1.get_attribute("type")) # get_css_value self.assertEquals("1", self.text_input_id.get_css_value("z-index")) # get_tag_name self.assertEquals("label", self.label_xpath_a.get_tag_name()) # get_identifier self.assertEquals("text-input", self.text_input_id.get_identifier()) # get_location self.assertEquals({"x": 1, "y": 50}, self.text_input_id.get_location()) # get_size self.assertEquals({"width": 150, "height": 25}, self.text_input_id.get_size()) def test_states(self): # is_enabled self.assertTrue(self.checkbox_input_1.is_enabled()) self.assertFalse(self.checkbox_input_2.is_enabled()) # is_selected self.assertFalse(self.checkbox_input_1.is_selected()) self.assertTrue(self.checkbox_input_2.is_selected()) # is_displayed self.assertTrue(self.label_xpath_a.is_displayed()) self.assertFalse(self.hidden_label_id.is_displayed()) with self.assertRaises(NoSuchElementException): self.label_relative_xpath.is_displayed() with self.assertRaises(NoSuchElementException): self.non_existent_id.is_displayed() def test_ignoring_states(self): self.assertTrue(self.label_xpath_a.is_displayed(ignore=True)) self.assertFalse(self.hidden_label_id.is_displayed(ignore=True)) # now that we ignore, NoSuchElementException should not be thrown self.assertFalse(self.label_relative_xpath.is_displayed(ignore=True)) self.assertFalse(self.non_existent_id.is_displayed(ignore=True)) def test_controls(self): # click self.assertEquals("0", self.change_label_id.get_text()) self.button_input_id.click() self.assertEquals("1", self.change_label_id.get_text()) self.button_input_id.click() self.assertEquals("2", self.change_label_id.get_text()) # send_keys self.text_input_id.send_keys("test text") self.assertEquals("test text", self.text_input_id.get_value()) self.text_input_id.send_keys(u"你好吗?") self.assertEquals(u"test text你好吗?", self.text_input_id.get_value()) self.text_input_id.send_keys(Keys.BACK_SPACE) self.text_input_id.send_keys(Keys.BACK_SPACE) self.text_input_id.send_keys(Keys.ARROW_LEFT) self.text_input_id.send_keys(Keys.BACK_SPACE) self.assertEquals(u"test text好", self.text_input_id.get_value()) # clear self.text_input_id.clear() self.assertEquals("", self.text_input_id.get_value()) def test_wait_exists(self): Element(self.w, ID, "timing-exists-button").click() new_label = Element(self.w, ID, "new-label") # test that when the element still hasn't appeared until_exists returns False self.assertFalse(new_label.wait_until_exists(2)) # test that when the element finally appears until_exists returns True self.assertTrue(new_label.wait_until_exists(2)) # test that when the element still hasn't dissappeared until_not_exists returns False self.assertFalse(new_label.wait_until_not_exists(2)) # test that when the element finally dissappears until_not_exists returns True self.assertTrue(new_label.wait_until_not_exists(2)) def test_wait_displayed(self): Element(self.w, ID, "timing-displayed-button").click() hidden_label = Element(self.w, ID, "hidden-label") # test that when the element still hasn't appeared until_displayed returns False self.assertFalse(hidden_label.wait_until_displayed(2)) # test that when the element finally appears until_displayed returns True self.assertTrue(hidden_label.wait_until_displayed(2)) # test that when the element still hasn't dissappeared until_not_displayed returns False self.assertFalse(hidden_label.wait_until_not_displayed(2)) # test that when the element finally dissappears until_not_displayed returns True self.assertTrue(hidden_label.wait_until_not_displayed(2)) # also test wait_displayed when element isn't in DOM Element(self.w, ID, "timing-exists-button").click() new_label = Element(self.w, ID, "new-label") # test that when the element still hasn't appeared until_exists returns False with self.assertRaises(NoSuchElementException): new_label.wait_until_displayed(2) # test that when the element finally appears until_exists returns True self.assertTrue(new_label.wait_until_displayed(2)) # test that when the element still hasn't dissappeared until_not_exists returns False self.assertFalse(new_label.wait_until_not_displayed(2)) # test that when the element finally dissappears until_not_exists returns True with self.assertRaises(NoSuchElementException): new_label.wait_until_not_displayed(2) def test_links(self): mock_container_a = Mock() mock_container_a.__class__ = Container e = Element(self.w, ID, "nadda", "nadda") \ .set_link(mock_container_a) self.assertTrue(mock_container_a is e.link) mock_container_b = Mock() mock_container_b.__class__ = Container e.set_link(mock_container_b) self.assertTrue(mock_container_b is e.link) e.set_link(mock_container_a, "a") self.assertTrue(mock_container_a is e.links["a"]) self.assertTrue(mock_container_b is e.link) with self.assertRaises(KeyError): e.links["b"]
class Tests(unittest.TestCase): @classmethod def setUpClass(self): self.d = webdriver.Firefox() self.w = WebApp(self.d, GUINEA_PIG) self.gp2s = Element(self.w, ID, "gp2s", "gp2s_label") \ .set_link(GP2(self.w)) self.gp2n = Element(self.w, ID, "gp2n", "gp2n_label") \ .set_link(GP2(self.w)) self.gp3s = Element(self.w, ID, "gp3s", "gp3s_label") \ .set_link(GP3(self.w)) self.gp3n = Element(self.w, ID, "gp3n") \ .set_link(GP3(self.w), "key") @classmethod def tearDownClass(self): self.d.quit() def setUp(self): self.w.go_to() # every go_to should wipe any extra windows self.assertTrue(1, len(self.w.get_windows())) def test_go_to_link_key_error(self): with self.assertRaises(KeyError): self.gp2s.go_to_link("no_keys") with self.assertRaises(KeyError): self.gp3n.go_to_link("incorrect_key") with self.assertRaises(KeyError): self.gp2s.go_to_link("Key") def test_go_to_link(self): # open gp2 in the same window for e in self.gp2s.go_to_link().get_elements(required=True): # make sure you land at gp2 self.assertTrue(e.is_displayed()) # there should be one windows self.assertEquals(1, len(self.w.get_windows())) # we shouldn't see the link which brought us here self.assertFalse(self.gp2s.exists()) # go back self.w.driver.back() # open gp3 in a new window for e in self.gp3n.go_to_link("key").get_elements(required=True): # make sure you land at gp3 self.assertTrue(e.is_displayed()) # there should be two windows self.assertEquals(2, len(self.w.get_windows())) # we shouldn't see the link which brought us here self.assertFalse(self.gp2s.exists()) def test_window_labels(self): ### opening no new window should leave the get_windows unaltered handles = self.w.get_windows() self.gp2s.go_to_link() self.assertEquals(handles, self.w.get_windows()) ### opening a new window from a labelled element should use the label self.w.driver.back() self.gp2n.go_to_link() self.assertNotEqual(handles, self.w.get_windows()) # get_windows should still contain the previous key self.assertTrue(handles[0] in self.w.get_windows()) # get_windows should also contain a key equal of the element's label which brought it there self.assertTrue(self.gp2n.label in self.w.get_windows()) ### opening a new window from an unlabelled element should use the next_window_key handles = self.w.get_windows() self.w.use_window(MAIN_WINDOW) next_key = self.w.next_window_key() self.gp3n.go_to_link("key") self.assertNotEqual(handles, self.w.get_windows()) # get_windows should still contain the previous keys self.assertTrue(handles[0] in self.w.get_windows()) self.assertTrue(handles[1] in self.w.get_windows()) # get_windows should also contain a key equal which is next_window_key self.assertTrue(next_key in self.w.get_windows()) ### opening a pre-existing label/key should not alter the get_windows() handles = self.w.get_windows() self.w.use_window(MAIN_WINDOW) self.gp2s.go_to_link() self.assertEquals(handles, self.w.get_windows()) def test_switching_windows(self): self.gp2n.go_to_link() self.w.use_window(MAIN_WINDOW) n_k_1 = self.w.next_window_key() self.gp3n.go_to_link("key") # should be next key 1 self.assertTrue(n_k_1 in self.w.get_windows()) self.w.use_window(MAIN_WINDOW) n_k_2 = self.w.next_window_key() self.gp3n.go_to_link("key") # should be next key 2 self.assertTrue(n_k_1 in self.w.get_windows()) self.assertTrue(n_k_2 in self.w.get_windows()) self.assertEquals(4, len(self.w.get_windows())) for k in self.w.get_windows(): self.w.use_window(k) if k == MAIN_WINDOW: self.assertTrue(self.gp2s.exists()) self.assertFalse(self.gp2n.link.get_elements(required=True)[0].exists()) self.assertFalse(self.gp3n.links["key"].get_elements(required=True)[0].exists()) elif k == self.gp2n.label: self.assertFalse(self.gp2s.exists()) self.assertTrue(self.gp2n.link.get_elements(required=True)[0].exists()) self.assertFalse(self.gp3n.links["key"].get_elements(required=True)[0].exists()) else: self.assertFalse(self.gp2s.exists()) self.assertFalse(self.gp2n.link.get_elements(required=True)[0].exists()) self.assertTrue(self.gp3n.links["key"].get_elements(required=True)[0].exists()) def test_dead_windows(self): self.gp2n.go_to_link() self.w.use_window(MAIN_WINDOW) self.gp2n.go_to_link() self.assertEquals(2, len(self.w.get_windows())) self.assertEquals(3, len(self.w.driver.window_handles)) self.w.go_to() self.assertEquals(1, len(self.w.get_windows())) self.assertEquals(1, len(self.w.driver.window_handles)) self.assertTrue(MAIN_WINDOW in self.w.get_windows())