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:com.common.ExpectedConditions.java

License:Apache License

/**
 * 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./*from w  ww .  ja  v  a2  s  . c om*/
 *
 * @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;
        }
    };
}

From source file:com.common.ExpectedConditions.java

License:Apache License

/**
 * 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.//w w  w. j av a2s .  com
 *
 * @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(findElement(locator, driver));
            } catch (NoSuchFrameException e) {
                return null;
            }
        }

        @Override
        public String toString() {
            return "frame to be available: " + locator;
        }
    };
}

From source file:com.common.ExpectedConditions.java

License:Apache License

/**
 * 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./*from   w w w.ja  v  a 2s  . com*/
 *
 * @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;
        }
    };
}

From source file:com.common.ExpectedConditions.java

License:Apache License

/**
 * 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./*from  w w w . j a  va2s. c  om*/
 *
 * @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;
        }
    };
}

From source file:com.common.ExpectedConditions.java

License:Apache License

public static ExpectedCondition<Alert> alertIsPresent() {
    return new ExpectedCondition<Alert>() {
        @Override/*from   w ww  .  java  2 s.  c  o  m*/
        public Alert apply(WebDriver driver) {
            try {
                return driver.switchTo().alert();
            } catch (NoAlertPresentException e) {
                return null;
            }
        }

        @Override
        public String toString() {
            return "alert to be present";
        }
    };
}

From source file:com.deque.axe.AXE.java

License:Mozilla Public License

/**
 * Recursively injects aXe into all iframes and the top level document.
 *
 * @param driver WebDriver instance to inject into
 *//* w  w w .  j a v a2s .  c  om*/
public static void inject(final WebDriver driver, final URL scriptUrl) {
    final String script = getContents(scriptUrl);
    final ArrayList<WebElement> parents = new ArrayList<WebElement>();

    injectIntoFrames(driver, script, parents);

    JavascriptExecutor js = (JavascriptExecutor) driver;
    driver.switchTo().defaultContent();
    js.executeScript(script);
}

From source file:com.deque.axe.AXE.java

License:Mozilla Public License

/**
 * Recursively find frames and inject a script into them.
 * @param driver An initialized WebDriver
 * @param script Script to inject//from  ww w  .java2s . co m
 * @param parents A list of all toplevel frames
 */
private static void injectIntoFrames(final WebDriver driver, final String script,
        final ArrayList<WebElement> parents) {
    final JavascriptExecutor js = (JavascriptExecutor) driver;
    final List<WebElement> frames = driver.findElements(By.tagName("iframe"));

    for (WebElement frame : frames) {
        driver.switchTo().defaultContent();

        if (parents != null) {
            for (WebElement parent : parents) {
                driver.switchTo().frame(parent);
            }
        }

        driver.switchTo().frame(frame);
        js.executeScript(script);

        ArrayList<WebElement> localParents = (ArrayList<WebElement>) parents.clone();
        localParents.add(frame);

        injectIntoFrames(driver, script, localParents);
    }
}

From source file:com.dhenton9000.selenium.generic.BaseTest.java

protected boolean isAlertPresent(WebDriver driver) {
    boolean isAlertPresent = true;
    String oldWindow = driver.getWindowHandle();
    try {/*  w w  w.  j av  a 2  s . c  o m*/
        driver.switchTo().alert();
    } catch (NoAlertPresentException err) {
        return false;
    }
    driver.switchTo().window(oldWindow);
    return isAlertPresent;
}

From source file:com.dhenton9000.selenium.generic.BaseTest.java

/**
 * This only tests if the item exists, and returns the state of the driver.
 *
 * @param frameTitle/*from www  .  java  2s  . c o  m*/
 * @param driver
 * @return true if frame is present
 */
protected boolean isFramePresent(String frameTitle, WebDriver driver) {
    boolean framePresent = true;
    WebDriver oldDriver = driver;
    try {
        driver.switchTo().frame("ModelFrameTitle");
        driver = oldDriver;
    } catch (NoSuchFrameException err) {
        framePresent = false;
    } catch (Exception err) {
        logger.error("err " + err.getClass().getName());
    }
    return framePresent;

}

From source file:com.dhenton9000.selenium.generic.UtilMethods.java

/**
 * handle an alert if no alert present error caught and reported
 * @param driver//from   ww w.  ja  va2s  . c  o m
 * @param doAccept 
 */
public static void handleAlert(WebDriver driver, boolean doAccept) {

    try {
        Alert a = driver.switchTo().alert();
        if (doAccept)
            a.accept();
        else
            a.dismiss();
    } catch (Exception err) {
        LOG.warn("handle alert error " + err.getClass().getName() + " " + err.getMessage());

    }

}