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:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_guides_button_b2b(WebDriver driver) {

    driver.findElement(By.cssSelector("div[class='demos-guides-link'] button[class='demos-guides-button']"))
            .click();/*from   w  w  w  .ja v a  2s.  c  o  m*/
    String user_guide = driver.findElement(By.cssSelector(
            "li[class='link-button user-guide'] a[href='https://www.iwdagency.com/help/m2-b2b-suite/b2b-suite-2-settings']"))
            .getText();
    System.out.println(user_guide);
    String expected_user_guide = "User Guide";
    assertEquals(user_guide, expected_user_guide);
    System.out.println("user_guide is correct");

    String installation_guide = driver.findElement(By.cssSelector(
            "li[class='link-button install-guide'] a[href='https://www.iwdagency.com/help/general-information/installing-iwd-extensions']"))
            .getText();
    System.out.println(installation_guide);
    String expected_installation_guide = "Installation Guide";
    assertEquals(installation_guide, expected_installation_guide);
    System.out.println("installation_guide is correct");
    String homePage = driver.getWindowHandle();

    driver.findElement(By.cssSelector(
            "li[class='link-button user-guide'] a[href='https://www.iwdagency.com/help/m2-b2b-suite/b2b-suite-2-settings']"))
            .click();

    Set<String> windows = driver.getWindowHandles();
    //System.out.println(windows.size());

    Iterator iterator = windows.iterator();
    String currentWindowId;

    while (iterator.hasNext()) {
        currentWindowId = iterator.next().toString();
        //System.out.println(currentWindowId);

        if (!currentWindowId.equals(homePage)) {
            driver.switchTo().window(currentWindowId);

            String actualTitle = driver.getTitle();
            System.out.println(actualTitle);
            String expectedTitle = "[M2] B2B Suite Settings - IWD Agency Support Center";
            assertEquals(actualTitle, expectedTitle);
            System.out.println("Title of page is correct");

            String b2b_suite_settings = driver.findElement(By.cssSelector(".header-title")).getText();
            System.out.println(b2b_suite_settings);
            String expected_b2b_suite_settings = "[M2] B2B Suite Settings";
            assertEquals(b2b_suite_settings, expected_b2b_suite_settings);
            System.out.println("[M2] B2B Suite Settings is correct");

            driver.findElement(By.cssSelector("a[href='#DefaultSettings']"));
            driver.findElement(By.cssSelector("a[href='#Access']"));
            driver.findElement(By.cssSelector("a[href='#Registration']"));
            driver.findElement(By.cssSelector("a[href='#Requester']"));
            driver.findElement(By.cssSelector("a[href='#Notifications']"));
            driver.findElement(By.cssSelector("a[href='#Tables']"));
            driver.findElement(By.cssSelector("a[href='#Download']"));
            driver.findElement(By.cssSelector("a[href='#Order']"));
            driver.findElement(By.cssSelector("a[href='#Account']"));
            driver.findElement(By.cssSelector("a[href='#Credit']"));
            driver.findElement(By.cssSelector("a[href='#Guest']"));
            driver.findElement(By.cssSelector("a[href='#Matrix']"));

            driver.close();
            driver.switchTo().window(homePage);
            driver.findElement(By.cssSelector("div[class=\"md-content es-wrapper\"] a[class=\"close\"]"))
                    .click();
            System.out.println("Test Verify_guides_button_b2b is done");

        }
    }
}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_installation_guide_button_b2b(WebDriver driver) {

    String homePage2 = driver.getWindowHandle();

    driver.findElement(By.cssSelector("div[class='demos-guides-link'] button[class='demos-guides-button']"))
            .click();/* ww w  .j  ava 2 s  .co  m*/
    driver.findElement(By.cssSelector(
            "li[class='link-button install-guide'] a[href='https://www.iwdagency.com/help/general-information/installing-iwd-extensions']"))
            .click();
    Set<String> windows2 = driver.getWindowHandles();
    //System.out.println(windows.size());

    Iterator iterator2 = windows2.iterator();
    String currentWindowId2;

    while (iterator2.hasNext()) {
        currentWindowId2 = iterator2.next().toString();
        //System.out.println(currentWindowId);

        if (!currentWindowId2.equals(homePage2)) {
            driver.switchTo().window(currentWindowId2);

            String actualtitle_Installing = driver.getTitle();
            System.out.println(actualtitle_Installing);
            String expectedtitle_Installing = "Installing IWD Extensions - IWD Agency Support Center";
            assertEquals(actualtitle_Installing, expectedtitle_Installing);
            System.out.println("Title of page Installing is correct");

            String installing_iwd_extensions = driver.findElement(By.cssSelector(".header-title")).getText();
            System.out.println(installing_iwd_extensions);
            String expected_installing_iwd_extensions = "Installing IWD Extensions";
            assertEquals(installing_iwd_extensions, expected_installing_iwd_extensions);
            System.out.println("Installing IWD Extensions is correct");

            driver.findElement(By.cssSelector("a[href='#MAGENTO 1 INSTALLATION']"));
            driver.findElement(By.cssSelector("a[href='#MAGENTO 2 INSTALLATION']"));
            driver.findElement(By.cssSelector("a[href='#ACTIVATING LICENSES']"));
            driver.findElement(By.cssSelector("a[href='/extensions/media/modules/m2/iwd_all.zip']"));
            driver.findElement(By.cssSelector("a[href='/extensions/downloadable/customer/products']"));
            driver.findElement(By
                    .cssSelector("a[href='https://iwdagency.com/help/general-information/installing-iwd-all"));

            driver.close();
            driver.switchTo().window(homePage2);
            driver.findElement(By.cssSelector("div[class=\"md-content es-wrapper\"] a[class=\"close\"]"))
                    .click();

        }
    }
}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_partner_program(WebDriver driver) {

    String homePage = driver.getWindowHandle();
    footer.Click_on_partner_program(driver);
    System.out.println("click on partner_program is done");
    Set<String> windows = driver.getWindowHandles();

    Iterator iterator = windows.iterator();
    String currentWindowId;//from w ww  .ja  v a2 s .c  om

    while (iterator.hasNext()) {
        currentWindowId = iterator.next().toString();
        if (!currentWindowId.equals(homePage)) {
            driver.switchTo().window(currentWindowId);
        }
    }

    String actualTitle = driver.getTitle();
    System.out.println(actualTitle);
    String expectedTitle = "IWD_Partner_Program.pdf";
    assertEquals(expectedTitle, actualTitle);
    System.out.println("Title is correct");

    String currentURL = driver.getCurrentUrl();
    System.out.println(currentURL);
    String expectedcurrentURL = "https://www.iwdagency.com/extensions/IWD_Partner_Program.pdf";

    assertEquals(currentURL, expectedcurrentURL);
    System.out.println("Link is correct https://www.iwdagency.com/extensions/IWD_Partner_Program.pdf");

    driver.findElement(By.cssSelector(".textLayer"));
    System.out.println("PDF was found");

    driver.close();
    driver.switchTo().window(homePage);

}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_facebook(WebDriver driver) {

    String homePage = driver.getWindowHandle();
    footer.Click_on_facebook(driver);/* w w  w  .  j  ava  2 s.c  o m*/
    Set<String> windows = driver.getWindowHandles();

    Iterator iterator = windows.iterator();
    String currentWindowId;

    while (iterator.hasNext()) {
        currentWindowId = iterator.next().toString();
        if (!currentWindowId.equals(homePage)) {
            driver.switchTo().window(currentWindowId);
        }
    }

    String actualTitle = driver.getTitle();
    System.out.println(actualTitle);
    String expectedTitle = "IWD Agency | Facebook";
    assertEquals(expectedTitle, actualTitle);
    System.out.println("Title is correct");

    String currentURL = driver.getCurrentUrl();
    System.out.println(currentURL);
    String expectedcurrentURL = "https://www.facebook.com/iwdagency";
    assertEquals(currentURL, expectedcurrentURL);
    System.out.println("Link is correct https://www.facebook.com/iwdagency");

    driver.close();
    driver.switchTo().window(homePage);

}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_google(WebDriver driver) {

    String homePage = driver.getWindowHandle();
    footer.Click_on_google(driver);/*w  ww .  j  av  a2  s .  co m*/
    Set<String> windows = driver.getWindowHandles();

    Iterator iterator = windows.iterator();
    String currentWindowId;

    while (iterator.hasNext()) {
        currentWindowId = iterator.next().toString();
        if (!currentWindowId.equals(homePage)) {
            driver.switchTo().window(currentWindowId);
        }
    }

    String actualTitle = driver.getTitle();
    System.out.println(actualTitle);
    String expectedTitle = "IWD Agency - Google+";
    assertEquals(expectedTitle, actualTitle);
    System.out.println("Title is correct");

    String currentURL = driver.getCurrentUrl();
    System.out.println(currentURL);
    String expectedcurrentURL = "https://plus.google.com/+Interiorwebdesign";
    assertEquals(currentURL, expectedcurrentURL);
    System.out.println("Link is correct https://plus.google.com/+Interiorwebdesign");

    driver.close();
    driver.switchTo().window(homePage);

}

