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

How to add result as HTML table format in QTP test results

We can use reporter.logevent method as explained in code/functions below to add results in  tabular format in QTP test results. Below is an example on how to use reporter.logevent


Public Function func_createHTMLTableFromExcel(strDataFile,strWorksheetName)

Set objXlHandle = Createobject("Excel.Application")

 objXlHandle.visible = false

Set objExcelWB = objXlHandle.Workbooks.Open(strDataFile)

Set objExcelWS = objExcelWB.Worksheets(strWorksheetName)

'Getting column and row count 

 strColumnCount = objExcelWS.UsedRange.Columns.Count

 strTotRows = objExcelWS.UsedRange.Rows.Count

strTable = "<table border=""""2"""">"

'Creating a html table based on content of the excel file

For j=1 to strTotRows

strTable = strTable & "<tr>"

For i=1 to strColumnCount

 strData = Trim(objExcelWS.Cells(j,i))

 strTable= strTable & "<td>"& strData &"</td>"

Next

strTable =strTable & "</tr>"

Next

strTable = strTable & "</table>"

Call reportinQTPhtmlContent(strTable)

'Closing the workbook

 objExcelWB.Close

End Function


Public Function reportinQTPhtmlContent(strTable)

Set objdict = CreateObject("Scripting.dictionary")

objdict.Add "Status", micdone

objdict.Add "NodeName", "Table Data"

objdict.Add ("StepHtmlInfo"), strTable

Reporter.LogEvent "User", objdict, Reporter.GetContext

End function

QTP Script to convert content of excel into a html document

Suppose I have some data in excel file and I want to create a html table from the excel file content. Below code can create a html table based on the content of an excel file.We can understand how to create a html file using scripting.filesystemObject in QTP. 


Example of how to call the function:

func_createHTMLTableFromExcel "E:/test.xlsx","Sheet1","e:/ht.htm"

Code for the function:

Public Function func_createHTMLFromExcel(strDataFile,strWorksheetName,strHTMLFile)

Set objXlHandle = Createobject("Excel.Application")

 objXlHandle.visible = false

Set objExcelWB = objXlHandle.Workbooks.Open(strDataFile)

Set objExcelWS = objExcelWB.Worksheets(strWorksheetName)

'Getting column and row count 

 strColumnCount = objExcelWS.UsedRange.Columns.Count

 strTotRows = objExcelWS.UsedRange.Rows.Count

strTable = "<table border=""""2"""">"

'Creating a html table based on content of the excel file

For j=1 to strTotRows

strTable = strTable & "<tr>"

For i=1 to strColumnCount

 strData = Trim(objExcelWS.Cells(j,i))

 strTable= strTable & "<td>"& strData &"</td>"

Next

strTable =strTable & "</tr>"

Next

strTable = strTable & "</table>"

set objFSO=Createobject("scripting.FileSystemObject")

set objtxt = objFSO.createTextFile(strHTMLFile)

    objtxt.write(strTable)

'Closing the workbook

 objExcelWB.Close

 set objFSO =nothing

End Function

How to add Attachments in QC during test script execution in QTP

Using below code, we can add attachment to QC during test script execution:

''This function will add the required attachment in QC, for Location defined by argument strLocationinQC

Function func_AddAttachmentToQC(FilePath,strLocationinQC)



On Error Resume next

If (ucase(strLocationinQC) = "TEST") Then

Set objCurrentTest = QCUtil.CurrentTest.Attachments

Set objAttach = objCurrentTest.AddItem(Null)

ElseIf(ucase(strLocationinQC) = "TESTSET") Then

Set objCurrentTest = QCUtil.CurrentTestSet.Attachments

Set objAttach = objCurrentTest.AddItem(Null)

ElseIf(ucase(strLocationinQC) = "TESTRUN") Then

Set objCurrentTest = QCUtil.CurrentRun.Attachments

Set objAttach = objCurrentTest.AddItem(Null)

End If

objAttach.FileName = strFilePath

objAttach.Type = 1

objAttach.Post

objAttach.Refresh

End Function