Example usage for org.openqa.selenium WebDriver getWindowHandles

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

Introduction

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

Prototype

Set<String> getWindowHandles();

Source Link

Document

Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to #switchTo() .

Usage

From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

License:Apache License

/**
 * Retrieving all the browser titles opened.
 * /*from   w w  w  .j  a  v a  2 s .  c om*/
 * @return the all windows
 * @throws Exception
 *             the exception
 */
public final Set<String> getAllWindows() throws Exception {

    WebDriver driver = getDriver();
    try {
        // allData = new String[ (availableWindows.size() )];
        return driver.getWindowHandles();

    } catch (Exception e) {
        throw new Exception("cannot access the windows ", e);
    }
}

From source file:com.volkhart.selenium.util.Window.java

License:Apache License

/**
 * Switches the WebDriver pointer to the specified window
 * /*  w  w w  .j  a  v  a2 s .c  om*/
 * Uses {@code String.contains()} to identify windows.
 * 
 * @param driver
 *            WebDriver being redirected
 * @param partialTitle
 *            A portion of the title of the window.
 */
public static void switchTo(WebDriver driver, String partialTitle) {
    Set<String> windows = driver.getWindowHandles();
    for (String s : windows) {
        driver.switchTo().window(s);
        if (driver.getTitle().toLowerCase().contains(partialTitle.toLowerCase()))
            break;
    }
}

From source file:com.westconcomster.MO365.java

public void mo365Order(WebDriver driver, String region) throws InterruptedException {

    String winHandleBefore = driver.getWindowHandle();
    Thread.sleep(10000);//w w w  . jav a 2s .c  o m
    driver.findElement(By.xpath("//p[contains(.,'Buy Now')]")).click();

    //Select the customer
    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
        System.out.println(">>>>>>>>>>inside child window");

        //Search for the customer
        Thread.sleep(5000);
        WebElement searchCust = driver.findElement(By.xpath("(.//*[@id='search_value'])[4]"));
        searchCust.clear();
        searchCust.sendKeys(Constants.companyName);
        Thread.sleep(5000);
        driver.findElement(By.xpath(
                ".//*[@id='customersList']/div[1]/ul/div[1]/div/div[2]/div/ecommerceselectable-customer/div/div[3]/div"))
                .click();

        //Select MO365
        WebElement searchProduct = driver.findElement(By.xpath("(.//*[@id='search_value'])[5]"));
        searchProduct.clear();
        searchProduct.sendKeys("MO365");
        Thread.sleep(2000);
        driver.findElement(By.xpath("//ecommerceselectable-product/div/div[3]/div/p")).click();
        (new WebDriverWait(driver, 60))
                .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='quoteName']")))
                .sendKeys("LATAM_MO365_Order");

        Select dropdown = new Select(driver.findElement(By.name("root[contract][supportLevel]")));
        dropdown.selectByIndex(1);

        //Add reseller Margin
        driver.findElement(By.name("root[contract][resellermargin]")).clear();
        driver.findElement(By.name("root[contract][resellermargin]")).sendKeys("10");
        System.out.println(">>>>>>>Reseller Margin Added");
        driver.findElement(By.name("root[contract][billenduser]")).click();
        System.out.println(">>>>>>>>Bill End User");
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0,350)", "");
        Thread.sleep(5000);

        //select license
        MO365Licences mo365lic = new MO365Licences();
        mo365lic.mo365Licences(driver, region);
        //Select MO365 Add-ons
        MO365AddOns moadd = new MO365AddOns();
        moadd.mo365AddOns(driver, region);

        //Select Westcon Services
        MO365WestconServices mo365WS = new MO365WestconServices();
        mo365WS.mo365WestconServices(driver, region);

        //Total price
        WebElement price = (new WebDriverWait(driver, 60)).until(
                ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[text()[contains(.,'Total:')]]")));
        String totalAmt = price.getText().substring(price.getText().indexOf(":") + 1).trim();
        System.out.println(">>>>>>>>>>>TotalAmount" + totalAmt);

        //Click Next Button
        (new WebDriverWait(driver, 20).until(
                ExpectedConditions.visibilityOfElementLocated(By.xpath(".//button[contains(text(),'Next')]"))))
                        .click();
        Thread.sleep(10000);
        driver.findElement(By.xpath(".//button[contains(text(),'Submit Order')]")).click();

        //Add domain
        WebElement domain1 = (new WebDriverWait(driver, 30).until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("//input[@name='root[microsoft_info][domain_prefix]']"))));
        domain1.sendKeys(Constants.domian);
        domain1.sendKeys(Keys.TAB);
        Thread.sleep(10000);

        //Click on Submit Order
        WebElement submit = (new WebDriverWait(driver, 20).until(ExpectedConditions
                .visibilityOfElementLocated(By.xpath("//button[contains(text(),'Submit Order')]"))));
        submit.click();
        System.out.println(">>>>>>>>Order Submitted ");
        Thread.sleep(10000);

        //PO Number
        String orderNumber = driver.findElement(By.xpath(".//ecommercewizardstep[4]/section/div/div[2]/p"))
                .getText().replaceAll("\\D", "").trim();
        ;
        System.out.println(">>>>>>>>>>>Order number" + orderNumber);

        //Click on Close Button
        driver.findElement(By.xpath("//ecommercewizardstep[4]/section/div/div[2]/article/button[2]")).click();
        driver.switchTo().window(winHandleBefore);

        //*******Verify Order on Portal
        Thread.sleep(10000);
        String orderScreenOrder = driver.findElement(By.xpath(
                ".//*[@id='orders']/div/section[2]/div[1]/div[2]/div/div[2]/div/order/div[1]/div/div[1]/div[3]/div/div[1]/div[2]/div[2]"))
                .getText();
        System.out.println(">>>>>>>>>> OrderID" + orderScreenOrder);
        if (orderScreenOrder.equals(orderNumber)) {
            System.out.println(">>>>>> PASS <<<<<<< MO365 Order Created Successfully on portal");
        } else {
            System.out.println("Order FAILED");
        }
    }

}

