PhantomJSOptions and PhantomJSDriverService Class in Selenium C#

In this tutorial, we are going to discuss PhantomJS classes. That is generally used for doing some certain Settings of PhantomJSDriver.

Using of PhantomJS Classes of PhantomJSDriver in Selenium C#

The two classes which we are going to discuss are the “PhantomJSOptions” and the “PhantomJSDriverService” class. So whenever we run our script with PhantomJSDriver it launches the PhantomJSDriver with a certain default setting.

To see those Settings we will copy the console log and paste it into Notepad. So as we can see here while launching the PhantomJSDriver it is Setting certain configurations such as providing the browser name, enabling the JavaScript, enabling the TakeScreenshot facility, and so on.

PhantomJSOptions class

Providing the browser name, enabling the JavaScript, enabling the TakeScreenshot facility, this type of Settings or the capabilities can be altered programmatically also for that we will use the PhantomJSOptions class.

So in our TestScript in BaseClass we will create a private static method that is going to give us the object of PhantomJSOptions class.

private static PhantomJSOptions GetPhantomJsOptions()
{
PhantomJSOptions option = new PhantomJSOptions();
option.AddAdditonalCapebility("TakeScreenshot",false);
return option;
}

So here PhantomJSOptions GetPhantomJsOptions() if we go to the class definition of this class so there is a method called “AddAdditionalCapebility” which takes two arguments. The first is the configuration or the name of the capability and the second one is the value.

So let us suppose we want to disable the “TakeScreenshot” property. So this will be the name of the capability so here, first of all, we will create the object of PhantomJSOptions class and we are going to call the method “AddAdditionalCapebility (” “) “ the first argument is the name of capability and the second is the value that returns the object.

Now if we go to the class definition of PhantomJSDriver, we can see here we have a constructor where we can specify the PhantomJSOptions argument.

So we are going to use GetPhantomJsOptions() method directly here because this method is going to return us the object of PhantomJSOptions class.

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

Now we will put debug point over {

PhantomJSOptions option = new PhantomJSOptions(); here and {

PhantomJSDriver driver= new PhantomJSDriver(GetPhantomJsOptions()); here.

Build our solution and run the script in debug mode. So again we will copy the console log to see whether the capability is set to the required value or not.

Output: so if we do a stepover it is going to create the driver instance before that it will call the GetPhantomJsOptions() method, it will create the object for the class set the capability value, and then return the object.

So now if we copy the console log and paste it into Notepad

so as we can see here the capability “TakeScreenshot” is set to “false” and this is happening because of this option AddAdditionalCapebility (TakeScreenshot",false);

so in this manner, we can use the PhantomJSOptions class to set the Additional Capabilities or to modify the existing one.

PhantomJSDriverService class

Now moving on to the next class is PhantomJSDriverService class. So this class also provides a lot of property. By which we can set the certain configuration.

If we compare both the classes PhantomJSOptions and PhantomJSDriverService, the second class PhantomJSDriverService class is more frequently used. So again we will create one more static method.

private static PhantomJSDriverService GetPhantomJsDriverService()
{
PhantomJSDriverService service= PhantomJSDriverService.CreateDefaultService();
service.LogFileName= "TestPhantomJS.log";
service.HideCommandPromptWindow= true;
return service;
}

We want this method to return us the object of PhantomJSDriverService class. So the return type will be the PhantomJSDriverService and GetPhantomJsDriverService(), if we go to the class definition of PhantomJSDriverService class as we can see here there are a lot of properties by which we can set or unset the configuration.

For example-

whatever the logs we are seeing at the console we might need to save them in a file. So that can be done with the help of this property public static {get; set; } and this PhantomJSDriverService class inherit the DriverService so if we go inside this class there is one more property that is HideCommandPromptWindow { get; set; } if this property is set to true then whatever the Command Prompt which we are saying to that will also run as a ghost process.

So here we will use this class to create the object, now in order to create the object of this class, we need to use a static method called CreateDefaultService(). So if we go inside the class definition this is the method that we are using for creating the object. Now if we look here

public static PhantomJSDriverService CreateDefaultService();
public static PhantomJSDriverService CreateDefaultService(string driverPath);
public static PhantomJSDriverService CreateDefaultService(string driverPath,string driverExecutableFileName);

there are three overloaded versions one which doesn’t take any argument, one which takes the driverPath, and the other one which takes the argument as driverPath and ExecutableFileName.

That means let’s say if we want to use this particular phantomjs.exe executable for launching our PhantomJSDriver go to (SeleniumWebDriver -> packages -> PhantomJS2.0.0 -> tools -> phantomjs) .

So we can use the name and the location inside this public static PhantomJSDriverService CreateDefaultService(string driverPath,string driverExecutableFileName); constructor to specify that while runtime uses this particular exe file (phantomjs.exe) and location for launching the PhantomJSDriver.

So we will go with the default setting where we are not specifying any argument. Now service.LogFile and we are going to store all the logs inside the ="TestPhantomJS.log".

And also we will hide the driver Command Prompt service.HideCommandPromptWindow= true; then we are going to return this object return service; and we are going to the class definition of PhantomJSDriver class.

There is one more constructor which takes the argument as PhantomJSDriverService object so we can directly call GetPhantomJsDriverService() method here.

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

As this method is returning us the PhantomJSDriverService object so we are going to put a debug point PhantomJSDriverService service= PhantomJSDriverService.CreateDefaultService(); here and build our solution and run the script in debug mode.

Output: so it has hit the debug point if we do a stepover from here it is going to jump to GetPhantomJsDriverService() method. It will create the instance then set the LogFile and it will hide the Command Prompt Window and return the object.

we can see here, we can instantiate the PhantomJSDriver object but still, there is no Command Prompt Driver at the UI. So this is happening because of service.CommandPromptWindow= true; this Setting, and if we click on continue it will go with the normal flow.

So it is done if we go to the location of my solution SeleniumWebDriver -> Bin -> Debug so there is a file called TestPhantomJS.log

if we open it we can see whatever log which we were seeing at the Command Prompt of the driver just like the image below.

Now it is stored inside the file and this is happening because of service.LogFile ="TestaphantomJS.log"; this setting also these are the screenshots that we just took while running our script.

So in this manner, we can use PhantomJSOptions class or PhantomJSDriverService class to enable or disable certain capabilities also to add certain extra arguments while launching the PhantomJSDriver.