Class PhantomJSDriver
Provides a way to access PhantomJS to run your tests by creating a PhantomJSDriver instance
public class PhantomJSDriver : RemoteWebDriver, IWebDriver, IDisposable, ISearchContext, IJavaScriptExecutor, IFindsById, IFindsByClassName, IFindsByLinkText, IFindsByName, IFindsByTagName, IFindsByXPath, IFindsByPartialLinkText, IFindsByCssSelector, ITakesScreenshot, IHasInputDevices, IHasCapabilities, IAllowsFileDetection
- Inheritance
-
PhantomJSDriver
- Implements
- Inherited Members
Examples
[TestFixture]
public class Testing
{
private IWebDriver driver;
[SetUp]
public void SetUp()
{
driver = new PhantomJSDriver();
}
[Test]
public void TestGoogle()
{
driver.Navigate().GoToUrl("http://www.google.co.uk");
/*
* Rest of the test
*/
}
[TearDown]
public void TearDown()
{
driver.Quit();
driver.Dispose();
}
}
Remarks
When the WebDriver object has been instantiated the browser will load. The test can then navigate to the URL under test and start your test.
Constructors
PhantomJSDriver()
Initializes a new instance of the PhantomJSDriver class.
public PhantomJSDriver()
PhantomJSDriver(PhantomJSDriverService)
Initializes a new instance of the PhantomJSDriver class using the specified driver service.
public PhantomJSDriver(PhantomJSDriverService service)
Parameters
service
PhantomJSDriverServiceThe PhantomJSDriverService used to initialize the driver.
PhantomJSDriver(PhantomJSDriverService, PhantomJSOptions)
Initializes a new instance of the PhantomJSDriver class using the specified PhantomJSDriverService and options.
public PhantomJSDriver(PhantomJSDriverService service, PhantomJSOptions options)
Parameters
service
PhantomJSDriverServiceThe PhantomJSDriverService to use.
options
PhantomJSOptionsThe PhantomJSOptions used to initialize the driver.
PhantomJSDriver(PhantomJSDriverService, PhantomJSOptions, TimeSpan)
Initializes a new instance of the PhantomJSDriver class using the specified PhantomJSDriverService.
public PhantomJSDriver(PhantomJSDriverService service, PhantomJSOptions options, TimeSpan commandTimeout)
Parameters
service
PhantomJSDriverServiceThe PhantomJSDriverService to use.
options
PhantomJSOptionsThe PhantomJSOptions used to initialize the driver.
commandTimeout
TimeSpanThe maximum amount of time to wait for each command.
PhantomJSDriver(PhantomJSOptions)
Initializes a new instance of the PhantomJSDriver class with the desired options.
public PhantomJSDriver(PhantomJSOptions options)
Parameters
options
PhantomJSOptionsThe PhantomJSOptions used to initialize the driver.
PhantomJSDriver(string)
Initializes a new instance of the PhantomJSDriver class using the specified path to the directory containing PhantomJS.exe.
public PhantomJSDriver(string phantomJSDriverServerDirectory)
Parameters
phantomJSDriverServerDirectory
stringThe full path to the directory containing PhantomJS.exe.
PhantomJSDriver(string, PhantomJSOptions)
Initializes a new instance of the PhantomJSDriver class using the specified path to the directory containing PhantomJS.exe and options.
public PhantomJSDriver(string phantomJSDriverServerDirectory, PhantomJSOptions options)
Parameters
phantomJSDriverServerDirectory
stringThe full path to the directory containing PhantomJS.exe.
options
PhantomJSOptionsThe PhantomJSOptions used to initialize the driver.
PhantomJSDriver(string, PhantomJSOptions, TimeSpan)
Initializes a new instance of the PhantomJSDriver class using the specified path to the directory containing PhantomJS.exe, options, and command timeout.
public PhantomJSDriver(string phantomJSDriverServerDirectory, PhantomJSOptions options, TimeSpan commandTimeout)
Parameters
phantomJSDriverServerDirectory
stringThe full path to the directory containing PhantomJS.exe.
options
PhantomJSOptionsThe PhantomJSOptions used to initialize the driver.
commandTimeout
TimeSpanThe maximum amount of time to wait for each command.
Properties
FileDetector
Gets or sets the IFileDetector responsible for detecting sequences of keystrokes representing file paths and names.
public override IFileDetector FileDetector { get; set; }
Property Value
Remarks
The PhantomJS driver does not allow a file detector to be set, as PhantomJS only allows uploads from the local computer environment. Attempting to set this property has no effect, but does not throw an exception. If you are attempting to run the PhantomJS driver remotely, use RemoteWebDriver in conjunction with a standalone WebDriver server.
Methods
ExecutePhantomJS(string, params object[])
Execute a PhantomJS script fragment. Provides extra functionality not found in WebDriver but available in PhantomJS.
public object ExecutePhantomJS(string script, params object[] args)
Parameters
script
stringThe fragment of PhantomJS JavaScript to execute.
args
object[]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
- object
The result of the evaluation.
Remarks
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.