Reputation: 13
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 :(");
}
Upvotes: 1
Views: 207
Reputation: 495
I also couldn't find any way to do this. But if your new window has different dimensions than current window, it's easy to detect it.
// size of current active window
const originalSize = JSON.stringify(await driver.manage().window().getRect());
// open new window/tab and switch to new windowHandle
await driver.switchTo().window(windowHandle);
// get size of new window
const size = await driver.executeScript(function () {
return { x: window.screenX, y: window.screenY, height: window.innerHeight, width: window.innerWidth };
});
/**
* now you can compare the size with originalSize
* If they're same - it's a new tab, otherwise new window
*/
But please keep in mind, this only works well if new window is supposed to be of different dimension.
Upvotes: 0
Reputation: 634
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
Upvotes: 1