Monday, December 17, 2012

Automating Word Document- 1

How to print all words in a word document?
Dim oWord, Doc, currentDocument
Doc = "C:\Desktop\Test.doc" 'contains 200 words
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = 0
oWord.Documents.Open Doc
Set currentDocument = oWord.Documents(1)
Dim nWords, n
nWords = currentDocument.Words.count
Print  "Number of Words:  " & nWords
For n = 1 to nWords
     Print currentDocument.Words(n)
Next
currentDocument.Close
Set currentDocument = Nothing
oWord.Quit
Set oWord = Nothing
********************************************
 How to count number of pages in a word document?
Below lines of code shows how to find total number of pages in a word document:
SystemUtil.CloseProcessByName("WINWORD.exe")
Dim oWord, Doc
Doc = "C:\Desktop\Test.doc" 'contains 20 pages
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open  doc
Dim nPages
nPages=oWord.ActiveDocument.BuiltInDocumentProperties("Number of pages")
msgbox nPages ' will display total number of pages
ActiveDocument.Close
Set ActiveDocument = Nothing
oWord.Quit
Set oWord = Nothing

Killing a Process

Below is the function that is used to kill any process:
Public Function fnProcessKill (ByVal strProcess)
Dim FunctionName: FunctionName = UCase ("fnProcessKill")
Dim strPreFix: strPreFix = "In " & pcThisLib & " - " & FunctionName & ","
Set objSWbemServices = GetObject("winmgmts:")
Set colSWbemObjectSet = objSWbemServices.ExecQuery("SELECT * FROM Win32_Process Where Name = '"& strProcess &"'")
If colSWbemObjectSet.Count > 0 Then
For i = 1 to colSWbemObjectSet.Count
SystemUtil.CloseProcessByName(strProcess)
Next
Else
fnProcessKill = micFail ' Did not find the process.
End If
Set objSWbemServices = Nothing ' Clear object from memory.
Set colSWbemObjectSet = Nothing ' Clear object from memory.
End Function

Function to close All Browsers except ALM

'Name: platform_web_CloseAllBrowsersExceptALM ()
'Desc: Closes all browsers except ALM
'Date Created:
'Author:
'Input Parameters: i
Public Sub platform_web_CloseAllBrowsersExceptALM ()
Call CloseAllBrowsersExceptALM ()
End Sub
Public Sub CloseAllBrowsersExceptALM ()
'Clean up the open browser windows from previous test
Dim vcol_Handles, vCtr, vHwnd, Flag, vLastHWnd,sCompare
vCtr = 0
Flag = 1
Set vcol_Handles = CreateObject("Scripting.Dictionary")
While (Window("regexpwndclass:=IEFrame","index:=" & vCtr).Exist(0) And Flag)
'wait 1
vHwnd = Window("regexpwndclass:=IEFrame","index:=" & vCtr).getroproperty("Hwnd")
If (vLastHWnd=vHwnd) Then
Flag = 0
Else
vcol_Handles.Add CStr(vcol_Handles.Count),vHwnd
vCtr = vCtr+1
End If
vLastHWnd = vHwnd
Wend
'Use reverse order in this for loop so that the text and Hwnd remain in correct order after each is closed
For vCtr = vcol_Handles.Count-1 to 0 step -1
vHwnd = vcol_Handles.Item(CStr(vCtr))
' Determine if the browser window has the word ALM in the caption bar
sALMCompareString = "HP Application Lifecycle Management"
sCompare=Instr(Window("hwnd:=" &vHwnd).getroproperty("title"),sALMCompareString)
'sCompare=Instr(Window("hwnd:=" &vHwnd).getroproperty("title"),"Quality Center")
' If it doesn't or the title bar is empty, close the window
If (sCompare=0) or (IsNull(sCompare)) then
Window("hwnd:=" &vHwnd).Close
End If
Next
End Sub

Function to sort Web table by COLUMN NAME in ASCending or DESCending order

