Path: blob/trunk/py/test/selenium/webdriver/support/relative_by_tests.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.16import pytest1718from selenium.common.exceptions import NoSuchElementException19from selenium.webdriver.common.by import By20from selenium.webdriver.support.relative_locator import locate_with, with_tag_name212223def test_should_be_able_to_find_first_one(driver, pages):24pages.load("relative_locators.html")25lowest = driver.find_element(By.ID, "below")2627el = driver.find_element(with_tag_name("p").above(lowest))2829assert el.get_attribute("id") == "mid"303132def test_should_be_able_to_find_first_one_by_locator(driver, pages):33pages.load("relative_locators.html")3435el = driver.find_element(with_tag_name("p").above({By.ID: "below"}))3637assert el.get_attribute("id") == "mid"383940def test_should_be_able_to_find_elements_above_another(driver, pages):41pages.load("relative_locators.html")42lowest = driver.find_element(By.ID, "below")4344elements = driver.find_elements(with_tag_name("p").above(lowest))4546ids = [el.get_attribute("id") for el in elements]47assert "above" in ids48assert "mid" in ids495051def test_should_be_able_to_find_elements_above_another_by_locator(driver, pages):52pages.load("relative_locators.html")5354elements = driver.find_elements(with_tag_name("p").above({By.ID: "below"}))5556ids = [el.get_attribute("id") for el in elements]57assert "above" in ids58assert "mid" in ids596061def test_should_be_able_to_combine_filters(driver, pages):62pages.load("relative_locators.html")6364elements = driver.find_elements(65with_tag_name("td").above(driver.find_element(By.ID, "center")).to_right_of(driver.find_element(By.ID, "top"))66)6768ids = [el.get_attribute("id") for el in elements]69assert "topRight" in ids707172def test_should_be_able_to_combine_filters_by_locator(driver, pages):73pages.load("relative_locators.html")7475elements = driver.find_elements(with_tag_name("td").above({By.ID: "center"}).to_right_of({By.ID: "top"}))7677ids = [el.get_attribute("id") for el in elements]78assert "topRight" in ids798081def test_should_be_able_to_use_css_selectors(driver, pages):82pages.load("relative_locators.html")8384elements = driver.find_elements(85locate_with(By.CSS_SELECTOR, "td")86.above(driver.find_element(By.ID, "center"))87.to_right_of(driver.find_element(By.ID, "top"))88)8990ids = [el.get_attribute("id") for el in elements]91assert "topRight" in ids929394def test_should_be_able_to_use_css_selectors_by_locator(driver, pages):95pages.load("relative_locators.html")9697elements = driver.find_elements(98locate_with(By.CSS_SELECTOR, "td").above({By.ID: "center"}).to_right_of({By.ID: "top"})99)100101ids = [el.get_attribute("id") for el in elements]102assert "topRight" in ids103104105def test_should_be_able_to_use_xpath(driver, pages):106pages.load("relative_locators.html")107108elements = driver.find_elements(109locate_with(By.XPATH, "//td[1]")110.below(driver.find_element(By.ID, "top"))111.above(driver.find_element(By.ID, "bottomLeft"))112)113114ids = [el.get_attribute("id") for el in elements]115assert "left" in ids116117118def test_should_be_able_to_use_xpath_by_locator(driver, pages):119pages.load("relative_locators.html")120121elements = driver.find_elements(locate_with(By.XPATH, "//td[1]").below({By.ID: "top"}).above({By.ID: "bottomLeft"}))122123ids = [el.get_attribute("id") for el in elements]124assert "left" in ids125126127def test_should_be_able_to_combine_straight_filters(driver, pages):128pages.load("relative_locators.html")129130elements = driver.find_elements(131with_tag_name("td")132.straight_below(driver.find_element(By.ID, "topRight"))133.straight_right_of(driver.find_element(By.ID, "bottomLeft"))134)135136ids = [el.get_attribute("id") for el in elements]137assert len(ids) == 1138assert "bottomRight" in ids139140141def test_no_such_element_is_raised_rather_than_index_error(driver, pages):142pages.load("relative_locators.html")143with pytest.raises(NoSuchElementException) as exc:144anchor = driver.find_element(By.ID, "top")145driver.find_element(locate_with(By.ID, "nonexistentid").above(anchor))146assert "Cannot locate relative element with: {'id': 'nonexistentid'}" in exc.value.msg147148149def test_no_such_element_is_raised_rather_than_index_error_by_locator(driver, pages):150pages.load("relative_locators.html")151with pytest.raises(NoSuchElementException) as exc:152driver.find_element(locate_with(By.ID, "nonexistentid").above({By.ID: "top"}))153assert "Cannot locate relative element with: {'id': 'nonexistentid'}" in exc.value.msg154155156def test_near_locator_should_find_near_elements(driver, pages):157pages.load("relative_locators.html")158rect = driver.find_element(By.ID, "rect1")159160el = driver.find_element(locate_with(By.ID, "rect2").near(rect))161162assert el.get_attribute("id") == "rect2"163164165def test_near_locator_should_find_near_elements_by_locator(driver, pages):166pages.load("relative_locators.html")167168el = driver.find_element(locate_with(By.ID, "rect2").near({By.ID: "rect1"}))169170assert el.get_attribute("id") == "rect2"171172173def test_near_locator_should_not_find_far_elements(driver, pages):174pages.load("relative_locators.html")175rect = driver.find_element(By.ID, "rect2")176177with pytest.raises(NoSuchElementException) as exc:178driver.find_element(locate_with(By.ID, "rect4").near(rect))179180assert "Cannot locate relative element with: {'id': 'rect4'}" in exc.value.msg181182183def test_near_locator_should_not_find_far_elements_by_locator(driver, pages):184pages.load("relative_locators.html")185186with pytest.raises(NoSuchElementException) as exc:187driver.find_element(locate_with(By.ID, "rect4").near({By.ID: "rect2"}))188189assert "Cannot locate relative element with: {'id': 'rect4'}" in exc.value.msg190191192def test_near_locator_should_find_far_elements(driver, pages):193pages.load("relative_locators.html")194rect = driver.find_element(By.ID, "rect2")195196el = driver.find_element(locate_with(By.ID, "rect4").near(rect, 100))197198assert el.get_attribute("id") == "rect4"199200201def test_near_locator_should_find_far_elements_by_locator(driver, pages):202pages.load("relative_locators.html")203204el = driver.find_element(locate_with(By.ID, "rect4").near({By.ID: "rect2"}, 100))205206assert el.get_attribute("id") == "rect4"207208209def test_should_find_elements_above_another(driver, pages):210pages.load("relative_locators.html")211212elements = driver.find_elements(with_tag_name("td").above({By.ID: "center"}))213214ids = [el.get_attribute("id") for el in elements]215assert len(ids) == 3216assert "top" in ids217assert "topLeft" in ids218assert "topRight" in ids219220221def test_should_find_elements_below_another(driver, pages):222pages.load("relative_locators.html")223224elements = driver.find_elements(with_tag_name("td").below({By.ID: "center"}))225226ids = [el.get_attribute("id") for el in elements]227assert len(ids) == 3228assert "bottom" in ids229assert "bottomLeft" in ids230assert "bottomRight" in ids231232233def test_should_find_elements_left_of_another(driver, pages):234pages.load("relative_locators.html")235236elements = driver.find_elements(with_tag_name("td").to_left_of({By.ID: "center"}))237238ids = [el.get_attribute("id") for el in elements]239assert len(ids) == 3240assert "left" in ids241assert "topLeft" in ids242assert "bottomLeft" in ids243244245def test_should_find_elements_right_of_another(driver, pages):246pages.load("relative_locators.html")247248elements = driver.find_elements(with_tag_name("td").to_right_of({By.ID: "center"}))249250ids = [el.get_attribute("id") for el in elements]251assert len(ids) == 3252assert "right" in ids253assert "topRight" in ids254assert "bottomRight" in ids255256257def test_should_find_elements_straight_above_another(driver, pages):258pages.load("relative_locators.html")259260elements = driver.find_elements(with_tag_name("td").straight_above({By.ID: "bottom"}))261262ids = [el.get_attribute("id") for el in elements]263assert len(ids) == 2264assert "top" in ids265assert "center" in ids266267268def test_should_find_elements_straight_below_another(driver, pages):269pages.load("relative_locators.html")270271elements = driver.find_elements(with_tag_name("td").straight_below({By.ID: "top"}))272273ids = [el.get_attribute("id") for el in elements]274assert len(ids) == 2275assert "bottom" in ids276assert "center" in ids277278279def test_should_find_elements_straight_left_of_another(driver, pages):280pages.load("relative_locators.html")281282elements = driver.find_elements(with_tag_name("td").straight_left_of({By.ID: "right"}))283284ids = [el.get_attribute("id") for el in elements]285assert len(ids) == 2286assert "left" in ids287assert "center" in ids288289290def test_should_find_elements_straight_right_of_another(driver, pages):291pages.load("relative_locators.html")292293elements = driver.find_elements(with_tag_name("td").straight_right_of({By.ID: "left"}))294295ids = [el.get_attribute("id") for el in elements]296assert len(ids) == 2297assert "right" in ids298assert "center" in ids299300301