org.openqa.selenium.NoSuchFrameException Java Examples

The following examples show how to use org.openqa.selenium.NoSuchFrameException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RemoteWebDriverUnitTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void canHandleSwitchToNonExistingFrameCommand() throws IOException {
  CommandExecutor executor = prepareExecutorMock(
      echoCapabilities, valueResponder(EMPTY_LIST));

  RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
  assertThatExceptionOfType(NoSuchFrameException.class)
      .isThrownBy(() -> driver.switchTo().frame("frameName"));

  verifyCommands(
      executor, driver.getSessionId(),
      new CommandPayload(DriverCommand.FIND_ELEMENTS, ImmutableMap.of(
          "using", "css selector", "value", "frame[name='frameName'],iframe[name='frameName']")),
      new CommandPayload(DriverCommand.FIND_ELEMENTS, ImmutableMap.of(
          "using", "css selector", "value", "frame#frameName,iframe#frameName")));
}
 
Example #2
Source File: ErrorHandlerTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void testThrowsCorrectExceptionTypes() {
  assertThrowsCorrectExceptionType(ErrorCodes.NO_SUCH_WINDOW, NoSuchWindowException.class);
  assertThrowsCorrectExceptionType(ErrorCodes.NO_SUCH_FRAME, NoSuchFrameException.class);
  assertThrowsCorrectExceptionType(ErrorCodes.NO_SUCH_ELEMENT, NoSuchElementException.class);
  assertThrowsCorrectExceptionType(
      ErrorCodes.UNKNOWN_COMMAND, UnsupportedCommandException.class);
  assertThrowsCorrectExceptionType(
      ErrorCodes.METHOD_NOT_ALLOWED, UnsupportedCommandException.class);
  assertThrowsCorrectExceptionType(
      ErrorCodes.STALE_ELEMENT_REFERENCE, StaleElementReferenceException.class);
  assertThrowsCorrectExceptionType(
      ErrorCodes.ELEMENT_NOT_VISIBLE, ElementNotVisibleException.class);
  assertThrowsCorrectExceptionType(
      ErrorCodes.INVALID_ELEMENT_STATE, InvalidElementStateException.class);
  assertThrowsCorrectExceptionType(
      ErrorCodes.XPATH_LOOKUP_ERROR, InvalidSelectorException.class);
  assertThrowsCorrectExceptionType(ErrorCodes.INVALID_ELEMENT_COORDINATES,
      InvalidCoordinatesException.class);
}
 
Example #3
Source File: FluentWaitTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void propagatesUnIgnoredExceptions() {
  final NoSuchWindowException exception = new NoSuchWindowException("");

  when(mockClock.instant()).thenReturn(EPOCH);
  when(mockCondition.apply(mockDriver)).thenThrow(exception);

  Wait<WebDriver> wait = new FluentWait<>(mockDriver, mockClock, mockSleeper)
    .withTimeout(Duration.ofMillis(0))
    .pollingEvery(Duration.ofSeconds(2))
    .ignoring(NoSuchElementException.class, NoSuchFrameException.class);

  assertThatExceptionOfType(NoSuchWindowException.class)
      .isThrownBy(() -> wait.until(mockCondition))
      .satisfies(expected -> assertThat(expected).isSameAs(exception));
}
 
Example #4
Source File: FluentWaitTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void canIgnoreMultipleExceptions() throws InterruptedException {
  when(mockClock.instant()).thenReturn(EPOCH);
  when(mockCondition.apply(mockDriver))
    .thenThrow(new NoSuchElementException(""))
    .thenThrow(new NoSuchFrameException(""))
    .thenReturn(ARBITRARY_VALUE);

  Wait<WebDriver> wait = new FluentWait<>(mockDriver, mockClock, mockSleeper)
    .withTimeout(Duration.ofMillis(0))
    .pollingEvery(Duration.ofSeconds(2))
    .ignoring(NoSuchElementException.class, NoSuchFrameException.class);

  assertThat(wait.until(mockCondition)).isEqualTo(ARBITRARY_VALUE);

  verify(mockSleeper, times(2)).sleep(Duration.ofSeconds(2));
}
 
