Can anyone please provide me some valueable information on how can I close a browser window with WebDriver.io.
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.
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:
browser.closewindow();
Yeah that’s what I am looking for. Can you give me a working example of the same for reference?
Sure. Here you go:
describe("Browser Commands ", function() {
it("closeWindow example ", function() {
browser.url("https://www.lambdatest.com/");
browser.newWindow(
"https://www.google.com/",
"Google Window",
"width=800,height=700,resizable,scrollbars=yes,status=1"
);
browser.pause(5000);
console.log(browser.getTitle()); // print google title
browser.pause(5000);
browser.closeWindow();
});
});
The above code opens url https://www.lambdatest.com/
and https://www.google.com/
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 WebDriver.io closes the current browser window and all the tabs in it.
For example, if you have a browser with google.com opened in first tab, lambdatest.com opened in second tab and community.lambdatest.com 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.
Usage
browser.close([windowHandle]);
Parameters
Param | Type | Details |
---|---|---|
windowHandle | ||
optional | String | new window to focus on |
Source: WebdriverIO - close
Ok. Thanks for the help. Will try it out.
Sure. Do let me know if you face any difficulty.
Hi
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:
-
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.
-
Second thing, that you can do is more of a hacks like
window.open('', '_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.
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.