org.openqa.selenium.phantomjs
Class PhantomJSDriver

java.lang.Object
  extended by org.openqa.selenium.remote.RemoteWebDriver
      extended by org.openqa.selenium.phantomjs.PhantomJSDriver
All Implemented Interfaces:
org.openqa.selenium.HasCapabilities, org.openqa.selenium.interactions.HasInputDevices, org.openqa.selenium.internal.FindsByClassName, org.openqa.selenium.internal.FindsByCssSelector, org.openqa.selenium.internal.FindsById, org.openqa.selenium.internal.FindsByLinkText, org.openqa.selenium.internal.FindsByName, org.openqa.selenium.internal.FindsByTagName, org.openqa.selenium.internal.FindsByXPath, org.openqa.selenium.JavascriptExecutor, org.openqa.selenium.SearchContext, org.openqa.selenium.TakesScreenshot, org.openqa.selenium.WebDriver

public class PhantomJSDriver
extends org.openqa.selenium.remote.RemoteWebDriver
implements org.openqa.selenium.TakesScreenshot

A WebDriver implementation that controls a PhantomJS running in Remote WebDriver mode. This class is provided as a convenience for easily testing PhantomJS. The control server which each instance communicates with will live and die with the instance.

The Driver requires to optionally set some Capabilities or Environment Variables:

PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY is required only if the executable phantomjs is not available through the $PATH environment variable: you can provide it either via the Capabilities construction parameter object, or via System Property.

PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_PATH_PROPERTY is optional in case you want to use a specific version of GhostDriver (i.e. during development of GhostDriver). You can provide it either via the Capabilities construction parameter object, or via System Property.

Instead, if you have a PhantomJS WebDriver process already running, you can instead use RemoteWebDriver.RemoteWebDriver(java.net.URL, org.openqa.selenium.Capabilities) to delegate the execution of your WebDriver/Selenium scripts to it. Of course, in that case you will than be in charge to control the life-cycle of the PhantomJS process.

NOTE: PhantomJS Remote WebDriver mode is implemented via GhostDriver. It's a separate project that, at every stable release, is merged into PhantomJS. If interested in developing (contributing to) GhostDriver, it's possible to run PhantomJS and pass GhostDriver as a script.

NOTE: The design of this class is heavily inspired by org.openqa.selenium.chrome.ChromeDriver.

See Also:
PhantomJSDriverService.createDefaultService()

Nested Class Summary
 
Nested classes/interfaces inherited from class org.openqa.selenium.remote.RemoteWebDriver
org.openqa.selenium.remote.RemoteWebDriver.RemoteTargetLocator, org.openqa.selenium.remote.RemoteWebDriver.RemoteWebDriverOptions, org.openqa.selenium.remote.RemoteWebDriver.When
 
Nested classes/interfaces inherited from interface org.openqa.selenium.WebDriver
org.openqa.selenium.WebDriver.ImeHandler, org.openqa.selenium.WebDriver.Navigation, org.openqa.selenium.WebDriver.Options, org.openqa.selenium.WebDriver.TargetLocator, org.openqa.selenium.WebDriver.Timeouts, org.openqa.selenium.WebDriver.Window
 
Constructor Summary
PhantomJSDriver()
          Creates a new PhantomJSDriver instance.
PhantomJSDriver(org.openqa.selenium.Capabilities desiredCapabilities)
          Creates a new PhantomJSDriver instance.
PhantomJSDriver(PhantomJSDriverService service, org.openqa.selenium.Capabilities desiredCapabilities)
          Creates a new PhantomJSDriver instance.
 
Method Summary
 java.lang.Object executePhantomJS(java.lang.String script, java.lang.Object... args)
          Execute a PhantomJS fragment.
protected static java.util.Map<java.lang.String,org.openqa.selenium.remote.CommandInfo> getCustomCommands()
           
<X> X
getScreenshotAs(org.openqa.selenium.OutputType<X> target)
          Take screenshot of the current window.
 
Methods inherited from class org.openqa.selenium.remote.RemoteWebDriver
close, execute, execute, executeAsyncScript, executeScript, findElement, findElement, findElementByClassName, findElementByCssSelector, findElementById, findElementByLinkText, findElementByName, findElementByPartialLinkText, findElementByTagName, findElementByXPath, findElements, findElements, findElementsByClassName, findElementsByCssSelector, findElementsById, findElementsByLinkText, findElementsByName, findElementsByPartialLinkText, findElementsByTagName, findElementsByXPath, get, getCapabilities, getCommandExecutor, getCurrentUrl, getElementConverter, getErrorHandler, getExecuteMethod, getFileDetector, getKeyboard, getMouse, getPageSource, getRemoteStatus, getSessionId, getTitle, getWindowHandle, getWindowHandles, log, manage, navigate, quit, setCommandExecutor, setElementConverter, setFileDetector, setFoundBy, setLogLevel, setSessionId, startClient, startSession, startSession, stopClient, switchTo, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PhantomJSDriver

public PhantomJSDriver()
Creates a new PhantomJSDriver instance. The instance will have a default set of desired capabilities.

See Also:
for configuration details.

PhantomJSDriver

public PhantomJSDriver(org.openqa.selenium.Capabilities desiredCapabilities)
Creates a new PhantomJSDriver instance.

Parameters:
desiredCapabilities - The capabilities required from PhantomJS/GhostDriver.
See Also:
for configuration details.

PhantomJSDriver

public PhantomJSDriver(PhantomJSDriverService service,
                       org.openqa.selenium.Capabilities desiredCapabilities)
Creates a new PhantomJSDriver instance. The service will be started along with the driver, and shutdown upon calling RemoteWebDriver.quit().

Parameters:
service - The service to use.
desiredCapabilities - The capabilities required from PhantomJS/GhostDriver.
Method Detail

getScreenshotAs

public <X> X getScreenshotAs(org.openqa.selenium.OutputType<X> target)
                  throws org.openqa.selenium.WebDriverException
Take screenshot of the current window.

Specified by:
getScreenshotAs in interface org.openqa.selenium.TakesScreenshot
Overrides:
getScreenshotAs in class org.openqa.selenium.remote.RemoteWebDriver
Parameters:
target - The target type/format of the Screenshot
Returns:
Screenshot of current window, in the requested format
Throws:
org.openqa.selenium.WebDriverException
See Also:
TakesScreenshot.getScreenshotAs(org.openqa.selenium.OutputType)

executePhantomJS

public java.lang.Object executePhantomJS(java.lang.String script,
                                         java.lang.Object... args)
Execute a PhantomJS fragment. Provides extra functionality not found in WebDriver but available in PhantomJS.

See the PhantomJS API< for details on what is available.

A 'page' variable pointing to currently selected page is available for use. If there is no page yet, one is created.

When overriding any callbacks be sure to wrap in a try/catch block, as failures may cause future WebDriver calls to fail.

Certain callbacks are used by GhostDriver (the PhantomJS WebDriver implementation) already. Overriding these may cause the script to fail. It's a good idea to check for existing callbacks before overriding.

Parameters:
script - The fragment of PhantomJS JavaScript to execute.
args - List of arguments to pass to the function that the script is wrapped in. These can accessed in the script as 'arguments[0]', 'arguments[1]', 'arguments[2]', etc
Returns:
The result of the evaluation.

getCustomCommands

protected static java.util.Map<java.lang.String,org.openqa.selenium.remote.CommandInfo> getCustomCommands()