Example #5
Source File: ExpectedConditions.java    From selenium with Apache License 2.0 6 votes vote down vote up
/**
 * An expectation for checking whether the given frame is available to switch to. <p> If the frame
 * is available it switches the given driver to the specified webelement.
 *
 * @param frameLocator used to find the frame (webelement)
 * @return WebDriver instance after frame has been switched
 */
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(
  final WebElement frameLocator) {
  return new ExpectedCondition<WebDriver>() {
    @Override
    public WebDriver apply(WebDriver driver) {
      try {
        return driver.switchTo().frame(frameLocator);
      } catch (NoSuchFrameException e) {
        return null;
      }
    }

    @Override
    public String toString() {
      return "frame to be available: " + frameLocator;
    }
  };
}
 
Example #6
Source File: ExpectedConditions.java    From selenium with Apache License 2.0 6 votes vote down vote up
/**
 * An expectation for checking whether the given frame is available to switch to. <p> If the frame
 * is available it switches the given driver to the specified frameIndex.
 *
 * @param frameLocator used to find the frame (index)
 * @return WebDriver instance after frame has been switched
 */
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(
  final int frameLocator) {
  return new ExpectedCondition<WebDriver>() {
    @Override
    public WebDriver apply(WebDriver driver) {
      try {
        return driver.switchTo().frame(frameLocator);
      } catch (NoSuchFrameException e) {
        return null;
      }
    }

    @Override
    public String toString() {
      return "frame to be available: " + frameLocator;
    }
  };
}
 
Example #7
Source File: ExpectedConditions.java    From selenium with Apache License 2.0 6 votes vote down vote up
/**
 * An expectation for checking whether the given frame is available to switch to. <p> If the frame
 * is available it switches the given driver to the specified frame.
 *
 * @param locator used to find the frame
 * @return WebDriver instance after frame has been switched
 */
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(final By locator) {
  return new ExpectedCondition<WebDriver>() {
    @Override
    public WebDriver apply(WebDriver driver) {
      try {
        return driver.switchTo().frame(driver.findElement(locator));
      } catch (NoSuchFrameException e) {
        return null;
      }
    }

    @Override
    public String toString() {
      return "frame to be available: " + locator;
    }
  };
}
 
Example #8
Source File: ExpectedConditions.java    From selenium with Apache License 2.0 6 votes vote down vote up
/**
 * An expectation for checking whether the given frame is available to switch to. <p> If the frame
 * is available it switches the given driver to the specified frame.
 *
 * @param frameLocator used to find the frame (id or name)
 * @return WebDriver instance after frame has been switched
 */
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(
  final String frameLocator) {
  return new ExpectedCondition<WebDriver>() {
    @Override
    public WebDriver apply(WebDriver driver) {
      try {
        return driver.switchTo().frame(frameLocator);
      } catch (NoSuchFrameException e) {
        return null;
      }
    }

    @Override
    public String toString() {
      return "frame to be available: " + frameLocator;
    }
  };
}
 
Example #9
Source File: WebDriverBrowser.java    From darcy-webdriver with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Wrapper for interacting with a targeted driver that may or may not actually be present.
 * Returns a result.
 */
private <T> T attemptAndGet(Supplier<T> action) {
    try {
        return action.get();
    } catch (NoSuchFrameException | NoSuchWindowException | NoSuchSessionException e) {
        throw new FindableNotPresentException(this, e);
    }
}
 
Example #10
Source File: WebDriverWaitTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void shouldSilentlyCaptureNoSuchFrameExceptions() {
  final ExpectedCondition<WebElement> condition = mock(ExpectedCondition.class);
  when(condition.apply(mockDriver))
      .thenThrow(new NoSuchFrameException("foo"))
      .thenReturn(mockElement);

  TickingClock clock = new TickingClock();
  Wait<WebDriver> wait =
      new WebDriverWait(mockDriver, Duration.ofSeconds(5), Duration.ofMillis(500), clock, clock);
  wait.until(condition);
}
 
Example #11
Source File: FluentWaitTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWaitUntilABooleanResultIsTrue() throws InterruptedException {
  when(mockClock.instant()).thenReturn(EPOCH);
  when(mockCondition.apply(mockDriver)).thenReturn(false, false, true);

  Wait<WebDriver> wait = new FluentWait<>(mockDriver, mockClock, mockSleeper)
    .withTimeout(Duration.ofMillis(0))
    .pollingEvery(Duration.ofSeconds(2))
      .ignoring(NoSuchElementException.class, NoSuchFrameException.class);

  assertThat(wait.until(mockCondition)).isEqualTo(true);

  verify(mockSleeper, times(2)).sleep(Duration.ofSeconds(2));
}
 
