LeanFT Training Programme

For existing UFT/QTP resources, LeanFT is the need of the hour ! Here is why :-

  1. It gives you a better IDE – Visual Studio/Eclipse.
  2. It is faster in development as well as execution.
  3. It offers easier cross browser integration.
  4. It supports third party libraries as well as Frameworks.
  5. You can write more robust code (better exception handling).
  6. You can be more close to Developer’s Ecosystem – same IDE, Version Control and Testing Frameworks.

We have a comprehensive LeanFT training plan for individuals as well as corporates which can help you get kick-started with LeanFT within a few days. For more details, drop an email to – kohli_harshit@hotmail.com

 

Happy Automating !

Harshit Kohli

Tagged with: , , , , ,
Posted in Tutorials

2015 in review

The WordPress.com stats helper monkeys prepared a 2015 annual report for this blog.

Here’s an excerpt:

The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 26,000 times in 2015. If it were a concert at Sydney Opera House, it would take about 10 sold-out performances for that many people to see it.

Click here to see the complete report.

Posted in Awesum Stuff

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

 

 

Tagged with: , , ,
Posted in Awesum Stuff

LeanFT C# Tutorial Part 9 – Using LeanFT and Selenium Together !

I don’t want to debate which tool is better, of course both tools have their advantages and disadvantages. So lets use all the advantages together in a mega script which uses both technologies. For doing this, you have to:-

  1. Create a LeanFT Test Project in Visual Studio.
  2. Download Selenium Libraries from here.
  3. From the downloaded folder, add the reference for the following DLLs your Visual Studio project:-
    1. Selenium.WebDriverBackedSelenium.dll
    2. ThoughtWorks.Selenium.Core.dll
    3. WebDriver.dll
    4. WebDriver.Support.dll
  4. If you are working with Chrome, you need to additionally download the chromedriver.exe from here and put it in the bin\debug folder.
  5. Now the setup is done, you can get your hands dirty with the following code:-

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HP.LFT.SDK;
using HP.LFT.SDK.Web;
using HP.LFT.SDK.Insight;
using System.Drawing;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.IO;

namespace LeanFtTestProject1
{
    [TestClass]
    public class LeanFtTest : UnitTestClassBase<LeanFtTest>
    {
        IBrowser browser;
        IWebDriver chromeDriver;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            GlobalSetup(context);
        }

        [TestInitialize]
        public void TestInitialize()
        {
            //browser = BrowserFactory.Launch(BrowserType.Chrome);
            ChromeOptions CO = new ChromeOptions();
            CO.AddExtension(@"C:\Program Files (x86)\HP\LeanFT\Installations\Chrome\Agent.crx");
            chromeDriver = new ChromeDriver(CO);
            browser = BrowserFactory.Attach(new BrowserDescription
            {
                Type = BrowserType.Chrome
            });
        }


You can now use chromeDriver and browser objects interchangeably in your script!

For more information on using Selenium in C#, you can refer many blogs that write about it.

Happy Automating!
Harshit Kohli

Tagged with: , , , , , ,
Posted in Tutorials

LeanFT C# Tutorial Part 8 – Running Tests Remotely

The IDE that we use to create LeanFT Tests (Visual Studio/Eclipse) is heavy and may take a toll on the system memory-wise. So its advisable to have two separate kinds of machines – one for development of scripts and one for execution. The machines that we use for execution need not have the full IDE and we can just have the LeanFT Runtime engine running on them.

So when you are installing LeanFT, you can select the following option when you just want to use the machine for execution :-

LeanFTInstallation

In your Visual Studio project on the development machine, you need to open the App.config file and uncomment the following section :-


