Example usage for org.openqa.selenium.remote RemoteWebDriver getWindowHandles

List of usage examples for org.openqa.selenium.remote RemoteWebDriver getWindowHandles

Introduction

In this page you can find the example usage for org.openqa.selenium.remote RemoteWebDriver getWindowHandles.

Prototype

@Override
    @SuppressWarnings({ "unchecked" })
    public Set<String> getWindowHandles() 

Source Link

Usage

From source file:com.screenslicer.core.util.Util.java

License:Open Source License

public static void get(RemoteWebDriver driver, String url, Node urlNode, boolean retry) throws ActionFailed {
    boolean exception = true;
    boolean success = true;
    String origHandle = null;/* ww w .  j a v  a  2  s  .  c o  m*/
    try {
        String source = null;
        boolean badUrl = true;
        boolean statusFail = true;
        if (urlNode != null) {
            origHandle = driver.getWindowHandle();
        }
        for (int i = 0; i < REFRESH_TRIES
                && (badUrl || statusFail || exception || CommonUtil.isEmpty(source)); i++) {
            badUrl = false;
            statusFail = false;
            exception = false;
            source = null;
            if (WebApp.DEBUG) {
                System.out.println("getting url...");
            }
            try {
                driver.getKeyboard().sendKeys(Keys.ESCAPE);
            } catch (Throwable t) {
                Log.exception(t);
            }
            if (urlNode != null) {
                try {
                    cleanUpNewWindows(driver, origHandle);
                } catch (Throwable t) {
                    Log.exception(t);
                }
            }
            try {
                Util.driverSleepVeryShort();
                if (urlNode != null) {
                    try {
                        Util.clickToNewWindow(driver, toElement(driver, urlNode));
                        Set<String> newHandles = driver.getWindowHandles();
                        for (String newHandle : newHandles) {
                            if (!origHandle.equals(newHandle)) {
                                driver.switchTo().window(newHandle);
                                break;
                            }
                        }
                    } catch (Throwable t) {
                        exception = true;
                        Log.exception(t);
                        Util.cleanUpNewWindows(driver, origHandle);
                    }
                } else {
                    driver.get("about:blank");
                    try {
                        driver.get(url);
                    } catch (TimeoutException e) {
                        Log.exception(e);
                    }
                }
                if (!exception) {
                    Util.driverSleepShort();
                    Util.driverSleepLong();
                    statusFail = HttpStatus.status(driver, urlNode != null) != 200;
                    driver.switchTo().defaultContent();
                    source = driver.getPageSource();
                    try {
                        new URL(driver.getCurrentUrl());
                        badUrl = false;
                    } catch (Throwable t) {
                        badUrl = true;
                    }
                }
            } catch (Throwable t) {
                Log.exception(t);
                exception = true;
            }
            if ((!retry || i + 1 == REFRESH_TRIES)
                    && (badUrl || statusFail || exception || CommonUtil.isEmpty(source))) {
                try {
                    driver.getKeyboard().sendKeys(Keys.ESCAPE);
                    Util.driverSleepVeryShort();
                } catch (Throwable t) {
                    Log.exception(t);
                }
                success = false;
                if (!retry) {
                    break;
                }
            }
        }
        if (WebApp.DEBUG) {
            System.out.println("getting url - done");
        }
    } catch (Throwable t) {
        Log.exception(t);
        success = false;
    }
    if (!success) {
        if (urlNode != null && origHandle != null) {
            try {
                cleanUpNewWindows(driver, origHandle);
            } catch (Throwable t) {
                Log.exception(t);
            }
        }
        throw new ActionFailed();
    }
}

From source file:com.screenslicer.core.util.Util.java

License:Open Source License

