Path: blob/trunk/java/test/org/openqa/selenium/FrameSwitchingTest.java
3987 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.1617package org.openqa.selenium;1819import static org.assertj.core.api.Assertions.assertThat;20import static org.assertj.core.api.Assertions.assertThatExceptionOfType;21import static org.openqa.selenium.support.ui.ExpectedConditions.frameToBeAvailableAndSwitchToIt;22import static org.openqa.selenium.support.ui.ExpectedConditions.not;23import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;24import static org.openqa.selenium.support.ui.ExpectedConditions.textToBe;25import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;26import static org.openqa.selenium.testing.drivers.Browser.CHROME;27import static org.openqa.selenium.testing.drivers.Browser.EDGE;28import static org.openqa.selenium.testing.drivers.Browser.IE;29import static org.openqa.selenium.testing.drivers.Browser.SAFARI;3031import java.util.Random;32import org.junit.jupiter.api.AfterEach;33import org.junit.jupiter.api.Test;34import org.junit.jupiter.api.Timeout;35import org.openqa.selenium.testing.Ignore;36import org.openqa.selenium.testing.JupiterTestBase;37import org.openqa.selenium.testing.NotYetImplemented;3839class FrameSwitchingTest extends JupiterTestBase {4041@AfterEach42public void tearDown() {43try {44driver.switchTo().defaultContent();45} catch (Exception e) {46// May happen if the driver went away.47}48}4950// ----------------------------------------------------------------------------------------------51//52// Tests that WebDriver doesn't do anything fishy when it navigates to a page with frames.53//54// ----------------------------------------------------------------------------------------------55@Test56void testShouldAlwaysFocusOnTheTopMostFrameAfterANavigationEvent() {57driver.get(pages.framesetPage);58driver.findElement(By.tagName("frameset")); // Test passes if this does not throw.59}6061@Test62void testShouldNotAutomaticallySwitchFocusToAnIFrameWhenAPageContainingThemIsLoaded() {63driver.get(pages.iframePage);64driver.findElement(By.id("iframe_page_heading"));65}6667@Test68@Timeout(10)69public void testShouldOpenPageWithBrokenFrameset() {70driver.get(appServer.whereIs("framesetPage3.html"));7172WebElement frame1 = driver.findElement(By.id("first"));73driver.switchTo().frame(frame1);7475driver.switchTo().defaultContent();7677WebElement frame2 = driver.findElement(By.id("second"));7879try {80driver.switchTo().frame(frame2);81} catch (WebDriverException e) {82// IE9 can not switch to this broken frame - it has no window.83}84}8586// ----------------------------------------------------------------------------------------------87//88// Tests that WebDriver can switch to frames as expected.89//90// ----------------------------------------------------------------------------------------------91@Test92void testShouldBeAbleToSwitchToAFrameByItsIndex() {93driver.get(pages.framesetPage);94driver.switchTo().frame(1);9596assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");97}9899@Test100void testShouldBeAbleToSwitchToAnIframeByItsIndex() {101driver.get(pages.iframePage);102driver.switchTo().frame(0);103104assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");105}106107@Test108void testShouldBeAbleToSwitchToAFrameByItsName() {109driver.get(pages.framesetPage);110driver.switchTo().frame("fourth");111112assertThat(driver.findElement(By.tagName("frame")).getAttribute("name")).isEqualTo("child1");113}114115@Test116void testShouldBeAbleToSwitchToAnIframeByItsName() {117driver.get(pages.iframePage);118driver.switchTo().frame("iframe1-name");119120assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");121}122123@Test124void testShouldBeAbleToSwitchToAFrameByItsID() {125driver.get(pages.framesetPage);126driver.switchTo().frame("fifth");127assertThat(driver.findElement(By.name("windowOne")).getText()).isEqualTo("Open new window");128}129130@Test131void testShouldBeAbleToSwitchToAnIframeByItsID() {132driver.get(pages.iframePage);133driver.switchTo().frame("iframe1");134135assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");136}137138@Test139void testShouldBeAbleToSwitchToFrameWithNameContainingDot() {140driver.get(pages.framesetPage);141driver.switchTo().frame("sixth.iframe1");142assertThat(driver.findElement(By.tagName("body")).getText()).contains("Page number 3");143}144145@Test146void testShouldBeAbleToSwitchToAFrameUsingAPreviouslyLocatedWebElement() {147driver.get(pages.framesetPage);148WebElement frame = driver.findElement(By.tagName("frame"));149driver.switchTo().frame(frame);150151assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("1");152}153154@Test155void testShouldBeAbleToSwitchToAnIFrameUsingAPreviouslyLocatedWebElement() {156driver.get(pages.iframePage);157WebElement frame = driver.findElement(By.tagName("iframe"));158driver.switchTo().frame(frame);159160WebElement element = driver.findElement(By.name("id-name1"));161assertThat(element.getAttribute("value")).isEqualTo("name");162}163164@Test165void testShouldEnsureElementIsAFrameBeforeSwitching() {166driver.get(pages.framesetPage);167WebElement frame = driver.findElement(By.tagName("frameset"));168169assertThatExceptionOfType(NoSuchFrameException.class)170.isThrownBy(() -> driver.switchTo().frame(frame));171}172173@Test174void testFrameSearchesShouldBeRelativeToTheCurrentlySelectedFrame() {175driver.get(pages.framesetPage);176177driver.switchTo().frame("second");178assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");179180assertThatExceptionOfType(NoSuchFrameException.class)181.isThrownBy(() -> driver.switchTo().frame("third"));182183driver.switchTo().defaultContent();184driver.switchTo().frame("third");185186assertThatExceptionOfType(NoSuchFrameException.class)187.isThrownBy(() -> driver.switchTo().frame("second"));188189driver.switchTo().defaultContent();190driver.switchTo().frame("second");191assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");192}193194@Test195void testShouldSelectChildFramesByChainedCalls() {196driver.get(pages.framesetPage);197198driver.switchTo().frame("fourth").switchTo().frame("child2");199assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("11");200}201202@Test203void testShouldThrowFrameNotFoundExceptionLookingUpSubFramesWithSuperFrameNames() {204driver.get(pages.framesetPage);205driver.switchTo().frame("fourth");206207assertThatExceptionOfType(NoSuchFrameException.class)208.isThrownBy(() -> driver.switchTo().frame("second"));209}210211@Test212void testShouldThrowAnExceptionWhenAFrameCannotBeFound() {213driver.get(pages.xhtmlTestPage);214215assertThatExceptionOfType(NoSuchFrameException.class)216.isThrownBy(() -> driver.switchTo().frame("Nothing here"));217}218219@Test220void testShouldThrowAnExceptionWhenAFrameCannotBeFoundByIndex() {221driver.get(pages.xhtmlTestPage);222223assertThatExceptionOfType(NoSuchFrameException.class)224.isThrownBy(() -> driver.switchTo().frame(27));225}226227@Test228void testShouldBeAbleToSwitchToParentFrame() {229driver.get(pages.framesetPage);230231driver.switchTo().frame("fourth").switchTo().parentFrame().switchTo().frame("first");232assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("1");233}234235@Test236@NotYetImplemented(SAFARI)237public void testShouldBeAbleToSwitchToParentFrameFromASecondLevelFrame() {238driver.get(pages.framesetPage);239240driver241.switchTo()242.frame("fourth")243.switchTo()244.frame("child1")245.switchTo()246.parentFrame()247.switchTo()248.frame("child2");249assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("11");250}251252@Test253void testSwitchingToParentFrameFromDefaultContextIsNoOp() {254driver.get(pages.xhtmlTestPage);255driver.switchTo().parentFrame();256assertThat(driver.getTitle()).isEqualTo("XHTML Test Page");257}258259@Test260void testShouldBeAbleToSwitchToParentFromAnIframe() {261driver.get(pages.iframePage);262driver.switchTo().frame(0);263264driver.switchTo().parentFrame();265driver.findElement(By.id("iframe_page_heading"));266}267268// ----------------------------------------------------------------------------------------------269//270// General frame handling behavior tests271//272// ----------------------------------------------------------------------------------------------273274@Test275void testShouldContinueToReferToTheSameFrameOnceItHasBeenSelected() {276driver.get(pages.framesetPage);277278driver.switchTo().frame(2);279WebElement checkbox = driver.findElement(By.xpath("//input[@name='checky']"));280checkbox.click();281checkbox.submit();282283wait.until(textToBe(By.xpath("//p"), "Success!"));284}285286@Test287@NotYetImplemented(SAFARI)288public void testShouldFocusOnTheReplacementWhenAFrameFollowsALinkToA_TopTargetedPage() {289driver.get(pages.framesetPage);290291driver.switchTo().frame(0);292driver.findElement(By.linkText("top")).click();293294String expectedTitle = "XHTML Test Page";295296wait.until(titleIs(expectedTitle));297}298299@Test300void testShouldAllowAUserToSwitchFromAnIframeBackToTheMainContentOfThePage() {301driver.get(pages.iframePage);302driver.switchTo().frame(0);303304driver.switchTo().defaultContent();305driver.findElement(By.id("iframe_page_heading"));306}307308@Test309void testShouldAllowTheUserToSwitchToAnIFrameAndRemainFocusedOnIt() {310driver.get(pages.iframePage);311driver.switchTo().frame(0);312313driver.findElement(By.id("submitButton")).click();314315assertThat(getTextOfGreetingElement()).isEqualTo("Success!");316}317318public String getTextOfGreetingElement() {319return wait.until(presenceOfElementLocated(By.id("greeting"))).getText();320}321322@Test323void testShouldBeAbleToClickInAFrame() {324driver.get(pages.framesetPage);325driver.switchTo().frame("third");326327// This should replace frame "third" ...328driver.findElement(By.id("submitButton")).click();329// driver should still be focused on frame "third" ...330assertThat(getTextOfGreetingElement()).isEqualTo("Success!");331// Make sure it was really frame "third" which was replaced ...332driver.switchTo().defaultContent().switchTo().frame("third");333assertThat(getTextOfGreetingElement()).isEqualTo("Success!");334}335336@Test337void testShouldBeAbleToClickInAFrameThatRewritesTopWindowLocation() {338driver.get(appServer.whereIs("click_tests/issue5237.html"));339driver.switchTo().frame("search");340driver.findElement(By.id("submit")).click();341driver.switchTo().defaultContent();342wait.until(titleIs("Target page for issue 5237"));343}344345@Test346void testShouldBeAbleToClickInASubFrame() {347driver.get(pages.framesetPage);348driver.switchTo().frame("sixth").switchTo().frame("iframe1");349350// This should replace frame "iframe1" inside frame "sixth" ...351driver.findElement(By.id("submitButton")).click();352// driver should still be focused on frame "iframe1" inside frame "sixth" ...353assertThat(getTextOfGreetingElement()).isEqualTo("Success!");354// Make sure it was really frame "iframe1" inside frame "sixth" which was replaced ...355driver.switchTo().defaultContent().switchTo().frame("sixth").switchTo().frame("iframe1");356assertThat(driver.findElement(By.id("greeting")).getText()).isEqualTo("Success!");357}358359@Test360void testShouldBeAbleToFindElementsInIframesByXPath() {361driver.get(pages.iframePage);362363driver.switchTo().frame("iframe1");364365WebElement element = driver.findElement(By.xpath("//*[@id = 'changeme']"));366367assertThat(element).isNotNull();368}369370@Test371void testGetCurrentUrlReturnsTopLevelBrowsingContextUrl() {372driver.get(pages.framesetPage);373assertThat(driver.getCurrentUrl()).isEqualTo(pages.framesetPage);374375driver.switchTo().frame("second");376assertThat(driver.getCurrentUrl()).isEqualTo(pages.framesetPage);377}378379@Test380void testGetCurrentUrlReturnsTopLevelBrowsingContextUrlForIframes() {381driver.get(pages.iframePage);382assertThat(driver.getCurrentUrl()).isEqualTo(pages.iframePage);383384driver.switchTo().frame("iframe1");385assertThat(driver.getCurrentUrl()).isEqualTo(pages.iframePage);386}387388@Test389@Ignore(SAFARI)390public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUs() {391driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));392393driver.switchTo().frame("iframe1");394395WebElement killIframe = driver.findElement(By.id("killIframe"));396killIframe.click();397driver.switchTo().defaultContent();398399assertFrameNotPresent("iframe1");400401WebElement addIFrame = driver.findElement(By.id("addBackFrame"));402addIFrame.click();403wait.until(presenceOfElementLocated(By.id("iframe1")));404405driver.switchTo().frame("iframe1");406407wait.until(presenceOfElementLocated(By.id("success")));408}409410@Test411@Ignore(SAFARI)412public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithFrameIndex() {413driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));414int iframe = 0;415wait.until(frameToBeAvailableAndSwitchToIt(iframe));416// we should be in the frame now417WebElement killIframe = driver.findElement(By.id("killIframe"));418killIframe.click();419420driver.switchTo().defaultContent();421422WebElement addIFrame = driver.findElement(By.id("addBackFrame"));423addIFrame.click();424wait.until(frameToBeAvailableAndSwitchToIt(iframe));425426wait.until(presenceOfElementLocated(By.id("success")));427}428429@Test430@Ignore(SAFARI)431public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithWebElement() {432driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));433WebElement iframe = driver.findElement(By.id("iframe1"));434wait.until(frameToBeAvailableAndSwitchToIt(iframe));435// we should be in the frame now436WebElement killIframe = driver.findElement(By.id("killIframe"));437killIframe.click();438439driver.switchTo().defaultContent();440441WebElement addIFrame = driver.findElement(By.id("addBackFrame"));442addIFrame.click();443444iframe = driver.findElement(By.id("iframe1"));445wait.until(frameToBeAvailableAndSwitchToIt(iframe));446wait.until(presenceOfElementLocated(By.id("success")));447}448449@Test450@Ignore(SAFARI)451public void testShouldBeAbleToSwitchToFrameBySelector() {452driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));453wait.until(frameToBeAvailableAndSwitchToIt(By.id("iframe1")));454// we should be in the frame now455WebElement killIframe = driver.findElement(By.id("killIframe"));456killIframe.click();457458driver.switchTo().defaultContent();459460WebElement addIFrame = driver.findElement(By.id("addBackFrame"));461addIFrame.click();462463wait.until(frameToBeAvailableAndSwitchToIt(By.id("iframe1")));464wait.until(presenceOfElementLocated(By.id("success")));465}466467@Test468@NotYetImplemented(value = CHROME, reason = "Throws NoSuchElementException")469@NotYetImplemented(value = EDGE, reason = "Throws NoSuchElementException")470@Ignore(IE)471@Ignore(SAFARI)472public void testShouldNotBeAbleToDoAnythingTheFrameIsDeletedFromUnderUs() {473driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));474475driver.switchTo().frame("iframe1");476driver.findElement(By.id("killIframe")).click();477478assertThatExceptionOfType(NoSuchWindowException.class)479.isThrownBy(() -> driver.findElement(By.id("killIframe")));480}481482@Test483void testShouldReturnWindowTitleInAFrameset() {484driver.get(pages.framesetPage);485driver.switchTo().frame("third");486assertThat(driver.getTitle()).isEqualTo("Unique title");487}488489@Test490void testJavaScriptShouldExecuteInTheContextOfTheCurrentFrame() {491JavascriptExecutor executor = (JavascriptExecutor) driver;492493driver.get(pages.framesetPage);494assertThat((Boolean) executor.executeScript("return window == window.top")).isTrue();495driver.switchTo().frame("third");496assertThat((Boolean) executor.executeScript("return window != window.top")).isTrue();497}498499@Test500void testShouldNotSwitchMagicallyToTheTopWindow() {501String baseUrl = appServer.whereIs("frame_switching_tests/");502driver.get(baseUrl + "bug4876.html");503driver.switchTo().frame(0);504wait.until(presenceOfElementLocated(By.id("inputText")));505506for (int i = 0; i < 20; i++) {507try {508WebElement input = wait.until(presenceOfElementLocated(By.id("inputText")));509WebElement submit = wait.until(presenceOfElementLocated(By.id("submitButton")));510input.clear();511input.sendKeys("rand" + new Random().nextInt());512submit.click();513} finally {514String url =515(String) ((JavascriptExecutor) driver).executeScript("return window.location.href");516// IE6 and Chrome add "?"-symbol to the end of the URL517if (url.endsWith("?")) {518url = url.substring(0, url.length() - 1);519}520assertThat(url).isEqualTo(baseUrl + "bug4876_iframe.html");521}522}523}524525@Test526void testGetShouldSwitchToDefaultContext() {527driver.get(pages.iframePage);528driver.switchTo().frame(driver.findElement(By.id("iframe1")));529driver.findElement(By.id("cheese")); // Found on formPage.html but not on iframes.html.530531driver.get(pages.iframePage); // This must effectively switchTo().defaultContent(), too.532driver.findElement(By.id("iframe1"));533}534535private void assertFrameNotPresent(String locator) {536driver.switchTo().defaultContent();537wait.until(not(frameToBeAvailableAndSwitchToIt(locator)));538driver.switchTo().defaultContent();539}540}541542543