Headless Automation using C#, PhantomJS and NUnit

Although the name “Headless” might sound a bit scary, but Headless Browser Automation is a great way of creating lightweight tests that quickly send you results without the burden of actually opening a physical browser. So basically this way, we navigate to a website but save on the rendering time which may be beneficial in some scenarios like Continuous Integration where we want to have quick results and is more convenient to run from the command line(or a BAT file).

We can Launch multiple instances of the the script which can help in reducing the time significantly. So here is how we can leverage Headless Automation in C#:-

*Please note that I have taken NUnit Test Framework for the sake of simplicity of execution and this is optional.

  1. Create a New Project in Visual Studio and from the Templates, select Visual C# -> Windows Desktop -> Class Library.
  2. Install NUnit from here and add a reference to the following DLL in your project – “C:\Program Files (x86)\NUnit.org\framework\3.0.5813.39031\net-4.0\nunit.framework.dll” (this path may change depending on your version and the path where you install NUnit)
  3. Download Selenium Libraries from here and add reference to following DLLs in your Project(this would be present where you download selenium\selenium-dotnet-2.48.0\net40 :-
    1. Selenium.WebDriverBackedSelenium.dll
    2. ThoughtWorks.Selenium.Core.dll
    3. WebDriver.dll
    4. WebDriver.Support.dll
  4. You will also need the phantomjs.exe from here. You need to add this to the bin\Debug folder in your project.
  5. In Order to capture snapshots(yes, this is possible for Headless Browsers also !), you need to add a reference to System.Drawing from the default assemblies available.
  6. In Order to run the Nunit Tests from the Visual Studio GUI, you need to goto Tools-> Extensions and Updates->Online and search for NUnit3 Test Adapter. Install this extension and restart Visual Studio.
  7. For Nunit Fundamentals, you can refer this link.

Now you are fully loaded and ready to go with this. Following is a sample code:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Imaging;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
using OpenQA.Selenium.Remote;


namespace NUnitPhantom2
{
 [TestFixture]
 //TestFixture indicates that this class contains Test Methods
 public class TestClass
 {
 //Declaring the PhantomJS Driver
 PhantomJSDriver phantom;
 
 [OneTimeSetUp]
 //OneTimeSetUp attribute defines that the following method will run once the class is instantiated, i.e. once in the start of the run
 public void classSetup()
 {
 //Creating the instance of the PhantomJS driver
 phantom = new PhantomJSDriver();
 //Setting the default timeout to 30 seconds
 phantom.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
 }

 [SetUp]
 //SetUp attribute defines that the following method will run before each method runs.
 public void Test_Setup()
 {

 }

 [Test]
 //Test attribute defines that the following is a Test Method
 public void Test_Phantom1()
 { 
 //Navigate to Bing
 phantom.Navigate().GoToUrl("https://www.bing.com");

 //Search for Learn2Automate
 phantom.FindElement(By.Id("sb_form_q")).SendKeys("learn2automate");

 //Click on Search
 phantom.FindElement(By.Id("sb_form_go")).Click(); 

 //Grab the Snapshot
 Screenshot sh = phantom.GetScreenshot(); 
 sh.SaveAsFile(@"C:\Temp.jpg", ImageFormat.Png); 
 }

 [TearDown]
 //TearDown attribute defines that the following method will run after each method runs.
 public void Test_CleanUp()
 {

 }

 [OneTimeTearDown]
 //OneTimeTearDown attribute defines that the following method will run once the methods in the class have run, i.e. once in the end of the run
 public void classTearDown()
 {
 //Close the PhantomJS instance
 phantom.Quit();
 }
 }
}

This is how this would look in Visual Studio:-

 

Nunit

I hope the comments in the script are self explanatory. You can comment below if you have any queries.

Happy (Headless) Automating !

Harshit Kohli

 

 

Advertisements

Eat, Sleep, Automate. REPEAT !

Tagged with: , , ,
Posted in Awesum Stuff
7 comments on “Headless Automation using C#, PhantomJS and NUnit
  1. […] Headless Automation using C#, PhantomJS and NUnit […]

  2. Nelson Perez says:

    thanks for share.. I will try it.

  3. viv says:

    works thanks!

  4. Mohammed says:

    Thank you boss!

  5. Amit Mishra says:

    I want to automate my web page i.e. clicking on hyperlinks and going to next page. There Selecting dropdown and press the button.

  6. David says:

    Thank you!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog Stats
  • 168,130 hits
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 796 other followers

%d bloggers like this: