I am am running some selenium tests(ruby) on my web page and as i enter an invalid characters in to a text box i have the JavaScript throw a alert like so
if(isNaN($(this).val()) || Number($(this).val().valueOf() 开发者_开发技巧<=0)){
alert("Please Enter A Number");
}
how can i handle this alert when its made and close the pop up?
i tried to use the wait_for_pop_up()
and close()
but i think that's only for browser pop up's and not JavaScript alerts.
any ideas?
thanks
The documentation isn't of much help but the Java Docs for Selenium RC seem to show that a getAlert() does exist here. Also this site lists these functions as well:
Processing with Selenium The following commands are available within Selenium for processing Alerts:
- getAlert()
- assertAlert() assertAlertNotPresent()
- assertAlertPresent() storeAlert()
- storeAlertPresent() verifyAlert()
- verifyAlertNotPresent()
- verifyAlertPresent() waitForAlert()
- waitForAlertNotPresent()
- waitForAlertPresent()
The …AlertPresent() and …AlertNotPresent() functions check for the existence or not of an alert – regardless of it’s content. The …Alert() functions allow the caller to specify a pattern which should be matched. The getAlert() method also exists in Selenium RC, and returns the text from the previous Alert displayed. Similar functions are also available for Confirmations
Selenium intercepts all JavaScript alert() calls (as well as confirm() and prompt()) and handles them itself. You can tell it what you want it to do, and you can find out what happened (excepting during onLoad, as Zugwalt says). You can call isAlertPresent() to check whether an alert was generated or not, and getAlert() to retrieve the text of it. Selenium will even queue them up for you, in case there is more than one (they're retrieved in order). If an alert is generated and you don't call getAlert() to retrieve it, the next Selenium operation will throw an exception. And if you call it when there isn't one, you'll get an exception.
For example (untested, and in C#, but you should get the point):
selenium.GetEval("window.alert('Hi, mom!'); window.alert('Bye, dad!')");
Assert.AreEqual("Hi, mom!", selenium.GetAlert());
Assert.AreEqual("Bye, dad!", selenium.GetAlert());
Assert.IsFalse(selenium.isAlertPresent());
精彩评论