How to handle the alert that appears during closing the popup window.

1,083 views
Skip to first unread message

munee...@gmail.com

unread,
Feb 28, 2013, 9:21:37 AM2/28/13
to seleniu...@googlegroups.com
Hi All,

I am facing a problem.

There is a main window and a popup window. If I enter some invalid data in the popup window, an alert appears. What I have done is I have accepted the alert and I have tried to close the popup window, there the problem comes,  I have tried using Javascript window.close to  close the popup, but what happening is the same alert appears again. I could not able to handle the alert. I have used Javascript to close because using driver.close() is triggering  a (Yes and no alert) which driver is not able to handle.

I have used onbeforeunload also, but the alert about the invalid data still comes when I try to close the popupwindow..

Another question, I am using selenium server stand alone jar. What does the java client driver jar does actually? Do I need to use the java client driver jar also? I have tried to understand but I could not. A brief explanation would suffice.

Thank you.

ASP

unread,
Feb 28, 2013, 11:14:25 AM2/28/13
to seleniu...@googlegroups.com
I cant really visualize your problem code along with screenshot will be helpful

munee...@gmail.com

unread,
Feb 28, 2013, 12:22:59 PM2/28/13
to seleniu...@googlegroups.com
Hi ASP,

May be I was complicating the problem. I would like to know what needs to be done to suppress the alert which would occur when you try to close the window. How to handle Javascript's window onclose event using WebDriver?

Example: When I enter some invalid data in a text box in the popup window, due to the element level validation, an invalid data alert appears. What I need to do is I need fail the test case, and I have to close this popup so that I would let the next test case to run successfully. If I didn't handle this then all the test case which are after the test case will not run.

When I use ((JavascriptExecutor)driver).executeScript("window.close()");
//the next block of the code.

In the above when the first line gets executed, invalid data alert appears and the next block of the code does not get executed, it just hangs.

If I use the simple driver.close(), two alerts appear.

1. The webpage you are viewing is trying to close the window.(yes and no)

2. Invalid data alert

The code is hanging in both the cases.

Thanks,
Muneer.

ASP

unread,
Feb 28, 2013, 2:23:38 PM2/28/13
to seleniu...@googlegroups.com
Looks like you wann click ok cancel on alert box 

try this 

Alert alert = webDriver.switchTo().alert();

alert.accept();

or 

alert.dismiss();


ASP

munee...@gmail.com

unread,
Feb 28, 2013, 2:40:25 PM2/28/13
to seleniu...@googlegroups.com
I have tried it already and it is not working.

ASP

unread,
Feb 28, 2013, 3:28:41 PM2/28/13
to seleniu...@googlegroups.com
is it a public website ? or some site that mimics same thing u r doing ? 

munee...@gmail.com

unread,
Feb 28, 2013, 4:07:32 PM2/28/13
to seleniu...@googlegroups.com
If it was one, I would have shared the link, code and reproduction steps. :(

As of now I am doing a workaround. I am deleting the entered data in an element in the popup window which is causing the alert to appear. Once the data is deleted then no alert appears, I can simply close the popup window, fail the test case and run the next test case. :-)

Mike Riley

unread,
Mar 20, 2013, 10:18:17 PM3/20/13
to seleniu...@googlegroups.com
What does a user do to avoid it?  That is what your code should be doing.

Mike

Oleksandr Bavykin

unread,
Mar 21, 2013, 3:00:26 AM3/21/13
to seleniu...@googlegroups.com
Hi driverlearner,

So, let me summarize your problem:
- an invalid data was entered to field(s) on a pop-up window.
- field data validator fires when you try to close that pop-up
- accepting/dissmissing the validator alert closes that alert; pop-up is not closed
- when you try to close pop-up, field data validator fires again.

If I understand your problem correct, then I would suggest the following scenario:
1) perform your actions with a pop-up window.
2) try to close it
3) check, if it is closed
3.1) if not - fill in correct data or just clear all your fields (if it is valid combination); close pop-up
3.2) if it is closed - go to next step/test case

Selenium interact with a web page just like a user. So if user has no ability to close window without entering correct data, Selenium shouldn't be able also.

четверг, 28 февраля 2013 г., 16:21:37 UTC+2 пользователь driverlearner написал:

driverlearner

unread,
Mar 21, 2013, 3:04:32 AM3/21/13
to seleniu...@googlegroups.com
Hi,

Thanks for your reply. That is exactly what I had done as a workaround.

Madan Singh

unread,
Apr 1, 2013, 1:27:12 AM4/1/13
to seleniu...@googlegroups.com
Hi All,

Can any body help me, there is a login page on my application  here a
popup is appearing in some situation not regularly, Now I want to
handle this situation by C# and Web driver  and want to perform
further action, any help is appreciated.
1. If popup alert is present then click on OK and continue to step A.
2. If If popup alert is not present then also continue to step A


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/GyAC53YU3u4J.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
M P Singh
9971360313

Ripon Al Wasim

unread,
Apr 1, 2013, 1:41:38 AM4/1/13
to seleniu...@googlegroups.com
to accept the popup/alert using Java in WebDriver:
driver.switchTo().alert().accept();

to close/dismiss the popup/alert using Java in WebDriver:
driver.switchTo().alert().dismiss();



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/M4xeYZE1tWMJ.

Madan Singh

unread,
Apr 1, 2013, 3:21:35 AM4/1/13
to seleniu...@googlegroups.com
Hi Ripon,
Thanks for your quick response,

but I have already tried it , it is giving No alert Present Exception.

Ripon Al Wasim

unread,
Apr 1, 2013, 3:41:40 AM4/1/13
to seleniu...@googlegroups.com
Hi,
The presence of exception means, there is no alert present. So, you have to use a boolean check. The snippet of code is as below:
public boolean isAlertPresent(){
try{
driver.switchTo().alert();
return true;
}//try
catch(Exception e){
return false;
}//catch
}

Now, use the following code to accept or dismiss. You should use neither accept nor dismiss:

if(isAlertPresent() == true){
driver.switchTo().alert();
//to accept
driver.switchTo().alert().accept();
//or, to dismiss
driver.switchTo().alert().dismiss();
}

Madan Singh

unread,
Apr 1, 2013, 7:13:33 AM4/1/13
to seleniu...@googlegroups.com
Hi Ripon,

May u help me about pop up handling because on every time I facing same issue No Alert Found

Thanks & regard
Madan
Reply all
Reply to author
Forward
0 new messages