Tuesday Tip Day – Dealing With Tabs

Tuesday Tip Day – Dealing With Tabs

Pixel Ratio Confusion

When writing your automated tests for websites, you will some times have to deal with opening a new tab when clicking links or performing certain tasks. If you’re using Selenium, then it’s quite easy to think that because WebDriver only drives a single browser window, that it will also fail to deal with multiple tabs. But while it does mean writing some extra code, your Selenium tests can definitely look at and interact with tabs.

public bool NavigateToNewTab(string tabTitle)
{
    var tabHandles = driver.WindowHandles;
    var currentTab = tabHandles.First();
    var newTab = tabHandles.Last();
    
    driver.SwitchTo().Window(newTab);
    
    return driver.Title.Equals(tabTitle);
}

The above example is extremely simple, but it will handle switching to a new tab. This code assumes you have clicked a link or button already that has opened the new tab. It will also verify that the new tab has been properly switched too and return a true or false Boolean depending on the result