Example #12
Source File: FluentWaitTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldWaitUntilReturnValueOfConditionIsNotNull() throws InterruptedException {
  when(mockClock.instant()).thenReturn(EPOCH);
  when(mockCondition.apply(mockDriver)).thenReturn(null, ARBITRARY_VALUE);

  Wait<WebDriver> wait = new FluentWait<>(mockDriver, mockClock, mockSleeper)
    .withTimeout(Duration.ofMillis(0))
    .pollingEvery(Duration.ofSeconds(2))
    .ignoring(NoSuchElementException.class, NoSuchFrameException.class);

  assertThat(wait.until(mockCondition)).isEqualTo(ARBITRARY_VALUE);
  verify(mockSleeper, times(1)).sleep(Duration.ofSeconds(2));
}
 
Example #13
Source File: RemoteWebDriver.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public WebDriver frame(String frameName) {
  String name = frameName.replaceAll("(['\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-/\\[\\]\\(\\)])", "\\\\$1");
  List<WebElement> frameElements = RemoteWebDriver.this.findElements(
      By.cssSelector("frame[name='" + name + "'],iframe[name='" + name + "']"));
  if (frameElements.size() == 0) {
    frameElements = RemoteWebDriver.this.findElements(
        By.cssSelector("frame#" + name + ",iframe#" + name));
  }
  if (frameElements.size() == 0) {
    throw new NoSuchFrameException("No frame element found by name or id " + frameName);
  }
  return frame(frameElements.get(0));
}
 
Example #14
Source File: AlertHandlingPageLoadListener.java    From vividus with Apache License 2.0 5 votes vote down vote up
private void selectOptionForAlertHandling(WebDriver driver)
{
    try
    {
        alertHandlingOptions.selectOptionForAlertHandling((JavascriptExecutor) driver);
    }
    catch (NoSuchFrameException e)
    {
        // SafariDriver do not process frame closure itself and throw NoSuchFrameException -
        // https://github.com/SeleniumHQ/selenium/issues/3314
        LOGGER.warn("Swallowing exception quietly (browser frame may have been closed)", e);
    }
}
 
Example #15
Source File: WebDriverBrowser.java    From darcy-webdriver with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Wrapper for interacting with a targeted driver that may or may not actually be present.
 */
private void attempt(Runnable action) {
    try {
        action.run();
    } catch (NoSuchFrameException | NoSuchWindowException | NoSuchSessionException e) {
        throw new FindableNotPresentException(this, e);
    }
}
 
Example #16
Source File: WebDriverBrowserTest.java    From webtester-core with Apache License 2.0 5 votes vote down vote up
@Test(expected = NoSuchFrameException.class)
public void testExceptionHandlingInCaseAFrameIsNotFoundForTheGivenNameOrId() {

    TargetLocator locator = mockTargetLocator();
    NoSuchFrameException exception = createSeleniumExceptionOfClass(NoSuchFrameException.class);
    doThrow(exception).when(locator).frame(NAME_OR_ID);

    try {
        cut.setFocusOnFrame(NAME_OR_ID);
    } finally {
        verifyLastEventFiredWasExceptionEventOf(exception);
    }

}
 
Example #17
Source File: WebDriverBrowserTest.java    From webtester-core with Apache License 2.0 5 votes vote down vote up
@Test(expected = NoSuchFrameException.class)
public void testExceptionHandlingInCaseAFrameIsNotFoundForTheGivenIndex() {

    TargetLocator locator = mockTargetLocator();
    NoSuchFrameException exception = createSeleniumExceptionOfClass(NoSuchFrameException.class);
    doThrow(exception).when(locator).frame(INDEX);

    try {
        cut.setFocusOnFrame(INDEX);
    } finally {
        verifyLastEventFiredWasExceptionEventOf(exception);
    }

}
 
Example #18
Source File: WaitHelper.java    From SeleniumCucumber with GNU General Public License v3.0 5 votes vote down vote up
private WebDriverWait getWait(int timeOutInSeconds,int pollingEveryInMiliSec) {
	oLog.debug("");
	WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
	wait.pollingEvery(pollingEveryInMiliSec, TimeUnit.MILLISECONDS);
	wait.ignoring(NoSuchElementException.class);
	wait.ignoring(ElementNotVisibleException.class);
	wait.ignoring(StaleElementReferenceException.class);
	wait.ignoring(NoSuchFrameException.class);
	return wait;
}
 
