Example usage for org.openqa.selenium WebDriver switchTo

List of usage examples for org.openqa.selenium WebDriver switchTo

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver switchTo.

Prototype

TargetLocator switchTo();

Source Link

Document

Send future commands to a different frame or window.

Usage

From source file:jp.vmi.selenium.selenese.subcommand.GetAllWindowTitles.java

License:Apache License

@Override
public String[] execute(Context context, String... args) {
    WebDriver driver = context.getWrappedDriver();
    String current = driver.getWindowHandle();
    try {/*from   w  w w .jav a  2  s  . c o  m*/
        return driver.getWindowHandles().stream().map(handle -> {
            driver.switchTo().window(handle);
            return driver.getTitle();
        }).toArray(String[]::new);
    } finally {
        driver.switchTo().window(current);
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.GetAttributeFromAllWindows.java

License:Apache License

@Override
public Object[] execute(Context context, String... args) {
    String attrName = args[ARG_ATTR_NAME];
    WebDriver driver = context.getWrappedDriver();
    String current = driver.getWindowHandle();
    try {//from w w w.ja  va  2s  . c om
        return driver.getWindowHandles().stream().map(handle -> {
            driver.switchTo().window(handle);
            return ((JavascriptExecutor) driver).executeScript("return window[arguments[0]];", attrName);
        }).toArray();
    } finally {
        driver.switchTo().window(current);
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.SeleneseRunnerWindows.java

License:Apache License

@Override
public void selectWindow(WebDriver driver, String windowID) {
    if (null == windowID || "null".equals(windowID) || "".equals(windowID)) {
        driver.switchTo().window(context.getInitialWindowHandle());
    } else if ("_blank".equals(windowID)) {
        selectBlankWindow(driver);//from   w  ww . j a v a 2s .  c o  m
    } else {
        if (windowID.startsWith("title=")) {
            selectWindowWithTitle(driver, windowID.substring("title=".length()));
            return;
        }

        if (windowID.startsWith("name=")) {
            windowID = windowID.substring("name=".length());
        }

        try {
            driver.switchTo().window(windowID);
        } catch (NoSuchWindowException e) {
            selectWindowWithTitle(driver, windowID);
        }
    }

    if (lastFrame.containsKey(driver.getWindowHandle())) {
        // If the frame has gone, fall back
        try {
            selectFrame(driver, lastFrame.get(driver.getWindowHandle()));
        } catch (SeleniumException e) {
            lastFrame.remove(driver.getWindowHandle());
        }
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.SeleneseRunnerWindows.java

License:Apache License

@Override
public void selectPopUp(WebDriver driver, String windowID) {
    if ("null".equals(windowID) || "".equals(windowID)) {
        Set<String> windowHandles = driver.getWindowHandles();
        windowHandles.remove(context.getInitialWindowHandle());
        if (windowHandles.size() > 0) {
            driver.switchTo().window(windowHandles.iterator().next());
        } else {//w  w  w.j  a v a  2s  . c o  m
            throw new SeleniumException("Unable to find a popup window");
        }
    } else {
        selectWindow(driver, windowID);
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.SeleneseRunnerWindows.java

License:Apache License

@Override
public void selectFrame(WebDriver driver, String locator) {
    if ("relative=top".equals(locator)) {
        driver.switchTo().defaultContent();
        lastFrame.remove(driver.getWindowHandle());
        return;/*from w  w  w . j a v a 2s .c  o m*/
    }

    // TODO uncomment when support parentFrame() on next Selenium.
    //    if ("relative=up".equals(locator)) {
    //      driver.switchTo().parentFrame();
    //      lastFrame.put(driver.getWindowHandle(), locator);
    //      return;
    //    }

    if (locator.startsWith("index=")) {
        try {
            int index = Integer.parseInt(locator.substring("index=".length()));
            lastFrame.put(driver.getWindowHandle(), locator);
            driver.switchTo().frame(index);
            return;
        } catch (NumberFormatException e) {
            throw new SeleniumException(e.getMessage(), e);
        } catch (NoSuchFrameException e) {
            throw new SeleniumException(e.getMessage(), e);
        }
    }

    if (locator.startsWith("id=")) {
        locator = locator.substring("id=".length());
    } else if (locator.startsWith("name=")) {
        locator = locator.substring("name=".length());
    }

    try {
        lastFrame.put(driver.getWindowHandle(), locator);
        driver.switchTo().frame(locator);
    } catch (NoSuchFrameException e) {
        throw new SeleniumException(e.getMessage(), e);
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.SeleneseRunnerWindows.java

License:Apache License

/**
 * Selects the only <code>_blank</code> window. A window open with <code>target='_blank'</code>
 * will have a <code>window.name = null</code>.
 * <br>//from  w ww . j a  v  a 2 s.  com
 * <p>
 * This method assumes that there will only be one single <code>_blank</code> window and selects
 * the first one with no name. Therefore if for any reasons there are multiple windows with
 * <code>window.name = null</code> the first found one will be selected.
 * </p>
 * <p>
 * If none of the windows have <code>window.name = null</code> the last selected one will be
 * re-selected and a {@link SeleniumException} will be thrown.
 * </p>
 *
 * @throws NoSuchWindowException if no window with <code>window.name = null</code> is found.
 */
@Override
public void selectBlankWindow(WebDriver driver) {
    String current = driver.getWindowHandle();
    // Find the first window without a "name" attribute
    List<String> handles = new ArrayList<String>(driver.getWindowHandles());
    for (String handle : handles) {
        // the original window will never be a _blank window, so don't even look at it
        // this is also important to skip, because the original/root window won't have
        // a name either, so if we didn't know better we might think it's a _blank popup!
        if (handle.equals(context.getInitialWindowHandle())) {
            continue;
        }
        driver.switchTo().window(handle);
        String value = (String) ((JavascriptExecutor) driver).executeScript("return window.name;");
        if (value == null || "".equals(value)) {
            // We found it!
            return;
        }
    }
    // We couldn't find it
    driver.switchTo().window(current);
    throw new SeleniumException("Unable to select window _blank");
}

From source file:minium.web.internal.actions.WebWaitPredicates.java

License:Apache License

public static <T extends Elements> Predicate<T> forAlert() {
    return new Predicate<T>() {
        @Override/*w w w  . j  a  v a 2 s.  c  om*/
        public boolean apply(T elems) {
            WebDriver nativeWebDriver = elems.as(HasNativeWebDriver.class).nativeWebDriver();
            try {
                return nativeWebDriver.switchTo().alert() != null;
            } catch (NoAlertPresentException e) {
                return false;
            }
        }
    };
}

From source file:omelet.driver.DriverUtility.java

License:Apache License

/***
 * Switching between windows.//from   w  ww  .j a v a 2 s . c  o m
 * 
 * @param driver
 * @param sString
 *            :Target window Title
 * @return:True if window switched
 */
public static boolean switchToWindow(WebDriver driver, String sString) {
    String currentHandle = driver.getWindowHandle();
    Set<String> handles = driver.getWindowHandles();
    if (!handles.isEmpty()) {
        for (String handle : handles) {
            LOGGER.debug("Switching to other window");
            driver.switchTo().window(handle);
            if (sString.equals(driver.getTitle())) {
                LOGGER.info("switched to window with title:" + sString);
                return true;
            }
        }
        driver.switchTo().window(currentHandle);

        LOGGER.info("Window with title:" + sString + " Not present,Not able to switch");
        return false;
    } else {
        LOGGER.info("There is only one window handle :" + currentHandle);
        return false;
    }
}

From source file:org.auraframework.components.ui.inputRichText.InputRichTextUITest.java

License:Apache License

/**
 * ui:inputRichText doesn't render its initial value
 * Test case: W-2428455// w w w  . ja va  2 s  .c o m
 * @throws Exception
 * Excluding test as switchTo not supported with ios and android drivers
 */
@ExcludeBrowsers({ BrowserType.SAFARI, BrowserType.ANDROID_PHONE, BrowserType.ANDROID_TABLET, BrowserType.IPAD,
        BrowserType.IPHONE })
public void testRenderInitialValueOfRichText() throws Exception {
    String defaultText = "testing text";
    WebDriver driver = this.getDriver();
    open(String.format("%s?value=%s", CMP_URL, defaultText));
    WebElement ckEditor = auraUITestingUtil.waitForElement(By.cssSelector(CK_EDITOR_LOCATOR));
    WebElement ckEditorInput = ckEditor.findElement(By.tagName("iframe"));
    driver.switchTo().frame(ckEditorInput);
    waitForElementPresent(driver.findElement(By.cssSelector(IN_RICHTEXT_BODY)));
    auraUITestingUtil.waitForElementText(By.cssSelector(IN_RICHTEXT_BODY), defaultText, true);
    driver.switchTo().defaultContent();
}

From source file:org.auraframework.integration.test.components.ui.inputRichText.InputRichTextUITest.java

License:Apache License

/**
 * ui:inputRichText doesn't render its initial value Test case: W-2428455
 *///from  w  w w .  j a va 2 s . com
// Excluding test as switchTo not supported with android drivers
@ExcludeBrowsers({ BrowserType.SAFARI, BrowserType.ANDROID_PHONE, BrowserType.ANDROID_TABLET })
@Test
// TODO: Flapping on Jenkins autobuilds
public void _testRenderInitialValueOfRichText() throws Exception {
    String defaultText = "testing text";
    WebDriver driver = this.getDriver();
    open(String.format("%s?value=%s", CMP_URL, defaultText));
    WebElement ckEditor = getAuraUITestingUtil().waitForElement(By.cssSelector(CK_EDITOR_LOCATOR));
    WebElement ckEditorInput = ckEditor.findElement(By.tagName("iframe"));
    driver.switchTo().frame(ckEditorInput);
    getAuraUITestingUtil().waitForElementDisplayed(By.cssSelector(IN_RICHTEXT_BODY), "no richtext body");
    getAuraUITestingUtil().waitForElementText(By.cssSelector(IN_RICHTEXT_BODY), defaultText, true);
    driver.switchTo().defaultContent();
}