OPTIMUS
OPTIMUS

Reputation: 692

How to verify whether new tab opened or not in selenium webdriver?

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.

Upvotes: 15

Views: 54588

Answers (5)

Avishka Perera
Avishka Perera

Reputation: 436

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.

Upvotes: 3

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//

Upvotes: 0

Rahul Rana
Rahul Rana

Reputation: 41

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

Upvotes: 0

moyeradon
moyeradon

Reputation: 453

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.

Upvotes: 22

Andrii
Andrii

Reputation: 357

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))

Upvotes: 10

Related Questions