Example #19
Source File: FocusSetterTest.java    From webtester2-core with Apache License 2.0 5 votes vote down vote up
@Test
void switchingToFrameByNonFrameFragmentThrowsException() {
    PageFragment fragment = MockFactory.fragment().withName("The Name").build();
    NoSuchFrameException exception = mock(NoSuchFrameException.class);
    when(webDriver.switchTo().frame(fragment.webElement())).thenThrow(exception);
    assertThrows(NoSuchFrameException.class, () -> {
        cut.onFrame(fragment);
    });
    verify(events).fireExceptionEvent(exception);
    verifyNoMoreInteractions(events);
}
 
Example #20
Source File: FocusSetterTest.java    From webtester2-core with Apache License 2.0 5 votes vote down vote up
@Test
void switchingToFrameByUnknownNameOrIdThrowsException() {
    NoSuchFrameException exception = mock(NoSuchFrameException.class);
    when(webDriver.switchTo().frame("fooBar")).thenThrow(exception);
    assertThrows(NoSuchFrameException.class, () -> {
        cut.onFrame("fooBar");
    });
    verify(events).fireExceptionEvent(exception);
    verifyNoMoreInteractions(events);
}
 
Example #21
Source File: FocusSetterTest.java    From webtester2-core with Apache License 2.0 5 votes vote down vote up
@Test
void switchingToFrameByUnknownIndexThrowsException() {
    NoSuchFrameException exception = mock(NoSuchFrameException.class);
    when(webDriver.switchTo().frame(42)).thenThrow(exception);
    assertThrows(NoSuchFrameException.class, () -> {
        cut.onFrame(42);
    });
    verify(events).fireExceptionEvent(exception);
    verifyNoMoreInteractions(events);
}
 
Example #22
Source File: AlertHandlingPageLoadListenerTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testAfterClickOnWebDriverException()
{
    pageLoadListenerForAlertHanding.setAlertHandlingOptions(AlertHandlingOptions.ACCEPT);
    doThrow(NoSuchFrameException.class).when((JavascriptExecutor) webDriver).executeScript(ALERT_SCRIPT, true);
    pageLoadListenerForAlertHanding.afterClickOn(webElement, webDriver);
}
 
Example #23
Source File: ErrorHandlerTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
@Test
public void testStatusCodesRaisedBackToStatusMatches() {
  Map<Integer, Class<?>> exceptions = new HashMap<>();
  exceptions.put(ErrorCodes.NO_SUCH_SESSION, NoSuchSessionException.class);
  exceptions.put(ErrorCodes.NO_SUCH_ELEMENT, NoSuchElementException.class);
  exceptions.put(ErrorCodes.NO_SUCH_FRAME, NoSuchFrameException.class);
  exceptions.put(ErrorCodes.UNKNOWN_COMMAND, UnsupportedCommandException.class);
  exceptions.put(ErrorCodes.STALE_ELEMENT_REFERENCE, StaleElementReferenceException.class);
  exceptions.put(ErrorCodes.ELEMENT_NOT_VISIBLE, ElementNotVisibleException.class);
  exceptions.put(ErrorCodes.INVALID_ELEMENT_STATE, InvalidElementStateException.class);
  exceptions.put(ErrorCodes.UNHANDLED_ERROR, WebDriverException.class);
  exceptions.put(ErrorCodes.ELEMENT_NOT_SELECTABLE, ElementNotSelectableException.class);
  exceptions.put(ErrorCodes.JAVASCRIPT_ERROR, JavascriptException.class);
  exceptions.put(ErrorCodes.XPATH_LOOKUP_ERROR, InvalidSelectorException.class);
  exceptions.put(ErrorCodes.TIMEOUT, TimeoutException.class);
  exceptions.put(ErrorCodes.NO_SUCH_WINDOW, NoSuchWindowException.class);
  exceptions.put(ErrorCodes.INVALID_COOKIE_DOMAIN, InvalidCookieDomainException.class);
  exceptions.put(ErrorCodes.UNABLE_TO_SET_COOKIE, UnableToSetCookieException.class);
  exceptions.put(ErrorCodes.UNEXPECTED_ALERT_PRESENT, UnhandledAlertException.class);
  exceptions.put(ErrorCodes.NO_ALERT_PRESENT, NoAlertPresentException.class);
  exceptions.put(ErrorCodes.ASYNC_SCRIPT_TIMEOUT, ScriptTimeoutException.class);
  exceptions.put(ErrorCodes.INVALID_ELEMENT_COORDINATES, InvalidCoordinatesException.class);
  exceptions.put(ErrorCodes.IME_NOT_AVAILABLE, ImeNotAvailableException.class);
  exceptions.put(ErrorCodes.IME_ENGINE_ACTIVATION_FAILED, ImeActivationFailedException.class);
  exceptions.put(ErrorCodes.INVALID_SELECTOR_ERROR, InvalidSelectorException.class);
  exceptions.put(ErrorCodes.SESSION_NOT_CREATED, SessionNotCreatedException.class);
  exceptions.put(ErrorCodes.MOVE_TARGET_OUT_OF_BOUNDS, MoveTargetOutOfBoundsException.class);
  exceptions.put(ErrorCodes.INVALID_XPATH_SELECTOR, InvalidSelectorException.class);
  exceptions.put(ErrorCodes.INVALID_XPATH_SELECTOR_RETURN_TYPER, InvalidSelectorException.class);

  for (Map.Entry<Integer, Class<?>> exception : exceptions.entrySet()) {
    assertThatExceptionOfType(WebDriverException.class)
        .isThrownBy(() -> handler.throwIfResponseFailed(createResponse(exception.getKey()), 123))
        .satisfies(e -> {
          assertThat(e.getClass().getSimpleName()).isEqualTo(exception.getValue().getSimpleName());

          // all of the special invalid selector exceptions are just mapped to the generic invalid selector
          int expected = e instanceof InvalidSelectorException
                         ? ErrorCodes.INVALID_SELECTOR_ERROR : exception.getKey();
          assertThat(new ErrorCodes().toStatusCode(e)).isEqualTo(expected);
        });
  }
}
 