    <sdk mode="replay">
      <connection address="ws://execution_machine_name:5095">
        <connect timeout="60" />
        <response timeout="600"/>
      </connection>
      <server autolaunch="true" />
    </sdk>
    

In your exeucution machine, start the runtime engine and right click on the LeanFT Runtime icon on the right corner of the taskbar and click the settings option which will the file LFTRuntime.exe.config. Update the the connection key in the XML to have mode as “AcceptNonSecuredRemoteConnections” rather than the default value which is “AcceptLocalConnectionOnly” :-

<connection port="5095" mode="AcceptNonSecuredRemoteConnections">

Please make sure that the port number mentioned here is the same as one mentioned in the app.config file in the dev machine. Also, make sure you restart the runtime engine after making this change.

Now, if you start the code in your dev machine, it should run on the execution machine provided they are on the same domain.

LeanFT C# Tutorial Part 9 – LeanFT and Selenium Together

Happy Automating!
Harshit Kohli

Tagged with: , , , , , , , , ,
Posted in Tutorials

LeanFT C# Tutorial Part 7 – Using Insight Objects

Insight Object Identification has been one of my favorite features from HP since it was launched with UFT in 2012. I have been using it in a lot of my UFT scripts. In case you are not familiar with this concept, its a way of using Image based identification rather than object based identification that we generally do. It can be reaaaally helpful in following situations:-

1. An Object is not getting identified.
2. An Object does not belong to the supported technology stack, i.e. the underlying technology of the product is not supported (for eg: I used Inisght to write a script for itunes!)
3. You want to execute a script without hooking into the application(sometimes applications become slow on hooking)

Lets take an example of searching something from Bing(although this does not apply to the above 3 rules but taking this for the sake of simplicity).

BingSearchJPG

Here is how you can use Insight in in this case:

Take a cropped snapshot of the yellow search icon and save it in a file( can be BMP,JPG or PNG) and write the following code:-


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HP.LFT.SDK;
using HP.LFT.SDK.Web;
using HP.LFT.SDK.Insight;
using System.Drawing;

namespace LeanFtTestProject1
{
    [TestClass]
    public class LeanFtTest : UnitTestClassBase<LeanFtTest>
    {
        IBrowser browser;

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            GlobalSetup(context);
        }

        [TestInitialize]
        public void TestInitialize()
        {
            browser = BrowserFactory.Launch(BrowserType.Chrome);
        }

        [TestMethod]
        public void Test_Insight()
        {   
            //Navigate to Bing
            browser.Navigate("www.bing.com");
            browser.Sync();

            //Search for Learn2automate
            browser.Describe<IEditField>(new EditFieldDescription
            {
                XPath = @"//INPUT[@id=""sb_form_q""]"
            }).SetValue("Learn2automate");

            //Load the Image
            Image img = Image.FromFile(@"C:\BingSearch.PNG");

            //Create the Insight Object
            var objSearch = browser.Describe<IInsightObject>(new InsightDescription(img));

            Assert.IsTrue(objSearch.Exists(5),"Search Icon should be present");

            //Click on the Object if its found            
            objSearch.Click();          
            
        }

        [TestCleanup]
        public void TestCleanup()
        {
            browser.Close();
        }

        [ClassCleanup]
        public static void ClassCleanup()
        {
            GlobalTearDown();
        }
    }
}

LeanFT C# Tutorial Part 8 – Running Tests Remotely

Happy Automating !
Harshit Kohli

Tagged with: , , , , , , ,
Posted in Tutorials

WMI Tutorial – II

WMI Connection Mechanisms

As mentioned in the previous post in this series, there are two distinct mechanisms that can be used to establish a connection to WMI on a local/remote computer:

1) SWbemLocator object and
2) SWbemServices Object

Both the mechanisms are same except 2 differences. SwbemLocator object has to be used to connect to WMI if:

a) username and password is required to connect to remote computer (if local machine account has less privileges than that of remote machine)
b) WMI script is required to run from within the webpage

The sample statements illustrating both the connection mechanisms:



strComputer = "."
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")


strComputer = "PC-Vijay"
 Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
 Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", "domain\vijay", "password")

WMI Object Flow

WMI Connection Mechanism

As seen in both of the above illustrations, both return SWbemServices object after connecting to cimv2 WMI namespace on strComputer. After obtaining the reference to an SWbemServices object, any of the several methods can be called to connect to Managed Object in order to query it. It can return either of the SWbemObjectSet, SWbemObject, or SWbemEventSource object where each object represents the different entities listed below.

i) SWbemObject: Managed Object
ii) SWbemObjectset: Collection of Managed Objects
iii) SWbemEventSource: Collection of Events from event query

In the next post, we will examine sample script line by line in order to understand its components.

Tagged with: , , , , , , , , , , , ,
Posted in Tutorials
Blog Stats
  • 87,127 hits
Categories

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

Join 702 other followers

Follow

Get every new post delivered to your Inbox.

Join 702 other followers

%d bloggers like this: