Path: blob/trunk/py/test/selenium/webdriver/common/frame_switching_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, NoSuchFrameException, WebDriverException20from selenium.webdriver.common.by import By21from selenium.webdriver.support import expected_conditions as EC22from selenium.webdriver.support.ui import WebDriverWait2324# ----------------------------------------------------------------------------------------------25#26# Tests that WebDriver doesn't do anything fishy when it navigates to a page with frames.27#28# ----------------------------------------------------------------------------------------------293031@pytest.fixture(autouse=True)32def restore_default_context(driver):33yield34driver.switch_to.default_content()353637def test_should_always_focus_on_the_top_most_frame_after_anavigation_event(driver, pages):38pages.load("frameset.html")39driver.find_element(By.TAG_NAME, "frameset") # Test passes if this does not throw.404142def test_should_not_automatically_switch_focus_to_an_iframe_when_apage_containing_them_is_loaded(driver, pages):43pages.load("iframes.html")44driver.find_element(By.ID, "iframe_page_heading")454647def test_should_open_page_with_broken_frameset(driver, pages):48pages.load("framesetPage3.html")4950frame1 = driver.find_element(By.ID, "first")51driver.switch_to.frame(frame1)52driver.switch_to.default_content()5354frame2 = driver.find_element(By.ID, "second")55driver.switch_to.frame(frame2) # IE9 can not switch to this broken frame - it has no window.565758# ----------------------------------------------------------------------------------------------59#60# Tests that WebDriver can switch to frames as expected.61#62# ----------------------------------------------------------------------------------------------636465def test_should_be_able_to_switch_to_aframe_by_its_index(driver, pages):66pages.load("frameset.html")67driver.switch_to.frame(1)68assert driver.find_element(By.ID, "pageNumber").text == "2"697071def test_should_be_able_to_switch_to_an_iframe_by_its_index(driver, pages):72pages.load("iframes.html")73driver.switch_to.frame(0)74assert driver.find_element(By.NAME, "id-name1").get_attribute("value") == "name"757677def test_should_be_able_to_switch_to_aframe_by_its_name(driver, pages):78pages.load("frameset.html")79driver.switch_to.frame("fourth")80assert driver.find_element(By.TAG_NAME, "frame").get_attribute("name") == "child1"818283def test_should_be_able_to_switch_to_an_iframe_by_its_name(driver, pages):84pages.load("iframes.html")85driver.switch_to.frame("iframe1-name")86assert driver.find_element(By.NAME, "id-name1").get_attribute("value") == "name"878889def test_should_be_able_to_switch_to_aframe_by_its_id(driver, pages):90pages.load("frameset.html")91driver.switch_to.frame("fifth")92assert driver.find_element(By.NAME, "windowOne").text == "Open new window"939495def test_should_be_able_to_switch_to_an_iframe_by_its_id(driver, pages):96pages.load("iframes.html")97driver.switch_to.frame("iframe1")98assert driver.find_element(By.NAME, "id-name1").get_attribute("value") == "name"99100101def test_should_be_able_to_switch_to_frame_with_name_containing_dot(driver, pages):102pages.load("frameset.html")103driver.switch_to.frame("sixth.iframe1")104assert "Page number 3" in driver.find_element(By.TAG_NAME, "body").text105106107def test_should_be_able_to_switch_to_aframe_using_apreviously_located_web_element(driver, pages):108pages.load("frameset.html")109frame = driver.find_element(By.TAG_NAME, "frame")110driver.switch_to.frame(frame)111assert driver.find_element(By.ID, "pageNumber").text == "1"112113114def test_should_be_able_to_switch_to_an_iframe_using_apreviously_located_web_element(driver, pages):115pages.load("iframes.html")116frame = driver.find_element(By.TAG_NAME, "iframe")117driver.switch_to.frame(frame)118119element = driver.find_element(By.NAME, "id-name1")120assert element.get_attribute("value") == "name"121122123def test_should_ensure_element_is_aframe_before_switching(driver, pages):124pages.load("frameset.html")125frame = driver.find_element(By.TAG_NAME, "frameset")126with pytest.raises(NoSuchFrameException):127driver.switch_to.frame(frame)128129130def test_frame_searches_should_be_relative_to_the_currently_selected_frame(driver, pages):131pages.load("frameset.html")132driver.switch_to.frame("second")133assert driver.find_element(By.ID, "pageNumber").text == "2"134135with pytest.raises(NoSuchElementException):136driver.switch_to.frame(driver.find_element(By.NAME, "third"))137138driver.switch_to.default_content()139driver.switch_to.frame(driver.find_element(By.NAME, "third"))140141with pytest.raises(NoSuchFrameException):142driver.switch_to.frame("second")143144driver.switch_to.default_content()145driver.switch_to.frame(driver.find_element(By.NAME, "second"))146assert driver.find_element(By.ID, "pageNumber").text == "2"147148149def test_should_select_child_frames_by_chained_calls(driver, pages):150pages.load("frameset.html")151driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))152driver.switch_to.frame(driver.find_element(By.NAME, "child2"))153assert driver.find_element(By.ID, "pageNumber").text == "11"154155156def test_should_throw_frame_not_found_exception_looking_up_sub_frames_with_super_frame_names(driver, pages):157pages.load("frameset.html")158driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))159with pytest.raises(NoSuchElementException):160driver.switch_to.frame(driver.find_element(By.NAME, "second"))161162163def test_should_throw_an_exception_when_aframe_cannot_be_found(driver, pages):164pages.load("xhtmlTest.html")165with pytest.raises(NoSuchElementException):166driver.switch_to.frame(driver.find_element(By.NAME, "Nothing here"))167168169def test_should_throw_an_exception_when_aframe_cannot_be_found_by_index(driver, pages):170pages.load("xhtmlTest.html")171with pytest.raises(NoSuchFrameException):172driver.switch_to.frame(27)173174175def test_should_be_able_to_switch_to_parent_frame(driver, pages):176pages.load("frameset.html")177driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))178driver.switch_to.parent_frame()179driver.switch_to.frame(driver.find_element(By.NAME, "first"))180assert driver.find_element(By.ID, "pageNumber").text == "1"181182183@pytest.mark.xfail_safari184def test_should_be_able_to_switch_to_parent_frame_from_asecond_level_frame(driver, pages):185pages.load("frameset.html")186driver.switch_to.frame(driver.find_element(By.NAME, "fourth"))187driver.switch_to.frame(driver.find_element(By.NAME, "child1"))188driver.switch_to.parent_frame()189driver.switch_to.frame(driver.find_element(By.NAME, "child2"))190assert driver.find_element(By.ID, "pageNumber").text == "11"191192193def test_switching_to_parent_frame_from_default_context_is_no_op(driver, pages):194pages.load("xhtmlTest.html")195driver.switch_to.parent_frame()196assert driver.title == "XHTML Test Page"197198199def test_should_be_able_to_switch_to_parent_from_an_iframe(driver, pages):200pages.load("iframes.html")201driver.switch_to.frame(0)202driver.switch_to.parent_frame()203driver.find_element(By.ID, "iframe_page_heading")204205206# ----------------------------------------------------------------------------------------------207#208# General frame handling behavior tests209#210# ----------------------------------------------------------------------------------------------211212213def test_should_continue_to_refer_to_the_same_frame_once_it_has_been_selected(driver, pages):214pages.load("frameset.html")215driver.switch_to.frame(2)216checkbox = driver.find_element(By.XPATH, "//input[@name='checky']")217checkbox.click()218checkbox.submit()219220# TODO(simon): this should not be needed, and is only here because IE's submit returns too221# soon.222223WebDriverWait(driver, 3).until(EC.text_to_be_present_in_element((By.XPATH, "//p"), "Success!"))224225226@pytest.mark.xfail_firefox(raises=WebDriverException, reason="https://github.com/mozilla/geckodriver/issues/610")227@pytest.mark.xfail_remote(raises=WebDriverException, reason="https://github.com/mozilla/geckodriver/issues/610")228@pytest.mark.xfail_safari229@pytest.mark.xfail_chrome230@pytest.mark.xfail_edge231def test_should_focus_on_the_replacement_when_aframe_follows_alink_to_a_top_targeted_page(driver, pages):232pages.load("frameset.html")233driver.switch_to.frame(0)234driver.find_element(By.LINK_TEXT, "top").click()235236expectedTitle = "XHTML Test Page"237238WebDriverWait(driver, 3).until(EC.title_is(expectedTitle))239WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "only-exists-on-xhtmltest")))240241242def test_should_allow_auser_to_switch_from_an_iframe_back_to_the_main_content_of_the_page(driver, pages):243pages.load("iframes.html")244driver.switch_to.frame(0)245driver.switch_to.default_content()246driver.find_element(By.ID, "iframe_page_heading")247248249def test_should_allow_the_user_to_switch_to_an_iframe_and_remain_focused_on_it(driver, pages):250pages.load("iframes.html")251driver.switch_to.frame(0)252driver.find_element(By.ID, "submitButton").click()253assert get_text_of_greeting_element(driver) == "Success!"254255256def get_text_of_greeting_element(driver):257return WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "greeting"))).text258259260def test_should_be_able_to_click_in_aframe(driver, pages):261pages.load("frameset.html")262driver.switch_to.frame("third")263264# This should replace frame "third" ...265driver.find_element(By.ID, "submitButton").click()266# driver should still be focused on frame "third" ...267assert get_text_of_greeting_element(driver) == "Success!"268# Make sure it was really frame "third" which was replaced ...269driver.switch_to.default_content()270driver.switch_to.frame("third")271assert get_text_of_greeting_element(driver) == "Success!"272273274def test_should_be_able_to_click_in_aframe_that_rewrites_top_window_location(driver, pages):275pages.load("click_tests/issue5237.html")276driver.switch_to.frame(driver.find_element(By.ID, "search"))277driver.find_element(By.ID, "submit").click()278driver.switch_to.default_content()279WebDriverWait(driver, 3).until(EC.title_is("Target page for issue 5237"))280281282def test_should_be_able_to_click_in_asub_frame(driver, pages):283pages.load("frameset.html")284driver.switch_to.frame(driver.find_element(By.ID, "sixth"))285driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))286287# This should replace frame "iframe1" inside frame "sixth" ...288driver.find_element(By.ID, "submitButton").click()289# driver should still be focused on frame "iframe1" inside frame "sixth" ...290assert get_text_of_greeting_element(driver), "Success!"291# Make sure it was really frame "iframe1" inside frame "sixth" which was replaced ...292driver.switch_to.default_content()293driver.switch_to.frame(driver.find_element(By.ID, "sixth"))294driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))295assert driver.find_element(By.ID, "greeting").text == "Success!"296297298def test_should_be_able_to_find_elements_in_iframes_by_xpath(driver, pages):299pages.load("iframes.html")300driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))301element = driver.find_element(By.XPATH, "//*[@id = 'changeme']")302assert element is not None303304305def test_get_current_url_returns_top_level_browsing_context_url(driver, pages):306pages.load("frameset.html")307assert "frameset.html" in driver.current_url308driver.switch_to.frame(driver.find_element(By.NAME, "second"))309assert "frameset.html" in driver.current_url310311312def test_get_current_url_returns_top_level_browsing_context_url_for_iframes(driver, pages):313pages.load("iframes.html")314assert "iframes.html" in driver.current_url315driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))316assert "iframes.html" in driver.current_url317318319def test_should_be_able_to_switch_to_the_top_if_the_frame_is_deleted_from_under_us(driver, pages):320pages.load("frame_switching_tests/deletingFrame.html")321driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))322323killIframe = driver.find_element(By.ID, "killIframe")324killIframe.click()325driver.switch_to.default_content()326WebDriverWait(driver, 3).until_not(EC.presence_of_element_located((By.ID, "iframe1")))327328addIFrame = driver.find_element(By.ID, "addBackFrame")329addIFrame.click()330WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "iframe1")))331driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))332WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "success")))333334335def test_should_be_able_to_switch_to_the_top_if_the_frame_is_deleted_from_under_us_with_frame_index(driver, pages):336pages.load("frame_switching_tests/deletingFrame.html")337iframe = 0338WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))339# we should be in the frame now340killIframe = driver.find_element(By.ID, "killIframe")341killIframe.click()342driver.switch_to.default_content()343344addIFrame = driver.find_element(By.ID, "addBackFrame")345addIFrame.click()346WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))347WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "success")))348349350def test_frame_to_be_available_and_switch_to_it_using_string_inputs(driver, pages):351# Similar to above test, but using `iframe = "iframe1"` instead of `iframe = 0`352pages.load("frame_switching_tests/deletingFrame.html")353iframe = "iframe1"354WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))355# we should be in the frame now356killIframe = driver.find_element(By.ID, "killIframe")357killIframe.click()358driver.switch_to.default_content()359360addIFrame = driver.find_element(By.ID, "addBackFrame")361addIFrame.click()362WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))363WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "success")))364365366def test_should_be_able_to_switch_to_the_top_if_the_frame_is_deleted_from_under_us_with_webelement(driver, pages):367pages.load("frame_switching_tests/deletingFrame.html")368iframe = driver.find_element(By.ID, "iframe1")369WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))370# we should be in the frame now371killIframe = driver.find_element(By.ID, "killIframe")372killIframe.click()373driver.switch_to.default_content()374375addIFrame = driver.find_element(By.ID, "addBackFrame")376addIFrame.click()377378iframe = driver.find_element(By.ID, "iframe1")379WebDriverWait(driver, 3).until(EC.frame_to_be_available_and_switch_to_it(iframe))380WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "success")))381382383# @pytest.mark.xfail_chrome(raises=NoSuchElementException)384# @pytest.mark.xfail_edge(raises=NoSuchElementException)385# @pytest.mark.xfail_firefox(raises=WebDriverException,386# reason='https://github.com/mozilla/geckodriver/issues/614')387# @pytest.mark.xfail_remote(raises=WebDriverException,388# reason='https://github.com/mozilla/geckodriver/issues/614')389# @pytest.mark.xfail_webkitgtk(raises=NoSuchElementException)390# @pytest.mark.xfail_safari391# def test_should_not_be_able_to_do_anything_the_frame_is_deleted_from_under_us(driver, pages):392# pages.load("frame_switching_tests/deletingFrame.html")393# driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))394395# killIframe = driver.find_element(By.ID, "killIframe")396# killIframe.click()397398# with pytest.raises(NoSuchFrameException):399# driver.find_element(By.ID, "killIframe").click()400401402def test_should_return_window_title_in_aframeset(driver, pages):403pages.load("frameset.html")404driver.switch_to.frame(driver.find_element(By.NAME, "third"))405assert "Unique title" == driver.title406407408def test_java_script_should_execute_in_the_context_of_the_current_frame(driver, pages):409pages.load("frameset.html")410assert driver.execute_script("return window == window.top")411driver.switch_to.frame(driver.find_element(By.NAME, "third"))412assert driver.execute_script("return window != window.top")413414415def test_get_should_switch_to_default_context(driver, pages):416pages.load("iframes.html")417driver.find_element(By.ID, "iframe1")418driver.switch_to.frame(driver.find_element(By.ID, "iframe1"))419driver.find_element(By.ID, "cheese") # Found on formPage.html but not on iframes.html.420pages.load("iframes.html") # This must effectively switch_to.default_content(), too.421driver.find_element(By.ID, "iframe1")422423424