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

WMI Tutorial – I

WMI Flow

While developing scripts (not restricted to QTP/UFT), we all sometimes need to know the version of MS Office, Operating System version etc to handle different scenarios programmatically. To know the system properties, there is a technique called WMI provided by Microsoft inbuilt in its operating system ‘Windows’. In the next few posts, I will introduce you to WMI.

So, what is WMI. It stands for Windows Management Instrumentation. Windows Management Instrumentation (WMI) is the primary management technology for Microsoft® Windows® operating systems.WMI conforms to industry standards overseen by the Distributed Management Task Force (DMTF). The DMTF is an industry organization that works with key technology vendors, including Microsoft, as well as affiliated standards groups to define interoperable management solutions.

WMI was originally released in 1998 as an add-on component with Microsoft® Windows NT® 4.0 with Service Pack 4. WMI is now an integral part of the Windows family of operating systems, including Microsoft Windows 2000, Microsoft® Windows® XP and Windows 7.

WMI Architecture:
Within WMI Architecture, there are four components we need to consider:
WMI Flow

1) WMI Management Applications include tools such as the Exchange System Manager.  A management application can query, enumerate data, run provider methods or subscribe to events.

2) The WMI Infrastructure has two components – the WMI Service (winmgmt) including the WMI Core, and the WMI Repository.  The WMI Repository uses a namespace containing several sub-namespaces that are arranged hierarchically to organize objects.  A management application must connect to a namespace before the application can access objects within the namespace.  WMI names the namespace root directory as root.  The WMI service creates some namespaces such as root\default, root\cimv2 and root\subscription at system startup and pre-installs a default set of class definitions.  The WMI service acts as an intermediary between the providers, management applications, and the WMI repository. Only static data about objects is stored in the repository, such as the classes defined by providers.  WMI obtains most data dynamically from the Provider when a client requests it.

3) A WMI Provider is a COM object that monitors managed objects for WMI.

4) A Managed Object is a component (logical or physical) – such as a hard drive, network card, Operating System or service.  A provider supplies WMI with data from an object and handles messages from WMI to the object.  A WMI provider consists of a DLL and a Managed Object Format (MOF) file that defines the classes for the provider.  WMI classifies providers according to the interface features supplied by the provider.  WMI provider developers write their classes in the MOF language

In the next post, we will look at the different ways to connect to WMI Classes to retrieve information.

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

LeanFT C# Tutorial Part 6 – Parameterizing Tests

Since we are working with MSTest framework, we’ll have to deal with some of its limitations. One of them being that our test methods can not have parameters passed to them while being called(although there are indirect ways to do this but they are way too complicated). Lets focus on a simple approach that we can use:-

Our Visual Studio project has an app.config file which can be used to provide input data to our test methods. A sample config file in which i have added 2 custom keys looks like this:-

<?xml version="1.0" encoding="utf-8" ?>
    <add key="username" value="leanft" />
    <add key="password" value="tutorial" />

Now if we have a test method which needs to use these keys, our c# code will look something like this:-

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

public void login()
    string uName = ConfigurationManager.AppSettings["username"];
    string pWord = ConfigurationManager.AppSettings["password"];
    //do login here

This way we can parameterize our mstest test methods.

Happy Automating !
Harshit Kohli

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

LeanFT C# Tutorial Part 5 – Find Child Objects

If you have worked with UFT/QTP, you must be pretty familiar with the ChildObjects method but in case you have not, this method is used to find a collection of objects that match a given set of description properties.

For example, if you google “leanft tutorial”, and you want to find out how many links are redirecting to this blog, you will have to write code in the following way:-

var links = browser.FindChildren<IWebElement>(new WebElementDescription
   TagName = @"CITE",
   InnerText = As.RegExp(@".*learn2automate.wordpress.*"),

//Check that atleast one link should be present
Assert.AreNotEqual(0, links.Length);

So here the ChildObjects method(from UFT) is replaced by the FindChildren method and the logic more or less remains the same.

Please comment if you face any issues while executing this.

Happy Automating!
Harshit Kohli

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

LeanFT C# Tutorial Part 4 – Application Models

There are two ways in which we can specify the properties of the objects that will be used in our script:-

1. Descriptive Programming – This is the approach that was demonstrated in the previous tutorial where we specified the object’s properties in the TestMethod itself(by copying the object properties on the clipboard).

2. Application Model – Application Model is to LeanFT, what object repository is to UFT. Simply put together, its a collection of Test objects that we can use in our scripts. It acts as a centralized repository which can be shared across different scripts.

Which approach is better is a very subjective question and I will leave that to you to figure out which suits you best. Here is how we can create Application Models in LeanFT:-

1. Either create a completely new LeanFT Application Model Project(by going to File-> New Project-> Test) or Add a new Application Model Project to your solution(right click on Project in Solution explorer and add LeanFT-> Application Model).

2. Define the name of the model as well as a class name. Once the Application Model tsrx file is created, you can start adding objects to it.

3. There are two ways in which we can add the objects:-

a) Specify the object type and properties explicitly by clicking on the + icon on the top left corner:-


b) Open the Object Identification center and after selecting the appropriate object, click on the add to App Model icon :-


After you have the required objects in the Application Model, you can refer to these objects using intelli-sense in the TestMethod code that you can write:-


So this is how you can add objects to the application model and use them in your scripts.

