- metmanAmateur
- Posts : 58
Join date : 2010-04-02
Alert handling (Java)
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.
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.
- metmanAmateur
- Posts : 58
Join date : 2010-04-02
Re: Alert handling (Java)
Fri Apr 02, 2010 5:37 am
I'm looking at using:
if (selenium.isAlertPresent()) {
throw new Exception(selenium.getAlert());
}
Should that work correctly?
if (selenium.isAlertPresent()) {
throw new Exception(selenium.getAlert());
}
Should that work correctly?
- metmanAmateur
- Posts : 58
Join date : 2010-04-02
Re: Alert handling (Java)
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)
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
- susmithaActive particpant
- Posts : 15
Join date : 2011-10-21
Re: Alert handling (Java)
Fri Oct 21, 2011 8:07 pm
try this
assertEquals("AlertMessage", selenium.getAlert());
assertEquals("AlertMessage", selenium.getAlert());
Permissions in this forum:
You cannot reply to topics in this forum