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:edu.uga.cs.clickminer.datamodel.WindowState.java

License:Open Source License

/**
 * <p>Constructor for WindowState.</p>
 *
 * @param pclient a {@link edu.uga.cs.clickminer.ProxyClient} object.
 * @param wdriver a {@link org.openqa.selenium.WebDriver} object.
 * @param windowHandle a {@link java.lang.String} object.
 * @param hashAlgo a {@link java.lang.String} object.
 * @param minSameRequestAndPage a int./*from   ww  w  . java  2s  .  c om*/
 * @param minNoNewRequests a int.
 * @param checkLimit a int.
 */
public WindowState(ProxyClient pclient, WebDriver wdriver, String windowHandle, String hashAlgo,
        int minSameRequestAndPage, int minNoNewRequests, int checkLimit) {
    super(LogFactory.getLog(WindowState.class), hashAlgo);
    this.pclient = pclient;
    this.wdriver = wdriver;
    this.windowHandle = windowHandle;

    if (minSameRequestAndPage < 1 || minNoNewRequests < 1 || checkLimit < 1) {
        throw new BrowserStateException(
                "minSameRequestAndPage, minNoNewRequests, and " + "checkLimit must have positive values");
    }
    this.minSameRequestAndPage = minSameRequestAndPage;
    this.minNoNewCompletedRequests = minNoNewRequests;
    this.checkLimit = checkLimit;

    wdriver.switchTo().window(this.windowHandle);
    this.title = wdriver.getTitle();
    this.url = wdriver.getCurrentUrl();

}

From source file:edu.uga.cs.clickminer.test.BrowserEngineTest.java

License:Open Source License

/**
 * <p>browserEngineTest_16.</p>
 *
 * @throws java.io.IOException if any.//from w  w w. j  a va2  s  .c  o m
 * @throws java.lang.InterruptedException if any.
 */
public static void browserEngineTest_16() throws IOException, InterruptedException {
    FirefoxProfile profile = new FirefoxProfile(
            TestUtils.getWebdriverProfile("/home/cjneasbi/.mozilla/firefox", "webdriver"));
    WebDriver wdriver = new FirefoxDriver(
            new FirefoxBinary(new File("/home/cjneasbi/Desktop/old_firefox/firefox-17.0/firefox-bin")),
            profile);
    // WebDriver wdriver = new FirefoxDriver();
    wdriver.get("http://www.cs.uga.edu/~neasbitt/");
    wdriver.switchTo().activeElement();

    Thread.sleep(10000);

    WebElement elem = wdriver.findElement(By.id("logo"));
    List<String> locator = JSUtils.getElementLocator(wdriver, elem);

    for (String s : locator) {
        System.out.println(s);
    }
    wdriver.close();
}

From source file:edu.uga.cs.clickminer.test.BrowserEngineTest.java

License:Open Source License

/**
 * <p>browserEngineTest_17.</p>
 *
 * @throws java.io.IOException if any./*w w  w.  j av  a2 s  .  c  o  m*/
 * @throws java.lang.InterruptedException if any.
 * @throws edu.uga.cs.clickminer.exception.ProxyErrorException if any.
 */
public static void browserEngineTest_17() throws IOException, InterruptedException, ProxyErrorException {
    FirefoxProfile profile = new FirefoxProfile(
            TestUtils.getWebdriverProfile("/home/cjneasbi/.mozilla/firefox", "webdriver"));
    WebDriver wdriver = new FirefoxDriver(
            new FirefoxBinary(new File("/home/cjneasbi/Desktop/old_firefox/firefox-17.0/firefox-bin")), profile,
            TestUtils.createProxyConfig());
    ProxyClient pc = new ProxyClient("127.0.0.1", 8888);
    BrowserEngine bengine = new BrowserEngine(pc, wdriver, true, true);

    wdriver.get("file:///home/cjneasbi/workspace/clickminer-browser/resource/test/js_test.html");
    wdriver.switchTo().activeElement();
    Thread.sleep(5000);

    List<WebElement> elements = bengine.findJSClickableElements();
    for (WebElement elem : elements) {
        System.out.println(elem.getAttribute("id"));
    }
    bengine.close();
}

