How can I close a browser window with

Can anyone please provide me some valueable information on how can I close a browser window with

You can always use browser.close() to close the current instance of the browser, once your tests have been completed. If you want to close the tests altogether, you can use the browser.quit() method.

1 Like

Yeah I know these. But I wanted to know how to close the browser window, without ending the tests. I mean its a part of my test script to close the window.

In that case, you can use the closewindow() method. This method is used to close the current window instance, without ending the test, as you want. I think it will help you.

Here’s the syntax:

1 Like

Yeah that’s what I am looking for. Can you give me a working example of the same for reference?

1 Like

Sure. Here you go:

describe("Browser Commands ", function() {
       it("closeWindow  example ", function() {

                       "Google Window",
               console.log(browser.getTitle()); // print google title

The above code opens url and then close all opened browsers.

close all opened browsers!!!

Do you mean using this will close the complete browser window instead of current tab?

Yes. The browser.closewindow() method in closes the current browser window and all the tabs in it.

For example, if you have a browser with opened in first tab, opened in second tab and in third.

Then using this method will close the complete window and the above three tabs with it.

But what if I want to close just the current tab and not the others in the current browser window?

You can use the close() method for that case instead. The browser.close() method will help you close the current tab only, and not the complete browser window.

Suppose I am using 3 tabs, Tab1, Tab2, and Tab3. If I use close() method, which tab will the focus go when the current tab is closed?

This method closes current window (and focus on an other window). If no window handle is given it automatically switches back to the first handle.




Param Type Details
optional String new window to focus on

Source: WebdriverIO - close

1 Like

Ok. Thanks for the help. Will try it out.

1 Like

Sure. Do let me know if you face any difficulty.


I tried using this. But its showing me this Do you want to close this window prompt. My tests failed due to unexpected arrival of this prompt.

Is this expected? How to deal with this?

Hi Rebecca,

Yes it is an expected behavior. It will prompt you for confirmation.

Here are two things that you can try:

  1. Instead of closing the tab, try to switch tabs instead. Closing the tab can result in many unexpected behaviour and errors. You can prevent all of it by just switching to the required tab and close the session altogether.

  2. Second thing, that you can do is more of a hacks like'', '_self', ''); window.close();

Why a hack and not a solution for this? What if the hack doesn’t work?

As rvighne explained in this answer:

Scripts are not allowed to close a window that a user opened. This is considered a security risk. Though it isn’t in any standard, all browser vendors follow this (Mozilla docs). If this happens in some browsers, it’s a security bug that (ideally) gets patched very quickly.

None of the hacks in the answers on this question work any longer, and if someone would come up with another dirty hack, eventually it will stop working as well.

I suggest you don’t waste energy fighting this and embrace the method that the browser so helpfully gives you — ask the user before you seemingly crash their page.

So to prevent any kind of security breach, no solution exists for this. And the hacks don’t work after a period of time.

1 Like

ok. So I shouldn’t try to close a tab?

Yes. You can try to just switch tabs, execute test and then close the session. That’s optimal way to do this.

1 Like