Thursday, August 1, 2013

PhantomJS, the Headless Browser for your .NET WebDriver Tests

Did you know that Selenium already supports PhantomJS?

WebDriver is a specification for controlling the behavior of a web browser. PhantomJS is a headless WebKit scriptable with a JavaScript API. Ghost Driver is a WebDriver implementation that uses PhantomJS for its back-end. Selenium is a software testing framework for web applications. Selenium WebDriver is the successor to Selenium RC. The Selenium WebDriver NuGet Package is a .NET client for for Selenium WebDriver that includes support for PhantomJs via GhostDriver.

NuGet Packages

You need only install two NuGet packages in order to use PhantomJS with WebDriver. You will probably also want which ever Unit Testing framework you prefer. As always, I suggest xUnit.

  1. Selenium.WebDriver
  2. phantomjs.exe

PhantomJSDriver

After installing those, using the PhantomJSDriver is as easy as any other WebDriver!

const string PhantomDirectory =
    @"..\..\..\packages\phantomjs.exe.1.8.1\tools\phantomjs";
 
[Fact]
public void GoogleTitle()
{
    using (IWebDriver phantomDriver = new PhantomJSDriver(PhantomDirectory))
    {
        phantomDriver.Url = "http://www.google.com/";
        Assert.Contains("Google", phantomDriver.Title);
    }
}
Shout it

Enjoy,
Tom

7 comments:

  1. Replies
    1. PhantomJS is a complete browser like any other. There are several ways to post a form, the most simple being to click the submit button. :)

      Delete
    2. I assume he was asking for syntax in C# for filling a form and posting it.

      Delete
  2. hi tom,
    do you know how to run phantomjs.exe separately,
    i try to figure out how to make this condition :
    1. run phantomjs.exe (outside of our appication)
    2. in our application the initialization will like this :
    //no phantomjs path
    - IWebDriver phantomDriver = new PhantomJSDriver()
    // how to force the phantomDriver to use the exiting phantomjs.exe that already running on the system

    anyway iam new in phantomjs
    thanks,
    Ifan

    ReplyDelete
  3. Tom, Can you point me in the right direction. When using PhantomJS and adding cookies, how does one do this via .NET

    The examples provided on the PhantomJS page are in another language. Is there a C# api or have you figured out a way to add cookies?

    ReplyDelete
  4. Great, a headless web driver implementation, what I was looking for. Thanks!

    ReplyDelete
  5. not getting anything how to instal PhantomJS etc

    ReplyDelete

Real Time Web Analytics