From source file:edu.uga.cs.clickminer.test.BrowserEngineTest.java

License:Open Source License

/**
 * <p>browserEngineTest_18.</p>
 *
 * @throws java.io.IOException if any.//from  w  w  w.j  av a  2 s .c om
 * @throws java.lang.InterruptedException if any.
 */
public static void browserEngineTest_18() throws IOException, InterruptedException {
    FirefoxProfile profile = new FirefoxProfile(
            TestUtils.getWebdriverProfile("/home/cjneasbi/.mozilla/firefox", "webdriver"));
    WebDriver wdriver = new FirefoxDriver(
            new FirefoxBinary(new File("/home/cjneasbi/Desktop/old_firefox/firefox-17.0/firefox-bin")),
            profile);
    // WebDriver wdriver = new FirefoxDriver();
    wdriver.get("http://www.cs.uga.edu/~neasbitt/");
    wdriver.switchTo().activeElement();

    Thread.sleep(5000);

    WebElement elem = wdriver.findElement(By.id("profile"));
    Map<String, String> attrs = JSUtils.getElementAttributes(wdriver, elem);
    System.out.println(attrs);

    wdriver.close();
}

From source file:edu.uga.cs.clickminer.util.FrameUtils.java

License:Open Source License

/**
 * Traverse frame path./*from w  w  w . j  av a  2 s.co  m*/
 * 
 * @param framePath
 *            the frame path
 */
private static void traverseFramePath(WebDriver wdriver, List<WebElement> framePath) {
    wdriver.switchTo().defaultContent();
    for (WebElement elem : framePath) {
        wdriver.switchTo().frame(elem);
    }
}

From source file:edu.uga.cs.clickminer.util.FrameUtils.java

License:Open Source License

/**
 * Finds and returns a list of paths in the frame tree to frames in the
 * current window whose source is equal to absolute url.
 *
 * @param wdriver/*from   w w w . ja  va2  s  .c  om*/
 *            the WebDriver object to use
 * @param srcurl
 *            the source url, if null matches all paths
 * @return the paths from the default frame to the matching frame, null if
 *         the a matching frame is not found, an empty path signifies that
 *         the default frame is a match
 */
public static List<FramePath> findFramePaths(WebDriver wdriver, String srcurl) {
    List<FramePath> buf = new ArrayList<FramePath>();
    wdriver.switchTo().defaultContent();
    findFramePaths(wdriver, srcurl, new FramePath(), buf);
    wdriver.switchTo().defaultContent();
    if (buf.size() > 0) {
        return buf;
    }
    return null;
}

From source file:edu.uga.cs.clickminer.util.FrameUtils.java

License:Open Source License

/**
 * Recursive helper method of findFramePaths(String srcurl).
 * //from w  w w .  j a va  2s.  co m
 * @param wdriver
 *            the WebDriver object to use
 * @param srcurl
 *            the source url, if null matches all paths
 * @param curpath
 *            the currently explored path
 * @param buf
 *            the list of paths to populate
 */
private static void findFramePaths(WebDriver wdriver, String srcurl, FramePath curpath, List<FramePath> buf) {
    if (log.isDebugEnabled()) {
        log.debug("Checking frame url: " + wdriver.getCurrentUrl() + " against " + srcurl);
    }
    String cururl = wdriver.getCurrentUrl();
    if (srcurl == null || cururl.equals(srcurl)) {
        curpath.appendUrl(cururl);
        buf.add(curpath);
    }
    List<WebElement> frames = FrameUtils.getChildFrames(wdriver);
    for (WebElement frame : frames) {
        try {
            FramePath nFramePath = new FramePath(curpath);
            nFramePath.appendFrame(frame);
            wdriver.switchTo().frame(frame);
            findFramePaths(wdriver, srcurl, nFramePath, buf);
            FrameUtils.traverseFramePath(wdriver, curpath);
        } catch (StaleElementReferenceException e) {
            if (log.isErrorEnabled()) {
                log.error("A frame could not be switched to, skipping.", e);
            }
        } catch (NoSuchFrameException e) {
            if (log.isErrorEnabled()) {
                log.error("A frame could not be switched to, skipping.", e);
            }
        }
    }
}