From source file:IWDmainsiteProject.PageObjects.Verify_Pages.java

public static void Verify_reviews(WebDriver driver) {

    String homePage = driver.getWindowHandle();
    footer.Click_on_reviews(driver);/*w  w  w. j a v a 2s . com*/
    Set<String> windows = driver.getWindowHandles();

    Iterator iterator = windows.iterator();
    String currentWindowId;

    while (iterator.hasNext()) {
        currentWindowId = iterator.next().toString();
        if (!currentWindowId.equals(homePage)) {
            driver.switchTo().window(currentWindowId);
        }
    }
    String actualTitle = driver.getTitle();
    System.out.println(actualTitle);
    String expectedTitle = "Customer Reviews";
    assertEquals(expectedTitle, actualTitle);
    System.out.println("Title is correct");

    String currentURL = driver.getCurrentUrl();
    System.out.println(currentURL);
    String expectedcurrentURL = "https://www.iwdagency.com/reviews/";
    assertEquals(currentURL, expectedcurrentURL);
    System.out.println("Link is correct https://www.iwdagency.com/reviews/");

    driver.findElement(By.xpath("//p[contains(.,'IWD Agency Customer Reviews:')]"));
    driver.findElement(By.xpath("//span[contains(.,'Nothing but amazing!')]"));
    driver.findElement(By.xpath("//span[contains(.,'Stephanie Moody')]"));

    //Leave a review
    driver.findElement(
            By.cssSelector(".vc_general.vc_btn3.vc_btn3-size-lg.vc_btn3-shape-square.vc_btn3-style-custom"))
            .click();

    String actual_leave_a_review = driver.findElement(By.cssSelector(".rwp-ratings-form-label")).getText();
    System.out.println(actual_leave_a_review);
    String expected_leave_a_reviewe = "LEAVE A REVIEW";
    assertEquals(expected_leave_a_reviewe, actual_leave_a_review);
    System.out.println("Leave a Review title is correct");

    driver.findElement(
            By.cssSelector("div[class=\"rwp-rating-form-content rwp-no-avatar\"] input[placeholder=\"Name\"]"))
            .sendKeys("IWD Test Name");
    driver.findElement(By.cssSelector(
            "div[class=\"rwp-rating-form-content rwp-no-avatar\"] input[placeholder=\"Review Title\"]"))
            .sendKeys("IWD Test Review Title");
    driver.findElement(By.cssSelector(
            "div[class=\"rwp-rating-form-content rwp-no-avatar\"] textarea[placeholder=\"Write your review\"]"))
            .sendKeys("IWD Test Write your review");

    String actual_rating = driver.findElement(By.cssSelector(".rwp-lab")).getText();
    System.out.println(actual_rating);
    String expected_rating = "Rating:";
    assertEquals(expected_rating, actual_rating);
    System.out.println("Rating: is correct");

    driver.findElement(By.cssSelector(
            "div[class=\"rwp-stars\"] label[for=\"rwp-rating-1961-4b35bc789a120e0b49c7fcfba6d71488-9-141470591\"]"))
            .click();

    //Make click for submit
    driver.findElement(By.cssSelector(".rwp-submit-wrap>input"));

}

