In my current project, I am facing a challenge. I am utilizing Selenium 2.42.2 along with the phantomjsDriver 1.1.0 in Eclipse using Java. It is crucial for my test to be able to identify and save messages from Alerts, Confirms, and possibly Prompts when a Page is opened. Unfortunately, the phantomjsDriver does not have this capability yet, so I am in need of a workaround involving the JavascriptExecutor. However, I am not very experienced in JavaScript and find it difficult to solve this issue on my own. Here is the code I have tried so far:
DesiredCapabilities dcaps = new DesiredCapabilities();
String[] phantomArgs = new String[] {
"--webdriver-loglevel=NONE"};
dcaps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
dcaps.setCapability(CapabilityType.SUPPORTS_ALERTS, true);
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
phantomjs.getAbsolutePath());
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
dcaps.setJavascriptEnabled(true);
WebDriver driver = new PhantomJSDriver(dcaps);
JavascriptExecutor js=(JavascriptExecutor) driver;
String script = "window.confirm = function(message) {"+
"document.lastConfirmationMessage = message; return true; }";
js.executeScript(script);
driver.get("http://www.mysiteWithConfirm.de");
Object message = js.executeScript("return document.lastConfirmationMessage");
Upon opening my site, a Confirm prompt is immediately displayed indicating that there is a Confirm dialog. However, I am only receiving exceptions
Error Message => 'Can't find variable: lastConfirmationMessage'