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

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



How to write to Text Files using fileSystemObject in QTP

Using FileSystemObject we can write data to a text file. Below are the steps to write to a text file.


1. Create an instance of the FileSystemObject

 This is done through
Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Use OpenTextFile to open an existing file or CreateTextFile to create a new text file to write data into.

Set otf = objFSO.CreateTextFile(FilePath,[OverWrite])
where FilePath is path of file to be created.OverWrite – Flag for overwriting content 

OR

 Set otf = objFSO.OpenTextFile(FilePath,ioMode)

where FilePath is path of file to be opened.ioMode – Define mode in which file to be opened. Use 1 to overwrite data and 8 to append data. 


3. Use below methods to write to the file.


a. Write(strText) - Write specified string in the text file.

Otf.Write(strText)

b. Writeline(strText) - writes a specified string and newline character.

otf.writeline(strText)

c. WriteblankLines(intLine) - Writes the specified number of blank lines in the text file

otf.WriteblankLines(4)

4. Close the text file.

otf.close This will close the file. It will close the text file. Note file is saved automatically when we write to a text file.


5. Set all objects as nothing


How to read text files in QTP using VBScript- Working With FileSystem Object


We can read the content of a text file using FileSystemObject methods and properties. Below are the various methods and properties used to extract data from file.

1. Creating an instance of filesystem object

Before reading content from a file, we have to create instance of filesystemobject.
Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Opening an existing file to read

Set objTxtFile = objFSO.OpenTextFile(FilePath,iomode)

Iomode can have following values:

0 – For reading – opens the file for reading.
1 – For Writing – opens the file for writing, does overwrite the existing content in file.
8 – For Appending – opens the file for appending, new content is appended at the end of file.

3. ReadAll method 

It stores all the content of a file in a variable as string.sContent = objTxtFile.ReadAll

4. ReadLine method

It allows a script to read individual lines in a text file.
sContent = objTxtFile.Readline.
To read through all lines, use a Do Loop that continues until the AtEndOfStream property is True.
Below code snippet shows how to read through each line.

i=0
Do Until objTxtFile.AtEndOfStream
        sContent = objTxtFile.ReadLine
        sDataArray(i) = objTxtFile.ReadLine
        MsgBox sContent
        i = i+1
Loop

Content of each line can be stored in an array also.

5. Read(chr) 

 The Read method allows you to read only a specified number of characters.
Stores number of characters as defined in chr.

sContent = objTxtFile.Read(5)

6. Skip(chr) 

This method skips the number of characters defined in chr from the text file
skip(5)

7. SkipLine

This method skips line of data from the text file.
skipline

8. AtEndOfLine Property

 Returns true if the file pointer is positioned immediately before the end-of-line marker in a TextStream file. Can be useful to find characters in a line or reading characters till end of line.
Do While file.AtEndOfLine <> True 

9. Line property

gives the current line number of the textStream.
objTxtFile.line

10. Column Property

 Read-only property that returns the column number of the current character position in a TextStream file.
objTxtFile.Column       

11. OpenasTextStream Method

This method is used with getfile method to read from file.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxtFile = objFSO.getFile(FilePath)
var objts = objTxtFile.OpenAsTextStream(iomode)