I am testing my form and when I don't type needed data I get javascript alert in my web app that tells the user to enter missing data. I can't handle this with selenium because when I partially fill form and try to submit I get exception
org.openqa.selenium.UnhandledAlertException: Modal dialog present
If I catch exception the alert in webdriver is not shown. Is that any solution to solve this issue?I would like to be able to submit form and catch the alert. I am using Linux Mint,Firefox 18 and selenium 2.28.0 with java Best regards UPDATE I have following in my code
somePage.fillName(sth); //only 1 of 2 required field are filled
somgePage.submit(); //here js alert is shown right after clicking submit
somePage.getCurrentAlert();
//here are code parts
public Alert getCurrentAlert(){
return driver.switchTo().alert();
}
public AdminHome submit(){
saveUrl();
WebElement submit = driver.findElement(By.id("add_quiz_submit_button"));
try{
submit.click();
if(urlChanged()){
return new AdminHome(driver);
}
}
catch(Exception e){
e.printStackTrace();// exception 1
return null;
}
return null;
}
//Exception 1
org.openqa.selenium.UnhandledAlertException: Modal dialog present
//The test fails because of:
org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)
However if I click manual on submit the test work as expected. Thanks in advance
I am testing my form and when I don't type needed data I get javascript alert in my web app that tells the user to enter missing data. I can't handle this with selenium because when I partially fill form and try to submit I get exception
org.openqa.selenium.UnhandledAlertException: Modal dialog present
If I catch exception the alert in webdriver is not shown. Is that any solution to solve this issue?I would like to be able to submit form and catch the alert. I am using Linux Mint,Firefox 18 and selenium 2.28.0 with java Best regards UPDATE I have following in my code
somePage.fillName(sth); //only 1 of 2 required field are filled
somgePage.submit(); //here js alert is shown right after clicking submit
somePage.getCurrentAlert();
//here are code parts
public Alert getCurrentAlert(){
return driver.switchTo().alert();
}
public AdminHome submit(){
saveUrl();
WebElement submit = driver.findElement(By.id("add_quiz_submit_button"));
try{
submit.click();
if(urlChanged()){
return new AdminHome(driver);
}
}
catch(Exception e){
e.printStackTrace();// exception 1
return null;
}
return null;
}
//Exception 1
org.openqa.selenium.UnhandledAlertException: Modal dialog present
//The test fails because of:
org.openqa.selenium.NoAlertPresentException: No alert is present (WARNING: The server did not provide any stacktrace information)
However if I click manual on submit the test work as expected. Thanks in advance
Share Improve this question edited Jan 28, 2013 at 9:24 John asked Jan 27, 2013 at 17:01 JohnJohn 1,3606 gold badges28 silver badges51 bronze badges 3-
All test scenarios I tried work well using the usual way:
driver.switchTo().alert().accept();
after the click on the Submit button. Could you please post a short page example where this fails to work? – Petr Janeček Commented Jan 27, 2013 at 21:26 - What's the code in urlChanged()? – niharika_neo Commented Feb 27, 2013 at 9:22
- @Artur If you are trying to switch the driver to an alert which isn't exist then driver throws UnhandledAlertException. Are you sure that in the case you are talking about, alert appears each time? If so, wait until the alert is present then accept or dismiss it? – Code Enthusiastic Commented Mar 19, 2013 at 16:20
3 Answers
Reset to default 1you should handle the alert as soon as the action is done and there shouldn't be any other action before handling the alert.
for instance your code should be
try{
submit.click();
if (alertPresent())
getCurrentAlert();
if(urlChanged()){
return new AdminHome(driver);
}
}
This will check alert and then accept the alert. The interaction of webdriver is more similar to the action we interact with manually with browser. So when the click on submit is done we will be able to see alert and no actions can be done until accept or reject it.
Vishal
It is because driver accepts the alert itself when the UnhandledAlertException is thrown. How can you submit the form if you have filled it partially?
If it is even possible, just catch that exception, and in catch block write the line which clicks on the submit button.
Use Robot class (Press enter) to close modal dialog box
try {
(new Robot()).keyPress(java.awt.event.KeyEvent.VK_ENTER);
(new Robot()).keyRelease(java.awt.event.KeyEvent.VK_ENTER);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248773a4618548.html
评论列表(0条)