LeanFT Tutorial Part 5 – Find Child Objects

Happy Automating !
Harshit Kohli

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

LeanFT C# Tutorial Part 3 – Object Identification Center

LeanFT’s object identification center offers features that can give UFT’s object spy a run for its money !

You can invoke it by clicking the “HAT” icon on the Visual Studio toolbar. After the Spy has opened, you can click on the Start Spying button to spy on a particular object.

In the below example, I spied on the Google search button on the google homepage and it shows all the properties of the button:-

Object Identification Center


After I click on the Sort by Recommended button as well as Edit Mode button, I will get check boxes in front of each property so that I can select which ones I will be using:-

Using Regular Expressions

As you can see above, clicking on the “STAR” icon on the right of a property will make it as a regular expression and we can use the standard ones like .* and others.

After we are done with the property selection, we can click on the highlight button to check whether the properties are fine or not. A New feature here is that if there are multiple objects matching the description, LeanFT will highlight all the objects rather than giving an error.

So now we have decided the properties and confirmed by highlighting, its time to click the Generate code button which will copy the C# equivalent of the code to our clipboard and we can readily use this in the test.

The code will be something like:-

browser.Describe<IButton>(new ButtonDescription
ButtonType = "submit",
TagName = "INPUT",
Name = As.RegExp(".*Search.*")

Note: In case you are using Java as your preferred language, then you can change the settings to Java and the generated code will be in Java.

LeanFT C# Tutorial Part 4 – Application Models

Happy Automating !

Harshit Kohli

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

LeanFT C# Tutorial Part 2 – Create First Test

In case you missed on part 1, check it out here.

Now that we are familiar with the code skeleton, we can start writing our first test. I’m taking a basic scenario where we are searching for leanft on my blog. For doing this, I have placed the browser initiation code in the TestInitialize method so that before execution of each test, a new instance of the browser is launched:-

public void TestInitialize()
browser = BrowserFactory.Launch(BrowserType.InternetExplorer);

Here, BrowserFactory is a namespace provided by LeanFT’s SDK. The Launch method can be used to launch either IE, Chrome or Firefox. For version support, you can have a look at the availability matrix here (you will need an HP Passport for this).

Here’s the code for the test method:-

 public void TestMethod1()
 //Set reporter to take all snapshots
 Reporter.SnapshotCaptureLevel = HP.LFT.Report.CaptureLevel.All;

 //Navigate to Search Engine

 //Search for Learn2Automate blog
 IEditField txtSearch = browser.Describe<IEditField>(new EditFieldDescription
 Type = "text",
 TagName = "INPUT",
 Name = "q"

 IButton btnSearch = browser.Describe<IButton>(new ButtonDescription
 ButtonType = "submit",
 TagName = "INPUT",
 Name = "S"

 //Select the first search result
 ILink firstResult=browser.Describe<ILink>(new LinkDescription
 TagName ="A",
 InnerText = As.RegExp(".*learn2automate.*"),
 Index = 0



 //Search for LeanFT if the blog opens
 IEditField searchBox = browser.Describe<IEditField>(new EditFieldDescription
 Type = "text",
 TagName = "INPUT",
 Name = "s"



 IButton goButton=browser.Describe<IButton>(new ButtonDescription
 ButtonType = "submit",
 TagName = "INPUT",
 Name = "Go"


 //Verify that the blog entry with title LeanFT opens
 ILink blogResult = browser.Describe<ILink>(new LinkDescription
 TagName = "A",
 InnerText = As.RegExp(".*leanfT.*"),
 Index = 0


 catch(Exception e)
 Reporter.ReportEvent("Look for LeanFT on Lean2Automate", e.Message);

Lets understand it now. First of all we start with the try block. If you have been using UFT, you must be familiar with the pain of using the old school “On Error Resume Next”, so we can counter that here by using try catch block.

The first line of the code sets the SnapshotCaptureLevel to All, which essentially means that the HTML report generated at the end of the execution will have a corresponding snapshot for each operation(like click etc) that we perform in our test.

Then we are using the good old navigate function on the browser object that we created in the Test Initialize method. For this example, I have navigated to a search engine called Duck Duck Go.

After that, we create the edit box object where we will set the search term that we are looking for. In case you want to learn how we can create this statement. You should check out my post on using LFT’s Object Spy.

After setting the search term and clicking on the go button, I create the description for the result object and put in an assertion like this –  Assert.IsTrue(firstResult.Exists(10)). Please note that this assertion enables me to exit the test method if the firstResult object is not found in a matter of 10 seconds.

Similar to the above approach, upon clicking the first result, I look for the search button on my blog and look for the term leanFT and verify the search result similarly. You must have noticed that LFT does not have any explicit wait statement, but we have to use the exists method on an object to do the synchronization.

Lastly, I have closed the browser in the TestCleanUp method so that the browser gets closed after the test method finishes execution:-

public void TestCleanup()

I hope by now you are able to create a basic script in LeanFT. If you have doubts, feel free to comment !

LeanFT C# Tutorial Part 3 – Object Identification Center

Happy Automating,

Harshit Kohli

Tagged with: , , , , , , , , ,
Posted in Tutorials
Blog Stats
  • 81,261 hits

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

Join 699 other followers


Get every new post delivered to your Inbox.

Join 699 other followers

%d bloggers like this: