In the application i have two options "Google , Twitter" . When i click on any option it will open in new tab.
In my test case , i would like to click on go
You can try the following:
Switch to the new opened tab. Close the current windows (in this case, the new tab). Switch back to the first window.
browser.getAllWindowHandles().then((handles) => {
browser.driver.switchTo().window(handles[1]);
browser.driver.close();
browser.driver.switchTo().window(handles[0]);
});
I did and this and it works for me :
browser.getAllWindowHandles().then(function(handles) {
let newWindowHandle = handles[1]; // this is your new window
browser.switchTo().window(newWindowHandle).then(function() {
browser.close();
browser.switchTo().window(handles[0]);
});
});
This is how I used it in one project.
const allWindowHandlers = await browser.getAllWindowHandles();
if (allWindowHandlers.length > 1) {
for (let windowHandlerIndex = 1; windowHandlerIndex < allWindowHandlers.length; windowHandlerIndex++) {
const windowHandler = allWindowHandlers[windowHandlerIndex];
await browser.switchTo().window(windowHandler);
await browser.close();
}
}
await browser.switchTo().window(allWindowHandlers[0]);
You can use it inside your steps implementation.
You can use it before starting a new scenario, to be sure that only the first window is selected and everything else is closed.