0

I would like to save some resources on my low-spec Windows boxes by running the browser in a headless mode. As far as I am aware, PhantomJS + GhostDriver is the standard choice for such task to be used with Selenium Webdriver. However after trying it and immediately running into issues with alerts handling which doesn't seem to be supported by PhantomJS. Specifically, the following exception is returned:

[ERROR - 2016-08-01T04:24:24.894Z] RouterReqHand - _handle.error - {"name":"Invalid Command Method"," . . . "}

as a result of not supporting getAlertText WebDriver Command when performing:

Alert alert = driver.switchTo().alert();

and specifically this method implemented in EventFiringWebDriver:

public Alert alert() {
  return targetLocator.alert();
}

I am looking for an alternative approach or a feasible workaround. Anyone?

Eugene S
  • 6,709
  • 8
  • 57
  • 91
  • What do you mean doesn't seem to be supported by PhantomJS?? It just mean alert will not be handle by driver itself you need to handle it by switching to alert and accept or dismiss according to your choice... – Saurabh Gaur Aug 01 '16 at 03:18
  • @SaurabhGaur I mean that `getAlertText` WebDriver `Command` is not supported internally and will return an exception. See my edit. – Eugene S Aug 01 '16 at 04:34
  • You are calling wrong method. It's `alert.getText();` instead..:) – Saurabh Gaur Aug 01 '16 at 05:37

1 Answers1

2

I have been able to work around that by executing the alert handling using JavaScript directly like this:

JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("window.alert = function(){}");
jsExecutor.executeScript("window.confirm = function(){return true;}");

At the moment, there seem to be no way to perform that operation directly via WebDriver interface for PhantomJS.

Eugene S
  • 6,709
  • 8
  • 57
  • 91