Path: blob/trunk/py/test/selenium/webdriver/common/children_finding_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.1617import pytest1819from selenium.common.exceptions import NoSuchElementException, WebDriverException20from selenium.webdriver.common.by import By212223def test_should_find_element_by_xpath(driver, pages):24pages.load("nestedElements.html")25element = driver.find_element(By.NAME, "form2")26child = element.find_element(By.XPATH, "select")27assert child.get_attribute("id") == "2"282930def test_should_not_find_element_by_xpath(driver, pages):31pages.load("nestedElements.html")32element = driver.find_element(By.NAME, "form2")33with pytest.raises(NoSuchElementException):34element.find_element(By.XPATH, "select/x")353637def test_finding_dot_slash_elements_on_element_by_xpath_should_find_not_top_level_elements(driver, pages):38pages.load("simpleTest.html")39parent = driver.find_element(By.ID, "multiline")40children = parent.find_elements(By.XPATH, "./p")41assert 1 == len(children)42assert "A div containing" == children[0].text434445def test_should_find_elements_by_xpath(driver, pages):46pages.load("nestedElements.html")47element = driver.find_element(By.NAME, "form2")48children = element.find_elements(By.XPATH, "select/option")49assert len(children) == 850assert children[0].text == "One"51assert children[1].text == "Two"525354def test_should_not_find_elements_by_xpath(driver, pages):55pages.load("nestedElements.html")56element = driver.find_element(By.NAME, "form2")57children = element.find_elements(By.XPATH, "select/x")58assert len(children) == 0596061def test_finding_elements_on_element_by_xpath_should_find_top_level_elements(driver, pages):62pages.load("simpleTest.html")63parent = driver.find_element(By.ID, "multiline")64all_para_elements = driver.find_elements(By.XPATH, "//p")65children = parent.find_elements(By.XPATH, "//p")66assert len(all_para_elements) == len(children)676869def test_should_find_element_by_name(driver, pages):70pages.load("nestedElements.html")71element = driver.find_element(By.NAME, "form2")72child = element.find_element(By.NAME, "selectomatic")73assert child.get_attribute("id") == "2"747576def test_should_find_elements_by_name(driver, pages):77pages.load("nestedElements.html")78element = driver.find_element(By.NAME, "form2")79children = element.find_elements(By.NAME, "selectomatic")80assert len(children) == 2818283def test_should_find_element_by_id(driver, pages):84pages.load("nestedElements.html")85element = driver.find_element(By.NAME, "form2")86child = element.find_element(By.ID, "2")87assert child.get_attribute("name") == "selectomatic"888990def test_should_find_elements_by_id(driver, pages):91pages.load("nestedElements.html")92element = driver.find_element(By.NAME, "form2")93child = element.find_elements(By.ID, "2")94assert len(child) == 2959697def test_should_find_element_by_id_when_multiple_matches_exist(driver, pages):98pages.load("nestedElements.html")99element = driver.find_element(By.ID, "test_id_div")100child = element.find_element(By.ID, "test_id")101assert child.text == "inside"102103104def test_should_find_element_by_id_when_no_match_in_context(driver, pages):105pages.load("nestedElements.html")106element = driver.find_element(By.ID, "test_id_div")107with pytest.raises(NoSuchElementException):108element.find_element(By.ID, "test_id_out")109110111def test_should_find_element_by_link_text(driver, pages):112pages.load("nestedElements.html")113element = driver.find_element(By.NAME, "div1")114child = element.find_element(By.LINK_TEXT, "hello world")115assert child.get_attribute("name") == "link1"116117118def test_should_find_elements_by_link_text(driver, pages):119pages.load("nestedElements.html")120element = driver.find_element(By.NAME, "div1")121children = element.find_elements(By.LINK_TEXT, "hello world")122assert len(children) == 2123assert "link1" == children[0].get_attribute("name")124assert "link2" == children[1].get_attribute("name")125126127def test_should_find_element_by_class_name(driver, pages):128pages.load("nestedElements.html")129parent = driver.find_element(By.NAME, "classes")130element = parent.find_element(By.CLASS_NAME, "one")131assert "Find me" == element.text132133134def test_should_find_elements_by_class_name(driver, pages):135pages.load("nestedElements.html")136parent = driver.find_element(By.NAME, "classes")137elements = parent.find_elements(By.CLASS_NAME, "one")138assert 2 == len(elements)139140141def test_should_find_element_by_tag_name(driver, pages):142pages.load("nestedElements.html")143parent = driver.find_element(By.NAME, "div1")144element = parent.find_element(By.TAG_NAME, "a")145assert "link1" == element.get_attribute("name")146147148def test_should_find_elements_by_tag_name(driver, pages):149pages.load("nestedElements.html")150parent = driver.find_element(By.NAME, "div1")151elements = parent.find_elements(By.TAG_NAME, "a")152assert 2 == len(elements)153154155def test_should_be_able_to_find_an_element_by_css_selector(driver, pages):156pages.load("nestedElements.html")157parent = driver.find_element(By.NAME, "form2")158element = parent.find_element(By.CSS_SELECTOR, '*[name="selectomatic"]')159assert "2" == element.get_attribute("id")160161162def test_should_be_able_to_find_multiple_elements_by_css_selector(driver, pages):163pages.load("nestedElements.html")164parent = driver.find_element(By.NAME, "form2")165elements = parent.find_elements(By.CSS_SELECTOR, '*[name="selectomatic"]')166assert 2 == len(elements)167168169def test_should_throw_an_error_if_user_passes_in_invalid_by(driver, pages):170pages.load("nestedElements.html")171element = driver.find_element(By.NAME, "form2")172with pytest.raises(WebDriverException):173element.find_element("foo", "bar")174175176def test_should_throw_an_error_if_user_passes_in_invalid_by_when_find_elements(driver, pages):177pages.load("nestedElements.html")178element = driver.find_element(By.NAME, "form2")179with pytest.raises(WebDriverException):180element.find_elements("foo", "bar")181182183