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.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method shall wait for an alert shows-up.
 * /*  w  w  w .j a  va 2  s  .c  om*/
 * @param driver
 * @param timeout
 * @param pollingTime
 * @return
 */
public Alert WaitForAlert(final WebDriver driver, final long timeout, final long pollingTime) {
    this.log.debug("WaitForAlert::Enter");
    ExecutorService executor = null;

    Alert alert = null;

    try {
        class CheckForAlert implements Callable<Alert> {

            @Override
            public Alert call() {
                Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(timeout, TimeUnit.SECONDS)
                        .pollingEvery(pollingTime, TimeUnit.MILLISECONDS);

                return wait.until(new Function<WebDriver, Alert>() {

                    @Override
                    public Alert apply(WebDriver driver) {
                        try {
                            return driver.switchTo().alert();
                        } catch (NoAlertPresentException e) {
                            return null;
                        }
                    }
                });
            }
        }

        CheckForAlert call = new CheckForAlert();
        executor = Executors.newSingleThreadExecutor();
        alert = executor.submit(call).get(timeout + 2, TimeUnit.SECONDS);

    } catch (InterruptedException ie) {
        this.log.warn("Interrupted Exception");
        this.log.warn(ie.toString());
    } catch (ExecutionException ee) {
        if (ee.getCause().getClass().getCanonicalName()
                .equalsIgnoreCase(TimeoutException.class.getCanonicalName())) {
            this.log.warn("WebDriver timeout exceeded!");
        } else {
            this.log.warn("Execution Exception");
            this.log.warn(ee.toString());
        }
    } catch (java.util.concurrent.TimeoutException cte) {
        this.log.warn("Thread timeout exceeded!");
        this.log.warn(cte.toString());
    } catch (Exception e) {
        this.log.error("Exception");
        this.log.catching(e);
    }

    if (executor != null) {
        executor.shutdown();
    }
    this.log.debug("WaitForAlert::Exit");
    return alert;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method shall select a new window that has opened and return the handle for the parent window.
 * // w w w  .ja v  a2  s . com
 * The handle should be stored in a string so we can go back to the parent window afterwards
 * using the SelectParentWindow method
 * 
 * @param driver
 * @return parentWindow
 */
public String SelectNewWindow(final WebDriver driver) {
    String parentWindowHandle = driver.getWindowHandle();
    Set<String> listWindows = driver.getWindowHandles();
    WaitForNewWindow(driver);
    listWindows = driver.getWindowHandles();
    // Get the windowHandler of the new open window
    Iterator<String> iterWindows = listWindows.iterator();
    while (iterWindows.hasNext()) {
        String windowHandle = iterWindows.next();
        if (windowHandle.equals(parentWindowHandle) == false) {
            driver.switchTo().window(windowHandle);
            break;
        }
    }
    return parentWindowHandle;
}

From source file:com.pentaho.ctools.utils.ElementHelper.java

License:Apache License

/**
 * This method shall close current window and select previous window to carry on testing
 * /*  w ww.  j  a v a  2s.c o  m*/
 * @param driver
 * @param parentWindow
 */
public void SelectParentWindow(final WebDriver driver, final String parentWindow) {
    // Need guarantee we close everything
    WebDriver previewWindow = null;
    String currentWindowHandle = driver.getWindowHandle();
    previewWindow = driver.switchTo().window(currentWindowHandle);
    previewWindow.close();
    driver.switchTo().window(parentWindow);
}

From source file:com.qulix.ft.teachingSite.Conditions.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./* www .  ja v  a  2 s .  c  o m*/
 */
public static ExpectedCondition<WebDriver> frameToBeAvailableAndSwitchToIt(final String frameLocator) {
    return new ExpectedCondition<WebDriver>() {
        public WebDriver apply(WebDriver from) {
            try {
                return from.switchTo().frame(frameLocator);
            } catch (NoSuchFrameException e) {
                return null;
            }
        }

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

From source file:com.qulix.ft.teachingSite.Conditions.ExpectedConditions.java

License:Apache License

public static ExpectedCondition<Alert> alertIsPresent() {
    return new ExpectedCondition<Alert>() {
        public Alert apply(WebDriver input) {
            try {
                return input.switchTo().alert();
            } catch (NoAlertPresentException e) {
                return null;
            }//from w  w  w .jav a2 s.  c  o m
        }

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

From source file:com.raja.anucarita.SeCustomActions.java

License:Open Source License

public static String seAlert(WebDriver driver, String alertMessage, String actionNeeded) throws Exception {
     try {/* w ww.  j  a  v  a2s.  c o m*/
         SeCustomUtils.seTakeAlertScreenShot(driver);

         Alert alert = driver.switchTo().alert();
         String getAlertMessage = alert.getText();

         //testCaseResult(true,"","","","Alert for "+alertMessage+" is present");

         SeCustomUtils.seTakeAlertScreenShot(driver);

         if (actionNeeded.equalsIgnoreCase("accept")) {
             alert.accept();
         } else if (actionNeeded.equalsIgnoreCase("dismiss") || actionNeeded.equalsIgnoreCase("cancel")) {
             alert.dismiss();
         }
         result = false;
         if (getAlertMessage.equalsIgnoreCase(alertMessage)) {
             result = true;
         }
         //testCaseResult(result,"","","","alertMessage from UI is "+getAlertMessage+" and alert message is "+alertMessage+" ");

         SeCustomUtils.seTakeAlertScreenShot(driver);
     } catch (Exception e) {
         //testCaseResult(false,"","","","Alert for "+alertMessage+" is not present");
         SeCustomUtils.seTakeAlertScreenShot(driver);
     }
     return alertMessage;
 }

From source file:com.raja.anucarita.SeCustomActions.java

License:Open Source License

public static void seChangeWindow(WebDriver driver, String windowName) throws Exception {
     SeCustomUtils.seTakeScreenShot(driver);
     driver.switchTo().window(windowName);
     SeCustomUtils.seTakeScreenShot(driver);
 }

From source file:com.raja.anucarita.SeCustomActions.java

License:Open Source License

public static void seChangeIFrame(WebDriver driver, String locator) throws Exception {
     SeCustomUtils.seTakeScreenShot(driver);
     locator = values.getProperty(locator);
     element = SeCustomUtils.elementReturn(driver, locator);
     driver.switchTo().frame(element);
     SeCustomUtils.seTakeScreenShot(driver);
 }

From source file:com.raja.anucarita.SeWrapper.java

License:Open Source License

public static void invokeMethods(WebDriver driver, Class<?> c) throws Exception {
    Object t = c.newInstance();//from w  ww  .  j a va  2 s.c  o m
    Method m[] = c.getDeclaredMethods();

    for (Method name : m) {
        String methodname = name.getName();
        try {
            name.invoke(t, driver);

            SeCustomUtils.seTakeAlertScreenShot(driver);
        } catch (Exception e) {
            SeCustomUtils.seTakeAlertScreenShot(driver);
        } finally {
            try {
                SeCustomUtils.seTakeAlertScreenShot(driver);

                alert = driver.switchTo().alert();
                String getAlertMessage = alert.getText().replaceAll("\\n", "");
                if (!(getAlertMessage.equals(null)) || !(getAlertMessage.equals(""))) {
                    Utils.testCaseResult(false, "", "", "",
                            "There is and unexpected alert. Alert message is " + getAlertMessage + "");
                }

                SeCustomUtils.seTakeAlertScreenShot(driver);
            } catch (Exception e) {
                SeCustomUtils.seTakeAlertScreenShot(driver);
            }
        }
    }
}

From source file:com.redhat.darcy.webdriver.internal.CachingTargetedWebDriverTest.java

License:Open Source License

@Test
public void shouldSwitchUntargetedDriverBeforeForwardingCall() {
    WebDriver mockedDriver = mock(WebDriver.class);
    TargetLocator mockedTargetLocator = mock(TargetLocator.class);

    when(mockedDriver.switchTo()).thenReturn(mockedTargetLocator);

    TargetedWebDriverFactory targetedDriverFactory = new CachingTargetedWebWebDriverFactory(mockedDriver,
            WebDriverTargets.window("test"));

    TargetedWebDriver targetedWebDriver = targetedDriverFactory
            .getTargetedWebDriver(WebDriverTargets.window("test2"));

    targetedWebDriver.get("url");

    verify(mockedDriver).switchTo();//from   w w  w  .j  a va  2s.c  o  m
    verify(mockedTargetLocator).window("test2");
    verify(mockedDriver).get("url");
}