15

I have a situation where I have to check whether after clicking on a link a new tab is opened or not. If the tab opens I want to check the title as well.

Does anyone have any idea about this.

Joern Boegeholz
  • 533
  • 1
  • 8
  • 25
OPTIMUS
  • 672
  • 4
  • 14
  • 29

5 Answers5

22

Here is the same for C#; this is tested and will work whether the link opens in new tab or new window.

var browserTabs = driver.WindowHandles;            
driver.SwitchTo().Window(browserTabs[1]);

//check is it correct page opened or not (e.g. check page's title or url, etc.)
// ...
//close tab and get back
driver.Close();
driver.SwitchTo().Window(browserTabs[0]);

Hope this helps anyone who comes across this thread.

moyeradon
  • 443
  • 5
  • 13
10

Try to switch to a new tab and then verify whether is it correct page or not.

In Java it can be look like:

//get window handlers as list
List<String> browserTabs = new ArrayList<String> (driver.getWindowHandles());
//switch to new tab
driver.switchTo().window(browserTabs .get(1));
//check is it correct page opened or not (e.g. check page's title)
//...
//then close tab and get back
driver.close();
driver.switchTo().window(browserTabs.get(0))
Andrii
  • 357
  • 2
  • 9
  • Thanks for solution but it is not working for me. It is giving browsertab.size =1 even after waiting for one minute. – OPTIMUS Apr 23 '14 at 06:35
  • My mistake. I posted code without testing it. Try solution from 2-nd answer (provided by Jordan Silva) there http://stackoverflow.com/questions/12729265/switch-tabs-using-selenium-webdriver It works for me. – Andrii Apr 23 '14 at 16:43
  • Thanks for solution i will try and let you know if i am having any problem – OPTIMUS Apr 24 '14 at 04:45
3

You can use below method to implement the desired wait until the tab is fully loaded.

  public static void switchTabs(WebDriver driver, int expectedWindowsCount,int SwitchtoWindow) throws Exception {
    (new WebDriverWait(driver, 30)).until(ExpectedConditions.numberOfWindowsToBe(expectedWindowsCount));
    ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
    driver.switchTo().window(tabs2.get(SwitchtoWindow));
}

The method will check how many Active window handlers are available and wait until desired handlers are present and switch to the tab afterwards.

Avishka Perera
  • 416
  • 1
  • 5
  • 15
0

Use wait for no of tabs to open to make sure a new tab is opened

(new WebDriverWait(driver, 30)).until(ExpectedConditions.numberOfWindowsToBe(2));

Then switch to the new tab

protected void switchTabsUsingPartOfUrl(String platform) {
    String currentHandle = null;
    try {
        final Set<String> handles = driver.getWindowHandles();
        if (handles.size() > 1) {
            currentHandle = driver.getWindowHandle();
        }
        if (currentHandle != null) {
            for (final String handle : handles) {
                driver.switchTo().window(handle);
                if (currentUrl().contains(platform) && !currentHandle.equals(handle)) {
                    break;
                }
            }
        } else {
            for (final String handle : handles) {
                driver.switchTo().window(handle);
                if (currentUrl().contains(platform)) {
                    break;
                }
            }
        }
    } catch (Exception e) {
        System.out.println("Switching tabs failed");
    }
}

Call this method and pass parameter a substring of url of the tab you want to switch to

driver.getTitle().equals("Title");

Then verify page title

Rahul Rana
  • 41
  • 6
0

This would help you if you are doing test automation in Groovy

def browserTabs = driver.getWindowHandles()
driver.switchTo().window(browserTabs[1])
assert //Validate your new page title//