From source file:crawl.CrawlThread.java

License:Open Source License

@Override
public void run() {
    WebDriver driver = new FirefoxDriver(); // The Firefox driver supports javascript 
    Autenticate.perform(driver, fb_email, fb_password);
    try {//from   ww  w  . j av  a2  s  .  com
        Thread.sleep(Globals.TIME_TO_LOGIN_IN_FB_MS);
    } catch (Exception ex) {
        System.out.println("InterruptedException in crawl.run()");
        ex.printStackTrace();
        System.exit(-1);
    }
    for (String post_id : post_list) {
        //OPEN NEW TAB
        WebElement body = driver.findElement(By.tagName("body"));
        body.sendKeys(Keys.CONTROL + "t");
        Tree post = new Tree(post_id, GLOBALS);
        boolean result = post.crawl(driver);
        if (result) {
            post.prune();
            post.print();
            post.printEdgeList();
        } else {
            body.sendKeys(Keys.CONTROL + "w");
        }
        //SWITCH TAB
        ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
        driver.switchTo().window(tabs.get(tabs.size() - 1));
    }
    driver.quit();
}

From source file:de.knowwe.uitest.DiaFluxUITest.java

License:Open Source License

protected void switchToEditor(String articleHandle) throws InterruptedException {
    new WebDriverWait(getDriver(), 10).until((WebDriver driver) -> driver.getWindowHandles().size() == 2);
    Set<String> windowHandles = new HashSet<>(getDriver().getWindowHandles());
    windowHandles.remove(articleHandle);
    getDriver().switchTo().window(windowHandles.iterator().next());
    new WebDriverWait(getDriver(), 10)
            .until(ExpectedConditions.presenceOfElementLocated(By.id("start_prototype")));
}

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

License:Open Source License

/**
 * Get component using their GUI identification properties
 * <p>/*from   ww w. j  ava 2  s  .c  o 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.simple.HtmlUnitDriverTest.java

License:Open Source License

@Test
public void test() {
    WebDriver driver;
    driver = new HtmlUnitDriver();
    ((HtmlUnitDriver) driver).setJavascriptEnabled(true);

    driver.get("about:blank");
    ((JavascriptExecutor) driver).executeScript(injectAnchorTag(XYZ_TEST, "about:blank"));
    System.out.println(driver.getWindowHandles().size());

    // Click on the anchor element
    WebElement anchor;//from  www . j a  v  a  2s .c  o  m
    anchor = driver.findElement(By.id(XYZ_TEST));
    anchor.click();

    System.out.println(driver.getWindowHandles().size());
}

From source file:edu.umd.cs.guitar.simple.PageNotFoundTest.java

License:Open Source License

@Test
public void testPageNotFound() {

    WebDriver driver;
    driver = new FirefoxDriver();
    WebWindowManager wwm = WebWindowManager.getInstance(driver);
    wwm.createNewWindow(PAGENOTFOUND_URL);
    System.out.println(driver.getTitle());
    System.out.println(driver.getWindowHandles().size());
}

From source file:io.github.mmichaelis.selenium.client.provider.AbstractWebDriverProviderTest.java

License:Apache License

private void prepareResetTest(final HtmlUnitDriver testedDriver)
        throws InterruptedException, ExecutionException {
    final URL otherPageUrl = Resources.getResource(this.getClass(), "page2.html");
    final URL newPageUrl = Resources.getResource(this.getClass(), "page3.html");
    final WebDriverProvider driverProvider = new NoExceptionWebDriverProvider(testedDriver);
    final ExecutorService executorService = newCachedThreadPool();
    final Future<Void> openWindowResultFuture = executorService.submit(new Callable<Void>() {
        @Nullable/*from  w ww  .  ja va2  s  . c o  m*/
        @Override
        public Void call() {
            final WebDriver driver = driverProvider.get();
            driver.get(otherPageUrl.toExternalForm());
            final JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("window.open(arguments[0])", newPageUrl.toExternalForm());
            final Set<String> windowHandles = driver.getWindowHandles();
            checkState(windowHandles.size() > 1, "Failed to open additional window for driver: %s", driver);
            driverProvider.reset();
            return null;
        }
    });
    openWindowResultFuture.get();
}

From source file:io.selendroid.client.waiter.WaitingConditions.java

License:Apache License

public static Callable<Set<String>> windowHandleCountToBe(final WebDriver driver, final int count) {
    return new Callable<Set<String>>() {
        public Set<String> call() throws Exception {
            Set<String> handles = driver.getWindowHandles();

            if (handles.size() == count) {
                return handles;
            }// www .j  a v a  2 s  .c  om
            return null;
        }
    };
}