Headless Browser automation in Selenium C# using PhantomJS

In this tutorial, we will talk about PhantomJS. And how to run the test case in headless mode in Selenium C# Using PhantomJS.

What is PhantomJS headless browser

PhantomJS stands for “Phantom JavaScript”. It is a scripted headless browser used for web automation, when we say headless that means there is no UI (User Interface) for this browser unlike we have the UI for Firefox, Chrome, and IE (Internet Explorer).

Whatever the action provided by PhantomJS it will be done in the form of a background process or a daemon process. So in the UI, we will not see anything just like we used to see when we launch the Firefox, Chrome, or IE browser for the test.

PhantomJS uses JavaScript API for interacting with the webpage. So whatever the action such as opening the webpage, clicking on the link, clicking on button these all actions are done in the form of JavaScript API.

Setup PhantomJS in Selenium C#

Now in order to set up the PhantomJS in our local system. Again we will take the help of the Nuget Package Manager. So just like we install the driver for Chrome and IE we need to install the driver for PhantomJS.

So inside my project, we will do a right-click and select the option Manage Nuget Packages.

Now here we are going to search for the PhantomJS and we will install this driver.

So when we do the installation it is going to add the PhantomJS Driver exe in our project. So as we can see here we have PhantomJS.exe which is a driver file for PhantomJS.

Now after this we are going to do a couple of modifications to our framework. So first of all we need to add a browser type Inside our BrowserType.cs, in our browser type enum we will add PhantomJs.

Now inside the BaseClass.cs, we will create one more static method.

private static PhantomJSDriver GetPhantomJsDriver()
{
PhantomJSDriver driver= new PhantomJSDriver();
return driver;
}

Explanation of Code:

This method will give us the driver for PhantomJS. public static PhantomJSDriver GetPhantomJsDriver().

So just like we have created the private static method for individual drivers similarly we have created one more static method for getting the PhantomJS Driver and inside this, we are going to use the PhantomJS Driver class.

So just like we have used the FirefoxDriver for Firefox, ChromeDriver for Chrome, and IEDriver for Internet Explorer.

Similarly, in order to use the PhantomJS headless browser we are going to use PhantomJSDriver, it inherits from the RemoteWebDriver and if we go inside the RemoteWebDriver it inherits from the IWebDriver Interface.

In other words, we can say that PhantomJSDriver indirectly inherits from the IWebDriver Interface. So here we will create the driver for PhantomJS, PhantomJSDriver driver= new PhantomJSDriver(); and we will return this driver like return driver;

So as we have discussed indirectly the PhantomJSDriver class also implements the IWebDriver Interface so here it is a perfectly valid ObjectRepository.Driver= GetPhantomJsDriver(); and here we are going to add one more case.

case BrowserType.PhantomJs:

ObjectRepository.Driver= GetPhantomJsDriver(); 

break

Here we will write case  BrowserType.PhantomJs: so when the user specifies the PhantomJSDriver inside the App.config so we are going to instantiate our driver property using the GetPhantomJsDriver() method and after that put a break;

Now once this is done inside the App.config we will specify the BrowserType as PhantomJs.

<Configuration>
<appSettings>
<add key="Browser" value="PhantomJs"/>
<add key="Username" value="User"/> 
<add key="Password" value="Pass"/> 
<add Key="Website" Value="http://localhost:5001/"/>
</appSettings> 
</Configuration>

Now after this we are going to create a directory inside our TestScript and call it PhantomJS. Inside this we will create a class named TestPhantomJS, we will make it public use the attribute [TestClass] with it.

Inside this, we will add a public method TestaphantomJSDriver() and inside this, we are going to use the same set of steps that we do for the other browsers.

So from DropDownList.cs we are going to copy these lines where we were opening the Bugzilla webpage, clicking on the “File a Bug” link, and providing the Username Password and login.

As I told you everything will be done in the form of the daemon process so we will not able to see any interaction in the UI, for that we are going to add a method for taking the screenshot after every action.

For this, we will call GenericHelper.TakeScreenShot(); so after opening the Bugzilla webpage, it will take the screenshot, similarly, after clicking on the “File a Bug” link it will again take the screenshot and so on.

In this manner, we will know whether we are performing the action in the right manner or not.

[TestClass] 
public static TestPhantomJS 
[TestMethod]
public void TestaphantomJSDriver()
{
NavigationHelper.NavigateToUrl(ObjectRepository.Congig.GetWebsite());
GenericHelper.TakeScreenShot();
LinkHelper.ClickLink(By.LinkText("File a Bug")); 
GenericHelper.TakeScreenShot(); TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_login"), ObjectRepository.Config.GetUsername());
GenericHelper.TakeScreenShot();
TextBoxHelper.TypeInTextBox(By.Id("Bugzilla_password"), ObjectRepository.Config.GetPassword()); 
GenericHelper.TakeScreenShot();
ButtonHelper.ClickButton(By.Id("log_in")); 
GenericHelper.TakeScreenShot();
LinkHelper.ClickLink(By.LinkText("Testng"));
GenericHelper.TakeScreenShot();
}

Now put the Debug point on the first line, build the solution and run the script in debug mode.

Output: So as we can see here it has started the PhantomJSDriver which will launch the PhantomJS headless browser.

As we can see it has hit the debug point but other than the driver we don’t have any browser, but in the background process our headless browser has launched.

First, we will navigate to the Bugzilla webpage then it will take the screenshot, after we will click on the “File a Bug” link again it will take the screenshot. That means after every action we are going to take the screenshot, to make sure that we are doing the action in the right manner or not.

Similarly, we will supply the username password and take the screenshot, then click on the login button and take the screenshot, after that click on the “Testng” link and take the screenshot, at last, we will click on continue.

So as we can see here we have performed all the actions but there was no browser in the UI, which means all the actions have been done in the form of JavaScript API calls in the daemon process.

In order to make sure that everything went well we will go to the location of my solution in SeleniumWebDriver -> Bin -> Debug. So as we can see here these are the screenshots that we have taken.

If we open this one as we can see this is the Bugzilla main page,

and the reason why it is coming like this is because we didn’t maximize the browser.

As we have seen all the actions have been performed in the memory in the form of the daemon process. so PhantomJS is a very powerful browser when you want to do unit testing on your webpage in this case we will not see any interaction in the UI but everything will be happening in the daemon or in the background process.

And just like other browsers, PhantomJS has certain options by which we can modify it. so we will discuss that thing in our next tutorial.

So in this manner, you can set up the PhantomJS and run a TestScript inside the PhantomJS headless browser in Selenium C#.