public HiddenHtmlPrompt( UiDriver uiDriver ) { super(uiDriver); HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver; HtmlUnitDriver driver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name()); Field webClientField = null; boolean fieldAccessibleState = false; try { TargetLocator targetLocator = driver.switchTo(); webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient"); fieldAccessibleState = webClientField.isAccessible(); webClientField.setAccessible(true); webClient = (WebClient) webClientField.get(targetLocator.defaultContent()); } catch (Exception e) { throw new SeleniumOperationException("Error retrieving internal Selenium web client", e); } finally { if (webClientField != null) { webClientField.setAccessible(fieldAccessibleState); } } }
public void windowTitleWithPercentage() throws Throwable { driver = new JavaDriver(); SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { frame.setTitle("My %Dialog%"); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); WebElement element1 = driver.findElement(By.name("click-me")); String id1 = ((RemoteWebElement) element1).getId(); // driver.switchTo().window("My %25Dialog%25"); TargetLocator switchTo = driver.switchTo(); switchTo.window("My %Dialog%"); WebElement element2 = driver.findElement(By.name("click-me")); String id2 = ((RemoteWebElement) element2).getId(); AssertJUnit.assertEquals(id1, id2); }
public HiddenHtmlAlert( UiDriver uiDriver ) { super(uiDriver); HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver; HtmlUnitDriver driver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name()); Field webClientField = null; boolean fieldAccessibleState = false; try { TargetLocator targetLocator = driver.switchTo(); webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient"); fieldAccessibleState = webClientField.isAccessible(); webClientField.setAccessible(true); webClient = (WebClient) webClientField.get(targetLocator.defaultContent()); } catch (Exception e) { throw new SeleniumOperationException("Error retrieving internal Selenium web client", e); } finally { if (webClientField != null) { webClientField.setAccessible(fieldAccessibleState); } } }
public HiddenHtmlConfirm( UiDriver uiDriver ) { super(uiDriver); HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver; HtmlUnitDriver driver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name()); Field webClientField = null; boolean fieldAccessibleState = false; try { TargetLocator targetLocator = driver.switchTo(); webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient"); fieldAccessibleState = webClientField.isAccessible(); webClientField.setAccessible(true); webClient = (WebClient) webClientField.get(targetLocator.defaultContent()); } catch (Exception e) { throw new SeleniumOperationException("Error retrieving internal Selenium web client", e); } finally { if (webClientField != null) { webClientField.setAccessible(fieldAccessibleState); } } }
/** * {@inheritDoc} */ @Override protected Object onCommand(Context context) { TargetLocator tl = getSession().switchTo(); if (index >= 0) { tl.frame(index); } if (StringUtils.isNotBlank(name)) { tl.frame(name); } if (top != null && top) { tl.defaultContent(); } return null; }
@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); } }
@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); } }
@Test(expected = NoSuchWindowException.class) public void testExceptionHandlingInCaseAWindowIsNotFoundForTheGivenNameOrHandle() { TargetLocator locator = mockTargetLocator(); NoSuchWindowException exception = createSeleniumExceptionOfClass(NoSuchWindowException.class); doThrow(exception).when(locator).window(NAME_OR_HANDLE); try { cut.setFocusOnWindow(NAME_OR_HANDLE); } finally { verifyLastEventFiredWasExceptionEventOf(exception); } }
@Override public WebDriver switchTo(TargetLocator targetLocator) { // This is ugly :( if (targetLocator instanceof CachingTargetLocator) { return ((CachingTargetLocator) targetLocator).frame(parent, index); } parent.switchTo(targetLocator); return targetLocator.frame(index); }
@Override public WebDriver switchTo(TargetLocator targetLocator) { // This is ugly :( if (targetLocator instanceof CachingTargetLocator) { return ((CachingTargetLocator) targetLocator).frame(parent, nameOrId); } parent.switchTo(targetLocator); return targetLocator.frame(nameOrId); }
@Override public WebDriver switchTo(TargetLocator targetLocator) { // This is ugly :( if (targetLocator instanceof CachingTargetLocator) { return ((CachingTargetLocator) targetLocator).frame(parent, frameElement); } parent.switchTo(targetLocator); return targetLocator.frame(frameElement); }
@Override public WebDriver switchTo(TargetLocator targetLocator) { if (windowHandle == null) { windowHandle = findWindow(targetLocator); } return targetLocator.window(windowHandle); }
private String findWindow(TargetLocator targetLocator) { for (String windowHandle : targetLocator.defaultContent().getWindowHandles()) { Browser forWindowHandle = By.id(windowHandle).find(Browser.class, parentContext); view.setContext(forWindowHandle); if (view.isLoaded()) { return windowHandle; } } throw new NoSuchWindowException("No window in driver found which has " + view + " " + "currently loaded."); }
private String findWindow(TargetLocator targetLocator) { for (String windowHandle : targetLocator.defaultContent().getWindowHandles()) { if (targetLocator.window(windowHandle).getTitle().equals(title)) { return windowHandle; } } throw new NoSuchWindowException("No window in driver found which has title: " + title); }
private String findWindow(TargetLocator targetLocator) { for (String windowHandle : targetLocator.defaultContent().getWindowHandles()) { if (urlMatcher.matches(targetLocator.window(windowHandle).getCurrentUrl())) { return windowHandle; } } throw new NoSuchWindowException("No window in driver found which has url matching: " + urlMatcher); }
public TargetLocator switchTo() { return driver.switchTo(); }
@Test public void testThatSwitchingToFrameWithIndexInvokesCorrectLocatorMethod() { TargetLocator locator = mockTargetLocator(); cut.setFocusOnFrame(INDEX); verify(locator).frame(INDEX); }
@Test public void testThatSwitchingToFrameWithNameOrIdInvokesCorrectLocatorMethod() { TargetLocator locator = mockTargetLocator(); cut.setFocusOnFrame(NAME_OR_ID); verify(locator).frame(NAME_OR_ID); }
@Test public void testThatSwitchingToWindowWithNameOrHanldeInvokesCorrectLocatorMethod() { TargetLocator locator = mockTargetLocator(); cut.setFocusOnWindow(NAME_OR_HANDLE); verify(locator).window(NAME_OR_HANDLE); }
@Test public void testThatSwitchingToDefaultContentInvokesCorrectLocatorMethod() { TargetLocator locator = mockTargetLocator(); cut.setFocusOnDefaultContent(); verify(locator).defaultContent(); }
private TargetLocator mockTargetLocator() { TargetLocator locator = mock(TargetLocator.class); doReturn(locator).when(webDriver).switchTo(); return locator; }
@Override public WebDriver switchTo(TargetLocator targetLocator) { return targetLocator.window(nameOrHandle); }
@Override public WebDriver switchTo(TargetLocator targetLocator) { return targetLocator.defaultContent(); }
/** * @param myTarget Parent contexts must be targeted because frame targets depend on another, * "parent" target. So each parent context must have its own WebDriver target that is * associated with, because this state is used when finding frames. * @param locator Means of finding other WebDrivers for new targets. Each new browser shares the * same locator. * @param knowsWindowHandles Finds open windows' handles. Must be associated with the same * driver as the {@code locator}. * @param elementMap Each new browser must have an element conFunction or type which can provide the current set of window * handles of the driver associated with the locator.structor map so it may create */ public TargetedWebDriverParentContext(WebDriverTarget myTarget, TargetLocator locator, KnowsWindowHandles knowsWindowHandles, ElementConstructorMap elementMap) { this.myTarget = myTarget; this.locator = locator; this.knowsWindowHandles = knowsWindowHandles; this.elementMap = elementMap; }
/** * Constructs TargetLocatorWrapper. * * @param targetLocator instance of TargetLocator. * @param frameSwitcher instance of FrameSwitcher. */ public BobcatTargetLocator(TargetLocator targetLocator, FrameSwitcher frameSwitcher) { super(); this.targetLocator = targetLocator; this.frameSwitcher = frameSwitcher; }
/** * @throws org.openqa.selenium.NotFoundException if this target is not able to be switched to, * as per {@link WebDriver#switchTo()} behavior. */ WebDriver switchTo(TargetLocator targetLocator);
/** * Issues a log message before executing {@link WebDriver#switchTo()}. * * @return A TargetLocator which can be used to select a frame or window */ public TargetLocator switchTo() { LOGGER.info("Switching WebDriver..."); return webDriver.switchTo(); }