0

I am trying to automate a scenario in which the User closes the browser's tab or the browser itself. The User is expected to be prompted with an alert when doing so along the lines of Are you sure you want to leave?

The alert is displayed when I manually close the tab, or the browser. However, when attempting to automate it via browser.close(), or browser.quit() or executing a script window.close(), the alert is entirely ignored and the browser shuts down.

Is there a way to simulate the action of a closing the browser's tab?

I'm using: Protractor / Google Chrome

Digitful
  • 63
  • 1
  • 8
  • What exception you are getting when you close browser manually? – Muzzamil Jan 18 '20 at 13:56
  • You want to close browser and show an alert with selenium? – Muzzamil Jan 18 '20 at 15:20
  • Webpage has a built in alert if user attempts to close tab/browser. What I want: attempt to close the tab/browser and interact with the alert What happens when I automate the action of browser.close/quit: browser shuts down without showing the alert – Digitful Jan 19 '20 at 00:35
  • @Muzzamil See above – Digitful Jan 19 '20 at 02:12
  • Once you will close the session then you can’t control alert. It can’t work with driver.quit() but it can work with driver.close() as then session will be active. Else we hv to check with multithread or ScriptEngineManager to handle alert. We can give it a try if you want. – Muzzamil Jan 19 '20 at 12:08

1 Answers1

0

Here are some things you can try: 1) You can open a new window handle and then switch to the old one and close that one.

let url = https://google.com;
return browser.executeScript("return window.open(arguments[0], '_blank')", url);
browser.getAllWindowHandles().then(function (handles) {
    browser.driver.switchTo().window(handles[0]);
    browser.driver.close();
    browser.driver.switchTo().window(handles[1]);
});

2) If the above command forces the browser to close, you should be able to target the cross button that closes the browser by using actions and moving the mouse cursor there.

3) You can try using the keyboard command for quitting the browser using sendKeys Control + q.

  • Thank you for your input. Sorry, if I hadn't made myself clear, but what I'm trying to do is: attempt to close the tab/browser, and be able to display AND interact with the alert. However, browser.close() or variants forcefully shut down the browser without giving the alert any chance to show up. I've tried using SendKeys, however, they appear to be unfunctional as of Chrome v.75+ https://github.com/angular/protractor/issues/5274 – Digitful Jan 19 '20 at 00:37
  • Is the alert element that you are trying to verify already in the DOM? Or does it appear only after the action? – Sankalan Parajuli Jan 19 '20 at 06:42
  • I do not understand. It's a built-in standard alert dialogue box that you see on Google Chrome. The alert does appear when I test the page manually by either attempting to close the browser or the tab. However, automating the action of closing the browser/tab does not give the alert a chance to appear? – Digitful Jan 19 '20 at 14:13