Share
Go down
metman
Amateur
Amateur
Posts : 58
Join date : 2010-04-02

Alert handling (Java)

on Fri Apr 02, 2010 3:06 am
Hello, I've been working with Selenium in Java, and have come across a question for which the JavaDocs are wanting. For the getAlert() method, the docs say that if an alert is not detected, the method "fails". What is the failure behavior? Does it assert? Throw an exception? What?

I'm looking to write some code that enters data in to a form (credit card info), and on that form, if the user attempts to enter, for instance, characters in to the credit card number field, the page throws an Alert. As a means of illustration, let me give a snippet:

public void add(Selenium selenium) throws Exception {
(...)
selenium.type("creditCardNumber", cardNumber);
***need logic to handle possible Alert here**
throw new Exception(alertMessage);
(...)
}

The basic idea is that this method adds a credit card through a form, and I want it to bubble up any Alerts it generates through exceptions to the calling method. As mentioned, I think getAlert() is the way to go, but any further elaboration anyone could provide would be appreciated.
metman
Amateur
Amateur
Posts : 58
Join date : 2010-04-02

Re: Alert handling (Java)

on Fri Apr 02, 2010 5:37 am
I'm looking at using:

if (selenium.isAlertPresent()) {
throw new Exception(selenium.getAlert());
}

Should that work correctly?
metman
Amateur
Amateur
Posts : 58
Join date : 2010-04-02

Re: Alert handling (Java)

on Sat Apr 17, 2010 7:07 am
Should anyone come across this thread with a similar problem, yes, this works. The only thing for me was I had to use the selenium.typeKeys() method as opposed to selenium.type() because the alert box was triggered by keyUp events.
tejastrivedi
Posts : 3
Join date : 2011-09-02

Re: Alert handling (Java)

on Sat Sep 17, 2011 12:26 am
hi all in my application i have alert message on page load. i want to handle ok button click automatiocally pls guide
susmitha
Active particpant
Active particpant
Posts : 15
Join date : 2011-10-21

Re: Alert handling (Java)

on Fri Oct 21, 2011 8:07 pm
try this
assertEquals("AlertMessage", selenium.getAlert());
Sponsored content

Re: Alert handling (Java)

Back to top
Permissions in this forum:
You cannot reply to topics in this forum