Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Showing posts with label Utility Functions. Show all posts
Showing posts with label Utility Functions. Show all posts

Understanding Extern, Crypt and Environment Utility objects in QTP

In this post, We  will discuss about following utility objects in QTP.

Crypt Object
Environment Object
Extern Object


1. Crypt Object 


 Crypt object is used to encrypt the string. An encrypted string is generated for the provided string during Crypt.Encrypt method. This is useful for providing Password details and entering data in a field as setsecure.

Syntax : crypt.encrypt(strPass)


If we want to use this method through VBScript, we can create an instance of object using

Set crypt= CreateObject("Mercury.Encrypter")


2. Environment Object


Environment Object enables QTP to retrieve information  from the environment variables defined in QTP, Add new environment variables, and loading environment variables from external file.

Below are described the properties and method for Environment object.


Environment.value(EnvName) = EnvValue  will set the value of environment variable EnvName as EnvValue.


envVal = Environment.value(EnvName) will get the value of environment variable envName in variable envVal.


Environment.ExternalFileName will returns the ExternalFileName associated with the test.Will return “” if no externalfile is associated.


Environment.LoadFromFile(FileName) will load external user defined environment variables from the file specified.


3. Extern Object


By defining methods using extern object in QTP, we can call functions from external libraries. This is very useful to run functions from external DLL’s.

Using Extern Object

a. Declare Extern defintion

The syntax for declaring an extern object is as follows:

Extern.Declare micHwnd,UserProcedureName,DLLName,ProcedureinDLL, micString, micint


The above declaration declares user defined procedure UserProcedureName from procedure ProcedureinDLL in the DLL specified in DLLName. Next arguments are the datatype of various argument to be used when calling the procedure defined in extern.declare.


e.g : Extern.Declare micLong, "InternetOpen", "Wininet.dll", "InternetOpenA", micString, micDWord, micString, micString, micDWord will call InternetOpenA procedure from DLL  “Wininet.dll” on calling the method “InternetOpen”.Similarly other procedure from DLL can be declared using extern.declare.


b.Using procedure defined in Extern.declare

 Once we have defined the procedures using Extern.declare, we can call as shown below:

objIEOpen= Extern.InternetOpen("QTP_FTP", 1, vbNullChar, vbNullChar, 0).

Note we have to provide arguments with data type as defined during extern declaration above.
This will execute the procedure in DLL as specified in extern.declare.


QCUtil - utility object in QTP for QC- QTP intregration


In this tutorial, We will discuss about QCUtil - utility object in QTP for QC- QTP intregration. Will discuss various properties of QCUtil Object in this post. Hope this information is useful for QC-QTP integration.

QCUtil Object 

QCUtil Object is used to access QC OTA.
Below are the properties of QCUtil object that can be used for extracting information from QC.

Properties of QCUtil Object


1. QCUtil.CurrentRun:  This give reference to the current test run in QC.We can then get  information of currentrun  like name as shown below:


set objRun = QCUtil.CurrentRun
msgbox objRun.Name

2. QCUtil.CurrentTest: This give reference to the current test in QC. We can extract information like name of the test, adding attachment to current test using this property


set objTest = QCUtil.CurrentRun
msgbox objTest.Name

3. QCUtil.CurrentTestSet: This give reference to the current test set in QC. We can  extract information similar as above for testset using this property.


set CurrentTSTest = QCUtil.CurrentTestSet

4. QCUtil.CurrentTestSetTest: This gives reference to current test execution instance in the testset.


set CurrentTSTestSet = QCUtil.CurrentTestSetTest

5. IsConnected Property: returns true/false based on QTP currently connected to QC project.


blnQCsts = QCUtil.IsConnected

6. QCConnection Property: This gives an instance of current run session and can access the structure of QC using this property


Set QCConnection = QCUtil.QCConnection


Examples of working with QCUtil Object


1.Adding a defect in QC using QCUtil Object:


            ‘Create instance of QCConnection
            Set QCConnection = QCUtil.QCConnection
‘Create an instance of BugFactory
Set DefFactory = QCConnection.BugFactory
'Add a new defect
Set Bug = DefFactory.AddItem(Nothing)
‘Provide mandatory details for the defect
Bug.Status = “New”
Bug.Summary = “Module Detected new defect summary”
Bug.DetectedBy = “njoshi”
Bug.AssignedTo = “dev001”
Bug.Post
Set DefFactory = nothing
Set QCConnection = nothing.

2. Adding Attachment to QC 


Dim ObjCurrentTest,ObjAttch
‘ We can add attachment to currentTest, current run, testset, and testsettest by using repective ‘properties 
Set ObjTest = QCUtil.CurrentTest.Attachments
Set ObjAttachFile = ObjTest.AddItem(Null)
ObjAttachFile.FileName = FileName ‘ Provide path of the file that needs to be attached to QC 
ObjAttachFile.Type = 1
ObjAttachFile.Post
ObjAttachFile.Refresh

3. Validating If QC is connected properly: 


if QCUtil.IsConnected then
msgbox “QC is connected”
Else
Msgbox “QC is not connected”
EndIf
   

4. Connecting to QC through QTP


Set qtApp = CreateObject ("QuickTest.Application")
If  qtApp.launched <> True then
     qtApp.Launch
End If
qtApp.Visible = "true"
If Not qtApp.TDConnection.IsConnected Then
      qtApp.TDConnection.Connect QCurl, DomainName, ProjectName, UserName, Password, False
End If



Utility Function for Working with Loading OR/Libraries, Capturing Image/Text and QTP Reporter in QTP

Below are some of the useful utility functions in QTP

Loading OR/Libraries


1.    Function to load Library Dynamically:

This function will load function library at runtime with the test.
 LoadFunctionlibrary(Library Name)

2.    Function to Load OR dynamically:

This function will add an object repository to the test dynamically at run time.
 RepositoriesCollection.AddRepositoryName

Capturing Image/Text


3.   Capturing Image at run time:

    This will capture the screenshot at runtime
    Browser(“…”).Page(“…”).capturebitmap(path to store image”)


4.   Capture Text:

This will capture the text at runtime to the clipboard. 

setobjClip=createobject("Mercury.Clipboard")
objClip.SetText(someText)
printobjClip.GetText

Reporting event


5.    Reporter.reportEvent: 

   This will add user defined result to the QTP Results
   Reporter.reportEventstatus,Event,Details

6.    Reporter.Filter:

This will filter the events to be written in QTP
Reporter.Filter = rfEnableAll

7.    Systemutil.run:


 This will execute statement similar to what we write in the run in start menu
Systemutil.run(Process)