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 google or twitter option and close the google or twitter tab and return back to the my application.
Is there any way to close only newly opened tab.
I tried below solutions.
browser.close();
is closing the entire browser.
Can any one help me how to close only tab of the browser ( in my case google tab or twitter tab) with out closing the entire browser in protractor
window(handles[1]); browser. driver. close();
While doing stuff with selenium multiple browsers with multiple tabs will normally opens in order to close these tabs close() and quit() methods are used. close() method is used to close the current browser window on which the focus is set, on the other hand quit() method essentially calls the driver.
Handle simple Two Browser Windows / TabsClick on the Open New Window button, application open new window with google page. Make the protractor to sleep for 5 seconds; otherwise, it may not find the newly opened tab. 6. Switch to the window using switchTo().
switchTo(). window(windowHandler); await browser. close(); } } await browser.
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]);
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With