1

I have one test case where after login, on some page when user tries to close the browser, it will show popup windows(alert) asking "you might lose the data, are you sure you want to continue?', with two options:

  1. Leave the page
  2. Stay on page

Clicking on specific option, the page will perform action.

'Stay on page' will not leave the page and Leave the page will close the browser.

Now when I try to close the browser, it doesn't ask me for Popup

webdriver.close() 

closes the browser before.

How can I Accept/Reject popup and then based on action, it should close the browser?

Christian Garbin
  • 2,512
  • 1
  • 23
  • 31
Chandni
  • 13
  • 1
  • 4
  • webdriver.close(); Alert alert = getKeywordContext().switchTo.alert(); alert.dismiss() I tried but didn't work – Chandni Mar 14 '15 at 21:00
  • Does that mean that you are not sure if alert exists or not? Because you can use `alert.Accept()` then `driver.Quit();` to completely close and quit the browser. – Saifur Mar 14 '15 at 21:04
  • I have one test case where after login,On some page when user tries to close the browser, it will show popup windows(alert) asking "you might lose the data, are you sure you want to continue?', with two options : 1)Leave the page 2) Stay on Page. And Clicking on specific option, the page will perform action. 'Stay on page' will not leave the page and 'Leave the page' will close the browser. Now when I tries to close the browser, it doesn't ask me for Popup – Chandni Mar 14 '15 at 21:05
  • But Popup will not appear in that case right?. When User tries to click Close button of browser then only pop up will appear so don't I need to click on Close button first which will show popup and user can accept/reject popup. – Chandni Mar 14 '15 at 21:06
  • If that's a window you can use `driver.switchTo()` to switch focus to new window and perform any action you want. Why don't you do that? – Saifur Mar 14 '15 at 21:07
  • Sorry I tried this code : driver.close(); Alert alert =getKeywordContext.getWebdriver().switchTo().alert(); but didn't work. – Chandni Mar 14 '15 at 21:12
  • According to your description I think the pop up isn't an alert that's a window. If so try [this](http://stackoverflow.com/questions/9588827/webdriver-switch-to-new-browser-opened-after-click-on-button) – Saifur Mar 14 '15 at 21:13
  • But in the same window its opens as small confirmation asking Accept/Reject. so it seems to be alert. Can you please still suggest something for window – Chandni Mar 14 '15 at 21:27
  • I am really confused. can you not use `alert.accept()` or `reject()` then? – Saifur Mar 14 '15 at 21:33
  • I am having too much trouble in getting answer for that question.Can anyone please let me know about chat options. – Chandni Mar 14 '15 at 22:01
  • You can not chat unless you have enough reputation. To be honest with you, the question is still unclear. My guess is the scenario would be pretty simple to handle if you clarify the question more – Saifur Mar 14 '15 at 22:06
  • Even I think so. You are right. question is quite simple but unable to get rid of it. Ok let me tell you in simple language I have one page where when you tries to click on Close button of chrome browser, it will ask you 'Leaving this window may interrupt your session. Are you sure you want to leave this page?' with two buttons 1)Leave the Page 2)Stay on Page. Now I want to click on 'Leave the page'. – Chandni Mar 14 '15 at 22:14
  • I got your point driver.switchTo() option but first I need to click on Close button of browser which will ask me this confirmation. I tried calling driver.close() first followed by switch() but didn't work. – Chandni Mar 14 '15 at 22:14
  • Please clarify when the alert pops up? When you click **X** of browser window to close the browser? and, what language are you using `Java` or `Groovy`? – Saifur Mar 14 '15 at 22:27
  • Yes correct. when you click X of browsser window to close the browser. using java. – Chandni Mar 14 '15 at 22:28
  • And, you are using `Java`? – Saifur Mar 14 '15 at 22:30
  • Yes I am using java but groovy also works fine for me. any of the language solution. – Chandni Mar 14 '15 at 22:35

1 Answers1

1

If I am understanding the problem correctly, then you are trying to perform a click on X to close the browser window which generates additional pop up. If that's the case, you can try executing some JavaScript action to recreate the scenario

(( JavascriptExecutor ) webdriver).executeScript( "window.close()" );

instead of webdriver.close()

Note: Written in Java

More info: With the syntax above you can only close the child tab not the entire browser only IF it is invoked with window.open()

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • Ok great! this closed my browser but without asking for popup. So according to you do I need to add poup in this JavascriptExecutor Only?. Please help me – Chandni Mar 14 '15 at 22:46
  • I am not sure how that `JavaScript` alert is placed there. It's hard to determine anything without knowing the functionality. Is there any **other** way to do that? – Saifur Mar 14 '15 at 22:49
  • Can last question. This code seems to be worked for me ((JavascriptExecutor) wedriver).executeScript("window.close(), window.alert = function(msg) {return false; }"); Can you please let me know what is message in this syntax – Chandni Mar 14 '15 at 23:09
  • `msg` is just an *argument* nothing else. I am glad you found something that works out. Please accept and upvote the answer if it helps you – Saifur Mar 14 '15 at 23:14