How to distinguish between a new window and a new tab in Selenium

Viewed 82

I want to know if there is any way to distinguish between a new window and a new tab, natively in Selenium.

On my test page there are buttons to open a page in the same window, a new window, or a new tab.

I know how to switch to the new window or tab using window handles, but I cannot distinguish between the two. How do I know if the button opened the link in a new tab, and not a new window?

i.e. in pseudocode (need code for openedInNewWindow() and openedInNewTab())

click(newTabButton);
if (openedInSameWindow()) {
    Log.Error("Link opened in same window :(");
} else if (openedInNewWindow()) {
    Log.Error("Link opened in new window :(");
} else if (openedInNewTab()) {
    Log.Success("Link opened in new tab :)");
} else {
    Log.Error("Link didn't open anything :(");
}
1 Answers

Code snippet for new window,

 WebDriver newWindow = driver.switchTo().newWindow(WindowType.WINDOW);
    newWindow.get("https://blog.testproject.io/");
    System.out.println(driver.getTitle());

Code snippet for new tab,

 WebDriver newTab = driver.switchTo().newWindow(WindowType.TAB);
    newTab.get("https://testproject.io/platform/");
    System.out.println(driver.getTitle());

-Arjun

Related