Michael Whelan

behaviour driven blog

Using PhantomJS and GhostDriver with Selenium WebDriver in .Net

With GhostDriver 1.0 recently being released I was keen to check out using PhantomJS with WebDriver. I couldn’t find any .Net examples so I thought I would post one myself.

PhantomJS is a headless browser. That means it is a web browser, but the rendered web pages are never actually displayed. This makes it fast and an excellent candidate for speeding up those slow functional tests. According to their website PhantomJS is “a headless WebKit with JavaScript API.” Webkit is the layout engine used by a few browsers, such as Chrome and Safari. So PhantomJS is a browser, but a headless one.

Ghost Driver, a project by Ivan De Marino, is a pure JavaScript implementation of the Selenium WebDriver Wire Protocol. In other words, it's a Remote WebDriver that uses PhantomJS as the back-end. GhostDriver is designed to be an integral part of PhantomJS itself and recently PhantomJS 1.8 was released with GhostDriver integrated into it. Bindings for PhantomJS can be found in WebDriver versions 2.27 and above.

A simple example

To see it in action, open a Visual Studio class library project. The first thing you need to do is get WebDriver and PhantomJS. Both are available via NuGet.

PM> Install-Package Selenium.WebDriver

PM> Install-Package phantomjs.exe

Go into the PhantomJS packages folder and copy the phantomjs.exe file into the test project. Set the Copy to Output Directory property to Copy if newer so that the phantomjs.exe file will be in the bin directory when the tests are run.

This test just navigates to google, performs a search and views the results.

[TestFixture]
public class PhantomjsTests
{
    private IWebDriver _driver;

    [SetUp]
    public void SetUp()
    {
        _driver = new PhantomJSDriver();
    }

    [Test]
    public void should_be_able_to_search_google()
    {
        _driver.Navigate().GoToUrl("http://www.google.com");

        IWebElement element = _driver.FindElement(By.Name("q"));
        string stringToSearchFor = "BDDfy";
        element.SendKeys(stringToSearchFor);
        element.Submit();

        Assert.That(_driver.Title, Is.StringContaining(stringToSearchFor));
        ((ITakesScreenshot)_driver).GetScreenshot().SaveAsFile("google.png", ImageFormat.Png);
    }

    [TearDown]
    public void TearDown()
    {
        _driver.Quit();
    }
}    

In case you are sceptical as to whether anything actually happened, check out the screenshot google.png file in the test project bin directory. As you would expect, it shows the google results, just as they would appear in a browser.

Excellent! It seems that PhantomJS and GhostDriver are just as simple and easy to use as any other WebDriver implementation. I look forward to digging a bit deeper and seeing what sort of benefits headless browser testing has for my functional tests.

About Michael Whelan

Michael Whelan is a Technical Lead with over 20 years’ experience in building (and testing!) applications on the Microsoft stack. He is passionate about applying agile development practices, such as BDD and continuous delivery, to agile processes. These days his primary focus is ASP.Net MVC Core and Azure. He contributes to a number of open source frameworks through TestStack.

comments powered by Disqus
Google

Google