sColumn_Name="Type/"
'declaring variables
Dim arrDCol(1000)
Dim arrDSortCol(1000)
Dim arrCol(1000)
Dim arrSortCol(1000)
If Browser("").Page("").WebTable("").Exist(5) Then
intRowcnt = Browser("").Page("").WebTable("").RowCount
intColcnt = Browser("").Page("").WebTable("").ColumnCount(1)
' adding and comparing table headers
Set ColList = CreateObject("System.Collections.ArrayList")
j=0
For j = 3 to intColcnt
Set objCmp = Browser("").Page("").WebTable("")
strCol= trim(objCmp.GetCellData(1,j))
if(instr(strCol,sColumn_Name)) then
sFinColumn_Name=sColumn_Name
sFinColNo=j
Exit FOR
end if
Next
‘clicking on table column header which you want to sort in ascending order
set oLink=Browser("").Page("").WebTable("").ChildItem(1,sFinColNo,"Link",0)
Browser("").Page("").Sync
Set AppDataList = CreateObject("System.Collections.ArrayList")
Set SortASCList = CreateObject("System.Collections.ArrayList")
Set SortDESCList = CreateObject("System.Collections.ArrayList")
i=0
If intRowcnt>1 Then
For i = 3 to intRowcnt
Set objCmp = Browser("").Page("").WebTable("")
AppDataList.Add trim(objCmp.GetCellData(i,sFinColNo))
AppDataList.remove("")
AppDataList.TrimToSize
SortASCList.Add trim(objCmp.GetCellData(i,sFinColNo))
SortASCList.remove("")
SortASCList.TrimToSize
Next
SortASCList.Sort
n = 0
For each se in AppDataList
arrCol(n) = se
n = n +1
Next
m = 0
For each ses in SortASCList
arrSortCol(m) = ses
m = m +1
Next
i=0
For i = 0 to n
If trim(arrCol(i)) <> trim(arrSortCol(i)) then
sortFlag = sortFlag+1
strMatch = strMatch&"Unsorted : "&arrCol(i)& "- Sorted in ascending order : "&arrSortCol(i)&";"
Else
strMatch = strMatch&"sorted : "&arrCol(i)& "- Sorted in ascending order : "&arrSortCol(i)&";"
End If
Next
If sortFlag>0 Then
Reporter.ReportEvent micFail, " Table data is not displayed in sorted order - Ascending","" &strMatch
Else
Reporter.ReportEvent micPass, " Table data is displayed in sorted order - Ascending ","" &strMatch
End If
'sortin in descending order
set oLink=Browser("").Page("").WebTable("").ChildItem(1,sFinColNo,"Link",0)
oLink.Click
Browser("").Page("").Sync
Set DESCList = CreateObject("System.Collections.ArrayList")
i=0
For i = 3 to intRowcnt
Set objCmp = Browser("").Page("").WebTable("")
DESCList.Add objCmp.GetCellData(i,sFinColNo)
DESCList.remove("")
DESCList.TrimToSize
SortDESCList.Add trim(objCmp.GetCellData(i,sFinColNo))
SortDESCList.remove("")
SortDESCList.TrimToSize
Next
n = 0
For each se in DESCList
arrDCol(n) = se
n = n +1
Next
SortDESCList.Sort
SortDESCList.Reverse
m = 0
For each ses in SortDESCList
arrDSortCol(m) = ses
m = m +1
Next
strMatch=""
sortFlag=0
i=0
For i = 0 to n
If trim(arrDCol(i)) <> trim(arrDSortCol(i)) then
sortFlag = sortFlag+1
strMatch = strMatch&"Unsorted : "&arrDCol(i)& "- Sorted in descending order : "&arrDSortCol(i)&";"
Else
strMatch = strMatch&"sorted : "&arrDCol(i)& "- Sorted in descending order : "&arrDSortCol(i)&";"
End If
Next
If sortFlag>0 Then
Reporter.ReportEvent micFail, " Table data is not displayed in sorted order - Descending","" &strMatch
Else
Reporter.ReportEvent micPass, " Table data is displayed in sorted order - Descending ","" &strMatch
End If
else
Reporter.ReportEvent micWarning, " No data available to sort",""
End If
else
Reporter.ReportEvent micWarning, " No table exists",""
End If

Queries,Code Writing in QTP

Hi Everyone,
If you have any queries in QTP or looking for assistance in code /logic Please do describe the issue in the comments section.Your queries will be resolved ASAP.
Ankur Jain

Function to create File shortcut

Fn_FileShortcut()
Set WshShell=createobject(“WScript.shell”)
strDesktop=WshShell.SpecialFolders(“Desktop”)
set appConfigShortcut=WshShell.CreatShortcut(strDeskTop &  “\AppConfig.lnk”)
appConfigShortcut.TargetPath=”D:\QTP\Test.txt”
appConfigShortcut.IconLocation=”notepad.exe”
appConfigShortcut.HotKey=”CTRL+ALT+C”
appConfigShortcut.WindowStyle=1
appConfigShortcut.Description=”Application config file”
appConfigShortcut.WorkingDirectory=strDesktop
appConfigShortcut.Save

Code Optimisation in QTP

Hi Everyone,
I am looking for suggestions to optimisation in QTP.Please share if you have any :)

Thursday, August 16, 2012

Clicking an item inside Table


Here is the post that discusses how to click an item inside table.
It may be possible that when you are automating page containing web table it will contain objects such as links, images etc.We may require to click such objects and write code for it.
For such condition we can use below code:
Set otable=browser(“browser”).Page(“page”).WebTable(“table”)
table.ChildItemCount(1,1,”Link”)
If icount=1 Then
Set olink=otable.ChildItem(1,1,”link”,0)
olink.click
End If

Explanation of above code
‘setting object to table that contains links/images
Set otable=browser(“browser”).Page(“page”).WebTable(“table”)
‘using child item count inbuilt function
‘this function will count the number of objects in a cell
‘here (1,1) tells about 1st row and 1st column and value in double codes tells about type of “object” that we are looking for.
icount=otable.ChildItemCount(1,1,”Link”)
‘here we used child item inbuilt function.(1,1) signifies 1st row and column or any other cell that contains object that we want to click.”0″ specifies first link in cell to be clicked.
Set olink=otable.ChildItem(1,1,”link”,0)
if cell contains 2 links then we can use code line as below  to click on second link.
Set olink=otable.ChildItem(1,1,”link”,1)
‘it will click on the link
olink.click
We can use “image instead of “link” to click on an image in cell( 1, 1) as shown below:
Set oImage=otable.ChildItem(1,1,”image”,0)
‘it will click on the image
oImage.click
Please feel free to ask any questions.I will resolve all queries within 24 hrs.

Sunday, April 29, 2012


New features in QTP 12(possibly)

Below are some new features than can possibly be added in next version of QTP.
We hope that this will make QTP better product and easy to create effective automated scripts.
* New improved IDE with IntelliSense for classes
This will help in writing code faster.
* Multiple Document Interface (MDI) features for opening multiple tests within QTP
QTP had limitation of working on one test at a time.This will eliminate that problem.
* Integration of BPT as well as Service Test into QTP (giving rise to the name, UFT)
* Insight — an innovative, new image-recognition feature for carrying out image-based automation
* New canvas for designing test flow
Details shared by  Tarun Lalwani. Check his rendezvous with HP Israel team
I will update you soon with more details.