public static String newWindow(RemoteWebDriver driver) throws ActionFailed {
    try {// w  ww  .j ava  2s . com
        String origHandle = driver.getWindowHandle();
        cleanUpNewWindows(driver, origHandle);
        try {
            driver.getKeyboard().sendKeys(Keys.chord(Keys.CONTROL + "n"));
        } catch (Throwable t) {
            Log.exception(t);
        }
        Util.driverSleepStartup();
        Collection<String> handles = new HashSet<String>(driver.getWindowHandles());
        handles.remove(origHandle);
        if (!handles.isEmpty()) {
            driver.switchTo().window(handles.iterator().next());
        } else {
            driver.executeScript("window.open('');");
            Util.driverSleepStartup();
            handles = new HashSet<String>(driver.getWindowHandles());
            handles.remove(origHandle);
            if (!handles.isEmpty()) {
                driver.switchTo().window(handles.iterator().next());
            }
        }
        return driver.getWindowHandle();
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.screenslicer.core.util.Util.java

License:Open Source License

public static void cleanUpNewWindows(RemoteWebDriver driver, String handleToKeep) throws ActionFailed {
    try {//from   ww w .j  a  v  a 2  s  .  co m
        Set<String> handles = new HashSet<String>(driver.getWindowHandles());
        for (String handle : handles) {
            try {
                if (!handleToKeep.equals(handle)) {
                    driver.switchTo().window(handle);
                    driver.close();
                }
            } catch (Throwable t) {
                Log.exception(t);
            }
        }
        driver.switchTo().window(handleToKeep);
        driver.switchTo().defaultContent();
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:org.uiautomation.ios.Demo.java

License:Apache License

public static void main(String[] args) throws Exception {

    String[] a = { "-port", "5555", "-host", "127.0.0.1",
            //"-aut", SampleApps.getUICatalogFile(),
            //"-aut", SampleApps.getUICatalogIpad(),
            //"-beta",
            "-folder", "applications", "-simulators", "-hub", "http://127.0.0.1:4444/grid/register" };

    IOSServerConfiguration config = IOSServerConfiguration.create(a);

    IOSServer server = new IOSServer(config);
    server.start();/*from w w  w .  j  a v a 2s  . co  m*/

    IOSCapabilities cap = IOSCapabilities.iphone("eBay");
    cap.setCapability(IOSCapabilities.IOS_SWITCHES, Arrays.asList(new String[] { "-e", "useQA", "YES" }));

    RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
    try {

        driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);

        WebElement agree = driver.findElement(By.name("Agree"));
        agree.click();

        //driver.switchTo().alert().dismiss();
        WebElement signInButton = driver.findElement(By.xpath("//UIAButton[@name='Sign In']"));
        signInButton.click();
        WebElement user = driver.findElement(By.xpath("//UIATextField[@value='User ID or email']"));
        user.sendKeys(userId);
        WebElement pass = driver.findElement(By.xpath("//UIASecureTextField[@value='Password']"));
        pass.sendKeys(password);
        WebElement element4 = driver.findElement(By.xpath("//UIAButton[@name='Done']"));
        element4.click();

        Thread.sleep(2000);
        boolean cartDown = false;
        // navigate
        String itemId = "220003725262";
        if (cartDown) {
            WebElement search = driver.findElement(By.xpath("//UIAButton[@name='Search']"));
            search.click();

            //validate
            WebElement element1 = driver.findElement(By.xpath("//UIASearchBar[@value='Search Watching']"));
            element1.sendKeys(itemId);

            // check it's there.
            WebElement result = driver.findElement(By.className("UIATableCell"));
            System.out.println(result.getAttribute("name"));

        } else {
            WebElement basket = driver.findElement(By.xpath("//UIAButton[@name='Basket, 1 item']"));
            //By.xpath("//UIAButton[matches(@name,l10n('ItemCountFormatStringPlural'))]"));
            //ItemCountFormatStringSingular
            basket.click();

            //WebElement firstItem = driver.findElement(By.xpath("//UIATableCell[2]"));
            //firstItem.click();

            WebElement search = driver.findElement(By.name("Checkout"));
            search.click();

        }

        // watch
        WebElement paypal = driver.findElement(By.name("Please login to your account"));
        paypal.click();

        while (driver.getWindowHandles().size() != 3) {
            Thread.sleep(1000);
        }

        WebElement email = null;
        while (email == null) {
            try {
                driver.switchTo().window("Web_3");
                email = driver.findElement(By.id("email"));
            } catch (NoSuchElementException e) {
                Thread.sleep(1000);
                System.out.println("not found");
            }
        }

        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        driver.findElement(By.cssSelector(".resetter")).click();
        email.sendKeys("ppbuild-1920977828325914@paypal.com");

        WebElement password = driver.findElement(By.id("password"));
        password.sendKeys("11111111");

        WebElement login = driver.findElement(By.id("login"));
        login.click();

        Thread.sleep(30000);

    } finally {
        driver.quit();
        server.stop();
    }

}