Example #24
Source File: WebDriverUtils.java    From rice with Educational Community License v2.0 3 votes vote down vote up
/**
 * <p>
 * Select frame defined by locator without throwing an Exception if it doesn't exist.
 * </p>
 *
 * @param driver to select frame on
 * @param locator to identify frame to select
 */
public static void selectFrameSafe(WebDriver driver, String locator) {
    try {
        driver.switchTo().frame(locator);
    } catch (NoSuchFrameException nsfe) {
        // don't fail
    }
}
 
Example #25
Source File: FocusSetter.java    From webtester2-core with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the browser's focus to the frame of the given {@link PageFragment page fragment}.
 *
 * @param frame the page fragment representing the frame to focus on
 * @throws NoSuchFrameException in case there is no frame with the given name or ID
 * @see WebDriver.TargetLocator#frame(String)
 * @since 2.0
 */
public void onFrame(PageFragment frame) throws NoSuchFrameException {
    ActionTemplate.browser(browser())
        .execute(browser -> doOnFrame(browser, frame))
        .fireEvent(browser -> new SwitchedToFrameEvent(frame));
    log.debug("focused on frame page fragment: {}", frame);
}
 
Example #26
Source File: FocusSetter.java    From webtester2-core with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the browser's focus to the frame with the given name or ID.
 *
 * @param nameOrId the name or ID of the frame to focus on
 * @throws NoSuchFrameException in case there is no frame with the given name or ID
 * @see WebDriver.TargetLocator#frame(String)
 * @since 2.0
 */
public void onFrame(String nameOrId) throws NoSuchFrameException {
    ActionTemplate.browser(browser())
        .execute(browser -> doOnFrame(browser, nameOrId))
        .fireEvent(browser -> new SwitchedToFrameEvent(nameOrId));
    log.debug("focused on frame with name or ID: {}", nameOrId);
}
 
Example #27
Source File: FocusSetter.java    From webtester2-core with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the browser's focus to the frame with the given index.
 *
 * @param index the index of the frame to focus on
 * @throws NoSuchFrameException in case there is no frame with the given index
 * @see WebDriver.TargetLocator#frame(int)
 * @since 2.0
 */
public void onFrame(int index) throws NoSuchFrameException {
    ActionTemplate.browser(browser())
        .execute(browser -> doOnFrame(browser, index))
        .fireEvent(browser -> new SwitchedToFrameEvent(index));
    log.debug("focused on frame with index: {}", index);
}