Is it possible to return a value from a Javascript confirm dialog via Selenium JavascriptExecutor ? so the test code can determine if tester clicks OK or Cancel ? I tried this code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS);
driver = new ChromeDriver(chromeOptions);
JavascriptExecutor js = (JavascriptExecutor) driver;
String result =
(String)
js.executeScript(
"var confirmed = confirm('Press a button Either OK or Cancel'); return confirmed ? 'true' : 'false';");
WebDriverWait waitForAlert = new WebDriverWait(driver, Duration.ofSeconds(10));
waitForAlert.until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()));
System.out.println("Confirmed: " + result); // null
Seems like the confirm dialog is running asynchronously so assignment for the return variable happens before the dialog has returned anything.