From source file:edu.uga.cs.clickminer.util.FrameUtils.java

License:Open Source License

private static void getAllFrameNames(WebDriver wdriver, List<WebElement> curpath, List<String> buf) {
    List<WebElement> frames = FrameUtils.getChildFrames(wdriver);
    for (WebElement frame : frames) {
        try {//from   w  ww  . j  a v  a 2s .c  o m
            String fname = frame.getAttribute("name");
            if (fname != null && !buf.contains(fname)) {
                buf.add(fname);
            }
            List<WebElement> nFramePath = new ArrayList<WebElement>();
            for (WebElement elem : curpath) {
                nFramePath.add(elem);
            }
            nFramePath.add(frame);
            wdriver.switchTo().frame(frame);
            getAllFrameNames(wdriver, nFramePath, buf);
            FrameUtils.traverseFramePath(wdriver, curpath);
        } catch (StaleElementReferenceException e) {
            if (log.isErrorEnabled()) {
                log.error("A frame could not be switched to, skipping.", e);
            }
        } catch (NoSuchFrameException e) {
            if (log.isErrorEnabled()) {
                log.error("A frame could not be switched to, skipping.", e);
            }
        }
    }
}

From source file:edu.umd.cs.guitar.replayer2.WebReplayerMonitor2.java

License:Open Source License

/**
 * Get component using their GUI identification properties
 * <p>/*from   w  w  w  .  ja v  a 2  s .  co m*/
 *
 * @return the component if found and null if not found
 */
@Override
public GComponent getComponent(GApplication application, ComponentType window, ComponentType component) {
    if (!(application instanceof WebApplication))
        throw new ReplayerConstructionException();

    WebApplication webApplication = (WebApplication) application;
    WebDriver driver = webApplication.getDriver();

    String xpathExpression = getXpathFromComponent(component);

    GUITARLog.log.debug("xPath Expression: " + xpathExpression);

    String currentHandler = driver.getWindowHandle();
    driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);

    // Scan all open window for the desired element 
    for (String windowHandler : driver.getWindowHandles()) {
        try {
            driver.switchTo().window(windowHandler);

            if (isSearchWithinWindow) {
                ComponentType windowComponent = window;
                ComponentTypeWrapper windowComponentWrapper = new ComponentTypeWrapper(windowComponent);
                String windowTitle = windowComponentWrapper.getFirstValueByName(GUITARConstants.TITLE_TAG_NAME);
                windowTitle = normalizeURL(windowTitle);

                String url = driver.getCurrentUrl();
                url = normalizeURL(url);
                if (!windowTitle.equals(url)) {
                    continue;
                }
            }

            WebElement element = driver.findElement(By.xpath(xpathExpression));
            GComponent webComponent = new WebComponent(element, null, null, null);

            GUITARLog.log.debug("Elemement FOUND in: " + driver.getCurrentUrl());
            return webComponent;

        } catch (org.openqa.selenium.NoSuchElementException e) {
            GUITARLog.log.debug("Elemement NOT found in: " + driver.getCurrentUrl());
        }
    }

    driver.switchTo().window(currentHandler);
    return null;
}

From source file:edu.umd.cs.guitar.ripper.WebRipperMonitor.java

License:Open Source License

@Override
public void closeWindow(GWindow window) {
    if (window instanceof WebWindow) {
        WebDriver driver = application.getDriver();
        driver.switchTo().window(((WebWindow) window).getHandle());
        driver.close();/*from   ww  w . ja  v a 2 s.co  m*/
        driver.switchTo().window(windowManager.getLegalWindow());
        windowManager.close(window);
    }
}