Here is my JavaScript code embedded in an HTML file:
function CallMe(a,b){
alert("Hello");
var c = a + b;
return c;
}
Below is my Java code for Selenium WebDriver:
JavascriptExecutor executor = (JavascriptExecutor)driver;
Object result = null;
try{
result = executor.executeScript("return(CallMe(5,4))");
driver.switchTo().alert().accept();
}catch(NoAlertPresentException ex){
System.out.println("Alert not found");
}
driver.manage().timeouts().setScriptTimeout(30,TimeUnit.SECONDS);
System.out.println(result.toString());
When I run the Java program, it shows "Hello" as output if the alert box is present. However, if I remove the alert box, it displays "9" as expected result.
I'm curious why the remaining JavaScript statements are not executed when there's an alert box, even though I have accepted it in my Java code.
If anyone has an alternative solution or explanation, please share. Thank you!