Path: blob/trunk/java/test/org/openqa/selenium/FrameSwitchingTest.java
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.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 {4041private Random random;4243@AfterEach44public void tearDown() {45try {46driver.switchTo().defaultContent();47} catch (Exception e) {48// May happen if the driver went away.49}50}5152// ----------------------------------------------------------------------------------------------53//54// Tests that WebDriver doesn't do anything fishy when it navigates to a page with frames.55//56// ----------------------------------------------------------------------------------------------57@Test58void testShouldAlwaysFocusOnTheTopMostFrameAfterANavigationEvent() {59driver.get(pages.framesetPage);60driver.findElement(By.tagName("frameset")); // Test passes if this does not throw.61}6263@Test64void testShouldNotAutomaticallySwitchFocusToAnIFrameWhenAPageContainingThemIsLoaded() {65driver.get(pages.iframePage);66driver.findElement(By.id("iframe_page_heading"));67}6869@Test70@Timeout(10)71public void testShouldOpenPageWithBrokenFrameset() {72driver.get(appServer.whereIs("framesetPage3.html"));7374WebElement frame1 = driver.findElement(By.id("first"));75driver.switchTo().frame(frame1);7677driver.switchTo().defaultContent();7879WebElement frame2 = driver.findElement(By.id("second"));8081try {82driver.switchTo().frame(frame2);83} catch (WebDriverException e) {84// IE9 can not switch to this broken frame - it has no window.85}86}8788// ----------------------------------------------------------------------------------------------89//90// Tests that WebDriver can switch to frames as expected.91//92// ----------------------------------------------------------------------------------------------93@Test94void testShouldBeAbleToSwitchToAFrameByItsIndex() {95driver.get(pages.framesetPage);96driver.switchTo().frame(1);9798assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");99}100101@Test102void testShouldBeAbleToSwitchToAnIframeByItsIndex() {103driver.get(pages.iframePage);104driver.switchTo().frame(0);105106assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");107}108109@Test110void testShouldBeAbleToSwitchToAFrameByItsName() {111driver.get(pages.framesetPage);112driver.switchTo().frame("fourth");113114assertThat(driver.findElement(By.tagName("frame")).getAttribute("name")).isEqualTo("child1");115}116117@Test118void testShouldBeAbleToSwitchToAnIframeByItsName() {119driver.get(pages.iframePage);120driver.switchTo().frame("iframe1-name");121122assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");123}124125@Test126void testShouldBeAbleToSwitchToAFrameByItsID() {127driver.get(pages.framesetPage);128driver.switchTo().frame("fifth");129assertThat(driver.findElement(By.name("windowOne")).getText()).isEqualTo("Open new window");130}131132@Test133void testShouldBeAbleToSwitchToAnIframeByItsID() {134driver.get(pages.iframePage);135driver.switchTo().frame("iframe1");136137assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");138}139140@Test141void testShouldBeAbleToSwitchToFrameWithNameContainingDot() {142driver.get(pages.framesetPage);143driver.switchTo().frame("sixth.iframe1");144assertThat(driver.findElement(By.tagName("body")).getText()).contains("Page number 3");145}146147@Test148void testShouldBeAbleToSwitchToAFrameUsingAPreviouslyLocatedWebElement() {149driver.get(pages.framesetPage);150WebElement frame = driver.findElement(By.tagName("frame"));151driver.switchTo().frame(frame);152153assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("1");154}155156@Test157void testShouldBeAbleToSwitchToAnIFrameUsingAPreviouslyLocatedWebElement() {158driver.get(pages.iframePage);159WebElement frame = driver.findElement(By.tagName("iframe"));160driver.switchTo().frame(frame);161162WebElement element = driver.findElement(By.name("id-name1"));163assertThat(element.getAttribute("value")).isEqualTo("name");164}165166@Test167void testShouldEnsureElementIsAFrameBeforeSwitching() {168driver.get(pages.framesetPage);169WebElement frame = driver.findElement(By.tagName("frameset"));170171assertThatExceptionOfType(NoSuchFrameException.class)172.isThrownBy(() -> driver.switchTo().frame(frame));173}174175@Test176void testFrameSearchesShouldBeRelativeToTheCurrentlySelectedFrame() {177driver.get(pages.framesetPage);178179driver.switchTo().frame("second");180assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");181182assertThatExceptionOfType(NoSuchFrameException.class)183.isThrownBy(() -> driver.switchTo().frame("third"));184185driver.switchTo().defaultContent();186driver.switchTo().frame("third");187188assertThatExceptionOfType(NoSuchFrameException.class)189.isThrownBy(() -> driver.switchTo().frame("second"));190191driver.switchTo().defaultContent();192driver.switchTo().frame("second");193assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");194}195196@Test197void testShouldSelectChildFramesByChainedCalls() {198driver.get(pages.framesetPage);199200driver.switchTo().frame("fourth").switchTo().frame("child2");201assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("11");202}203204@Test205void testShouldThrowFrameNotFoundExceptionLookingUpSubFramesWithSuperFrameNames() {206driver.get(pages.framesetPage);207driver.switchTo().frame("fourth");208209assertThatExceptionOfType(NoSuchFrameException.class)210.isThrownBy(() -> driver.switchTo().frame("second"));211}212213@Test214void testShouldThrowAnExceptionWhenAFrameCannotBeFound() {215driver.get(pages.xhtmlTestPage);216217assertThatExceptionOfType(NoSuchFrameException.class)218.isThrownBy(() -> driver.switchTo().frame("Nothing here"));219}220221@Test222void testShouldThrowAnExceptionWhenAFrameCannotBeFoundByIndex() {223driver.get(pages.xhtmlTestPage);224225assertThatExceptionOfType(NoSuchFrameException.class)226.isThrownBy(() -> driver.switchTo().frame(27));227}228229@Test230void testShouldBeAbleToSwitchToParentFrame() {231driver.get(pages.framesetPage);232233driver.switchTo().frame("fourth").switchTo().parentFrame().switchTo().frame("first");234assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("1");235}236237@Test238@NotYetImplemented(SAFARI)239public void testShouldBeAbleToSwitchToParentFrameFromASecondLevelFrame() {240driver.get(pages.framesetPage);241242driver243.switchTo()244.frame("fourth")245.switchTo()246.frame("child1")247.switchTo()248.parentFrame()249.switchTo()250.frame("child2");251assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("11");252}253254@Test255void testSwitchingToParentFrameFromDefaultContextIsNoOp() {256driver.get(pages.xhtmlTestPage);257driver.switchTo().parentFrame();258assertThat(driver.getTitle()).isEqualTo("XHTML Test Page");259}260261@Test262void testShouldBeAbleToSwitchToParentFromAnIframe() {263driver.get(pages.iframePage);264driver.switchTo().frame(0);265266driver.switchTo().parentFrame();267driver.findElement(By.id("iframe_page_heading"));268}269270// ----------------------------------------------------------------------------------------------271//272// General frame handling behavior tests273//274// ----------------------------------------------------------------------------------------------275276@Test277void testShouldContinueToReferToTheSameFrameOnceItHasBeenSelected() {278driver.get(pages.framesetPage);279280driver.switchTo().frame(2);281WebElement checkbox = driver.findElement(By.xpath("//input[@name='checky']"));282checkbox.click();283checkbox.submit();284285wait.until(textToBe(By.xpath("//p"), "Success!"));286}287288@Test289@NotYetImplemented(SAFARI)290public void testShouldFocusOnTheReplacementWhenAFrameFollowsALinkToA_TopTargetedPage() {291driver.get(pages.framesetPage);292293driver.switchTo().frame(0);294driver.findElement(By.linkText("top")).click();295296String expectedTitle = "XHTML Test Page";297298wait.until(titleIs(expectedTitle));299}300301@Test302void testShouldAllowAUserToSwitchFromAnIframeBackToTheMainContentOfThePage() {303driver.get(pages.iframePage);304driver.switchTo().frame(0);305306driver.switchTo().defaultContent();307driver.findElement(By.id("iframe_page_heading"));308}309310@Test311void testShouldAllowTheUserToSwitchToAnIFrameAndRemainFocusedOnIt() {312driver.get(pages.iframePage);313driver.switchTo().frame(0);314315driver.findElement(By.id("submitButton")).click();316317assertThat(getTextOfGreetingElement()).isEqualTo("Success!");318}319320public String getTextOfGreetingElement() {321return wait.until(presenceOfElementLocated(By.id("greeting"))).getText();322}323324@Test325void testShouldBeAbleToClickInAFrame() {326driver.get(pages.framesetPage);327driver.switchTo().frame("third");328329// This should replace frame "third" ...330driver.findElement(By.id("submitButton")).click();331// driver should still be focused on frame "third" ...332assertThat(getTextOfGreetingElement()).isEqualTo("Success!");333// Make sure it was really frame "third" which was replaced ...334driver.switchTo().defaultContent().switchTo().frame("third");335assertThat(getTextOfGreetingElement()).isEqualTo("Success!");336}337338@Test339void testShouldBeAbleToClickInAFrameThatRewritesTopWindowLocation() {340driver.get(appServer.whereIs("click_tests/issue5237.html"));341driver.switchTo().frame("search");342driver.findElement(By.id("submit")).click();343driver.switchTo().defaultContent();344wait.until(titleIs("Target page for issue 5237"));345}346347@Test348void testShouldBeAbleToClickInASubFrame() {349driver.get(pages.framesetPage);350driver.switchTo().frame("sixth").switchTo().frame("iframe1");351352// This should replace frame "iframe1" inside frame "sixth" ...353driver.findElement(By.id("submitButton")).click();354// driver should still be focused on frame "iframe1" inside frame "sixth" ...355assertThat(getTextOfGreetingElement()).isEqualTo("Success!");356// Make sure it was really frame "iframe1" inside frame "sixth" which was replaced ...357driver.switchTo().defaultContent().switchTo().frame("sixth").switchTo().frame("iframe1");358assertThat(driver.findElement(By.id("greeting")).getText()).isEqualTo("Success!");359}360361@Test362void testShouldBeAbleToFindElementsInIframesByXPath() {363driver.get(pages.iframePage);364365driver.switchTo().frame("iframe1");366367WebElement element = driver.findElement(By.xpath("//*[@id = 'changeme']"));368369assertThat(element).isNotNull();370}371372@Test373void testGetCurrentUrlReturnsTopLevelBrowsingContextUrl() {374driver.get(pages.framesetPage);375assertThat(driver.getCurrentUrl()).isEqualTo(pages.framesetPage);376377driver.switchTo().frame("second");378assertThat(driver.getCurrentUrl()).isEqualTo(pages.framesetPage);379}380381@Test382void testGetCurrentUrlReturnsTopLevelBrowsingContextUrlForIframes() {383driver.get(pages.iframePage);384assertThat(driver.getCurrentUrl()).isEqualTo(pages.iframePage);385386driver.switchTo().frame("iframe1");387assertThat(driver.getCurrentUrl()).isEqualTo(pages.iframePage);388}389390@Test391@Ignore(SAFARI)392public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUs() {393driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));394395driver.switchTo().frame("iframe1");396397WebElement killIframe = driver.findElement(By.id("killIframe"));398killIframe.click();399driver.switchTo().defaultContent();400401assertFrameNotPresent("iframe1");402403WebElement addIFrame = driver.findElement(By.id("addBackFrame"));404addIFrame.click();405wait.until(presenceOfElementLocated(By.id("iframe1")));406407driver.switchTo().frame("iframe1");408409wait.until(presenceOfElementLocated(By.id("success")));410}411412@Test413@Ignore(SAFARI)414public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithFrameIndex() {415driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));416int iframe = 0;417wait.until(frameToBeAvailableAndSwitchToIt(iframe));418// we should be in the frame now419WebElement killIframe = driver.findElement(By.id("killIframe"));420killIframe.click();421422driver.switchTo().defaultContent();423424WebElement addIFrame = driver.findElement(By.id("addBackFrame"));425addIFrame.click();426wait.until(frameToBeAvailableAndSwitchToIt(iframe));427428wait.until(presenceOfElementLocated(By.id("success")));429}430431@Test432@Ignore(SAFARI)433public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithWebElement() {434driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));435WebElement iframe = driver.findElement(By.id("iframe1"));436wait.until(frameToBeAvailableAndSwitchToIt(iframe));437// we should be in the frame now438WebElement killIframe = driver.findElement(By.id("killIframe"));439killIframe.click();440441driver.switchTo().defaultContent();442443WebElement addIFrame = driver.findElement(By.id("addBackFrame"));444addIFrame.click();445446iframe = driver.findElement(By.id("iframe1"));447wait.until(frameToBeAvailableAndSwitchToIt(iframe));448wait.until(presenceOfElementLocated(By.id("success")));449}450451@Test452@NotYetImplemented(value = CHROME, reason = "Throws NoSuchElementException")453@NotYetImplemented(value = EDGE, reason = "Throws NoSuchElementException")454@Ignore(IE)455@Ignore(SAFARI)456public void testShouldNotBeAbleToDoAnythingTheFrameIsDeletedFromUnderUs() {457driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));458459driver.switchTo().frame("iframe1");460driver.findElement(By.id("killIframe")).click();461462assertThatExceptionOfType(NoSuchWindowException.class)463.isThrownBy(() -> driver.findElement(By.id("killIframe")));464}465466@Test467void testShouldReturnWindowTitleInAFrameset() {468driver.get(pages.framesetPage);469driver.switchTo().frame("third");470assertThat(driver.getTitle()).isEqualTo("Unique title");471}472473@Test474void testJavaScriptShouldExecuteInTheContextOfTheCurrentFrame() {475JavascriptExecutor executor = (JavascriptExecutor) driver;476477driver.get(pages.framesetPage);478assertThat((Boolean) executor.executeScript("return window == window.top")).isTrue();479driver.switchTo().frame("third");480assertThat((Boolean) executor.executeScript("return window != window.top")).isTrue();481}482483@Test484void testShouldNotSwitchMagicallyToTheTopWindow() {485String baseUrl = appServer.whereIs("frame_switching_tests/");486driver.get(baseUrl + "bug4876.html");487driver.switchTo().frame(0);488wait.until(presenceOfElementLocated(By.id("inputText")));489490for (int i = 0; i < 20; i++) {491try {492WebElement input = wait.until(presenceOfElementLocated(By.id("inputText")));493WebElement submit = wait.until(presenceOfElementLocated(By.id("submitButton")));494input.clear();495random = new Random();496input.sendKeys("rand" + random.nextInt());497submit.click();498} finally {499String url =500(String) ((JavascriptExecutor) driver).executeScript("return window.location.href");501// IE6 and Chrome add "?"-symbol to the end of the URL502if (url.endsWith("?")) {503url = url.substring(0, url.length() - 1);504}505assertThat(url).isEqualTo(baseUrl + "bug4876_iframe.html");506}507}508}509510@Test511void testGetShouldSwitchToDefaultContext() {512driver.get(pages.iframePage);513driver.switchTo().frame(driver.findElement(By.id("iframe1")));514driver.findElement(By.id("cheese")); // Found on formPage.html but not on iframes.html.515516driver.get(pages.iframePage); // This must effectively switchTo().defaultContent(), too.517driver.findElement(By.id("iframe1"));518}519520private void assertFrameNotPresent(String locator) {521driver.switchTo().defaultContent();522wait.until(not(frameToBeAvailableAndSwitchToIt(locator)));523driver.switchTo().defaultContent();524}525}526527528