From source file:jp.vmi.selenium.selenese.subcommand.GetAllWindowTitles.java

License:Apache License

@Override
public String[] execute(Context context, String... args) {
    WebDriver driver = context.getWrappedDriver();
    String current = driver.getWindowHandle();
    try {/* ww  w .j a  va2s. co  m*/
        return driver.getWindowHandles().stream().map(handle -> {
            driver.switchTo().window(handle);
            return driver.getTitle();
        }).toArray(String[]::new);
    } finally {
        driver.switchTo().window(current);
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.GetAttributeFromAllWindows.java

License:Apache License

@Override
public Object[] execute(Context context, String... args) {
    String attrName = args[ARG_ATTR_NAME];
    WebDriver driver = context.getWrappedDriver();
    String current = driver.getWindowHandle();
    try {/*from w w w. j  av a 2 s .  c  o  m*/
        return driver.getWindowHandles().stream().map(handle -> {
            driver.switchTo().window(handle);
            return ((JavascriptExecutor) driver).executeScript("return window[arguments[0]];", attrName);
        }).toArray();
    } finally {
        driver.switchTo().window(current);
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.SeleneseRunnerWindows.java

License:Apache License

@Override
public void selectPopUp(WebDriver driver, String windowID) {
    if ("null".equals(windowID) || "".equals(windowID)) {
        Set<String> windowHandles = driver.getWindowHandles();
        windowHandles.remove(context.getInitialWindowHandle());
        if (windowHandles.size() > 0) {
            driver.switchTo().window(windowHandles.iterator().next());
        } else {//from   w  w w .j a v a 2 s .  c  o m
            throw new SeleniumException("Unable to find a popup window");
        }
    } else {
        selectWindow(driver, windowID);
    }
}

From source file:jp.vmi.selenium.selenese.subcommand.SeleneseRunnerWindows.java

License:Apache License

/**
 * Selects the only <code>_blank</code> window. A window open with <code>target='_blank'</code>
 * will have a <code>window.name = null</code>.
 * <br>//from  w  ww . j  a va  2  s.c  o m
 * <p>
 * This method assumes that there will only be one single <code>_blank</code> window and selects
 * the first one with no name. Therefore if for any reasons there are multiple windows with
 * <code>window.name = null</code> the first found one will be selected.
 * </p>
 * <p>
 * If none of the windows have <code>window.name = null</code> the last selected one will be
 * re-selected and a {@link SeleniumException} will be thrown.
 * </p>
 *
 * @throws NoSuchWindowException if no window with <code>window.name = null</code> is found.
 */
@Override
public void selectBlankWindow(WebDriver driver) {
    String current = driver.getWindowHandle();
    // Find the first window without a "name" attribute
    List<String> handles = new ArrayList<String>(driver.getWindowHandles());
    for (String handle : handles) {
        // the original window will never be a _blank window, so don't even look at it
        // this is also important to skip, because the original/root window won't have
        // a name either, so if we didn't know better we might think it's a _blank popup!
        if (handle.equals(context.getInitialWindowHandle())) {
            continue;
        }
        driver.switchTo().window(handle);
        String value = (String) ((JavascriptExecutor) driver).executeScript("return window.name;");
        if (value == null || "".equals(value)) {
            // We found it!
            return;
        }
    }
    // We couldn't find it
    driver.switchTo().window(current);
    throw new SeleniumException("Unable to select window _blank");
}