Wednesday, November 11, 2009

How to search a string in a word document

Below function searches for a string in specified document and returns true if it finds the string else returns false.

function findTextInWord(doc_file,looking_for)
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(doc_file)

'Create a Range object of the file's contents
Set objRange = objDoc.Content 'Is equivalent to: Set objRange = Range()

'Grab the text within that range
strContents = objRange.Text

'Remove any non-printing characters such as bullets
strContents = objWord.CleanString(strContents)


objDoc.close
set objDoc=Nothing

if instr(strContents,looking_for)> 0 then
findTextInWord=true
else
findTextInWord=false
end if
end Function

if findTextInWord("path of the word document","search string") then
msgbox "Found"
else
msgbox "Not found"
end if

Thursday, May 14, 2009

How to check broken links in a webpage?

Broken link: A broken link refers to any clickable text that should take you to a document or webpage, that actually results in "page not found", a time out or any other type of error.

How can we verify these links in our application?

We can check the broken links by manually using Page Checkpoint.
To do this:
Step1: Start recording
Step2: Goto Inser Menu -> checkpoint -> Standard Checkpoint or
Start recording -> simply press F12(which is shortcut to Sandard Checkpoint)
Step3: Point to any object in the application -> Point to the Page object in Object Selection dialog box and click on the ok button.


Page Checkpoint Properties dialog box opens.
Step4: Select the "Broken Links" checkbox in "All objects in page" and click on ok button.


Step5: Stop recording.
If you run the script, QTP identifies any broken links in your web application.

We can also check the broken links by setting Advance web options.
To do this:
Step1: Tools Menu -> Options
Step2: Select the web tab and click on Advanced button
Step3: In Advanced Web Options, select "Create a checkpoint for each Web page while recording" check box and choose "Broken Links" check box and click on ok button

And click on ok button to close the options dialog box.

If you want to check the broken links for your current host,choose
Step1: Tools Menu -> Options and click on Web tab



Step2: select the "Broken links - check only links to current host" check box, which instructs QuickTest to check only for broken links that are targeted to your current host.

Wednesday, May 13, 2009

How to call a function existing in a DLL file using QTP?

Generally we cannot directly open the DLL files in notepad or in some editors to check the functions in a DLL.
We can use Dependency Walker or PE Explorer to open the DLL files to check the functions.

Here is the method to call a function existing in a DLL file:
We can use Extern object which enables you to declare calls to external procedures from an external dynamic link library.
Once you use the Declare method for a method, you can use the Extern object to call the declared method.

Syntax:
Extern.Declare(RetType, MethodName, LibName, Alias [, ArgType(s)])

RetType - Data type of the value returned by the method.
MethodName - Any valid procedure name.
LibName - Name of the DLL or code resource that contains the declared procedure.
Alias - Name of the procedure in the DLL or code resource.
Note: DLL entry points are case sensitive.
Note: If Alias is an empty string, MethodName is used as the Alias.

Example:
The following example uses the Extern.Declare and Extern. methods to change the title of the Notepad window.
'Declare FindWindow method
Extern.Declare micHwnd, "FindWindow", "user32.dll", "FindWindowA", micString, micString
'Declare SetWindowText method
Extern.Declare micLong, "SetWindowText", "user32.dll", "SetWindowTextA", micHwnd, micString
'Get HWND of the Notepad window
hwnd = Extern.FindWindow("Notepad", vbNullString)
if hwnd = 0 then
MsgBox "Notepad window not found"
end if
'Change the title of the notepad window
res = Extern.SetWindowText(hwnd, "Uday")

To check the above code, open one blank notepad file.
Observe the title of the notepad window will change to Uday.

Wednesday, May 6, 2009

How to pass parameters(input & output) between actions?

Here i want to pass two values from Action1 to Action2. In Action2 i am defining input parameters and output parameters. I do some operation and return the value as output parameter.

1. Start a new test
2. Create a new action by choosing Insert Menu -> Call to New Action
3. Choose "Reusable action" checkbox for Action2 and click Ok.
4. Now you are in Action2, now choose Edit Menu -> Action -> Action Properties and click on parameter tab.
Add two inpur parameters. To do this click on "+" sign.
Give the first parameter name as "a" and type as number.
Add another parameter with name as "b" and type as number.
In the output parameter add a parameter name with "result" of type number and click on ok button.
If any warning message displayed click on Yes button.
5. Do whatever operation you want in Action2. Here i want to add these values and assign to output parameter value soI am adding one statement called
parameter("result")=Parameter("a")+Parameter("b")
6. Now in the Keyword view, if Action2 is displayed, then right click on that action and choose delete. Choose "Delete the selected call to the action" and click ok button.
7. Now swith to Expert view and choose Action1.
8. Include the following two statements in Action1.
RunAction "Action2",0,34,54,var '(Here 34 and 54 vare const values i am supplying as input parameters to action2 and the result i am saving in 'var' variable).
msgbox var

Thursday, April 30, 2009

How to find the number of rows and columns used in an Excel sheet

strFilePath="D:\EmpDetails.xls"
Dim fileSysObj
Set xlObj=createobject("Excel.application")
Set xlWorkBookObj=xlObj.workbooks.open(strFilePath)
Set xlWorkSheetObj=xlWorkBookObj.worksheets(1)

intRowCount=xlWorkSheetObj.UsedRange.rows.count 'intRowCount=xlWorkSheetObj.rows.count - it returns the number of rown in an Excel sheet

intColCount=xlWorkSheetObj.UsedRange.columns.count
' intColCount=xlWorkSheetObj.columns.count - it returns the number of columns in an Excel sheet

msgbox "No. of rows used "&intRowCount&" and No. of columns used. "&intColCount

xlWorkBookObj.close
xlObj.application.quit

Set xlObj=nothing
Set xlWorkBookObj=nothing
Set xlWorkSheetObj=nothing

Monday, April 6, 2009

Working with Excel functions in QTP

strXLFilePath="H:\Sample VBScripts\SampleXL.xls"
rowId=6
colId=6
sheetId=2
setValue="Senior Member Technical"

Set fileSysObj=CreateObject("Scripting.filesystemobject")
result=getCellValue(strXLFilePath,rowId,colId,sheetId)
MsgBox(result)

setXLCellValue strXLFilePath,rowId,colId,sheetId,
setValueresult=getCellValue(strXLFilePath,rowId,colId,sheetId)
MsgBox(result)

temp=ComputeMacro(strXLFilePath,sheetId,rowId,colId)

Public Function getCellValue(strXLFilePath,rowId,colId,sheetId)
Set xlObj=CreateObject("Excel.Application")
If fileSysObj.FileExists(strXLFilePath) Then
Set xlWorkBookObj=xlObj.workbooks.open(strXLFilePath)
Else
MsgBox("The specified Excel file does not exist")
End If
Set xlWorkSheetObj=xlWorkBookObj.worksheets(sheetId)
returnData=xlWorkSheetObj.cells(rowId,colId).value
getCellValue=returnData
xlWorkBookObj.close
xlObj.application.quit
Set xlObj=Nothing
Set xlWorkBookObj=Nothing
Set xlWorkSheetObj=nothing
End Function

Public sub setXLCellValue(strXLFilePath,rowId,colId,sheetId,setValue)
Set xlObj=CreateObject("Excel.Application")
If fileSysObj.FileExists(strXLFilePath) Then
Set xlWorkBookObj=xlObj.workbooks.open(strXLFilePath)
Else
MsgBox("The specified Excel file does not exist")
End If
Set xlWorkSheetObj=xlWorkBookObj.worksheets(sheetId)
xlWorkSheetObj.cells(rowId,colId).value=setValue
xlWorkBookObj.save
xlWorkBookObj.close
xlObj.application.quit
Set xlObj=Nothing
Set xlWorkBookObj=Nothing
Set xlWorkSheetObj=nothing
End sub
Public Function ComputeMacro(fileName,SheetName,rowId,colId)
Set xlObj=CreateObject("Excel.Application")
If fileSysObj.fileexists(fileName) Then
Set xlWorkBookObj=xlObj.workbooks.open(strXLFilePath)
Else
MsgBox("The specified Excel file does not exist")
End If
Set xlWorkSheetObj=xlWorkBookObj.worksheets(sheetId)
cellValue=xlWorkSheetObj.cells(rowId,colId).value
ComputeMacro=cellValue MsgBox(cellValue)
End function

Thursday, April 2, 2009

ReturnDate function in QTP

'This function returns the date in the specified format
'General usage returndate(offset,format)
'Examples: returndate(NULL,NULL) displays output in 4/1/2009 format
' returndate(2,NULL) displays output in 4/3/2009 format
' returndate(2,"mm-dd-yyyy") displays output in 04-03-2009 format
' returndate(2,"dd-mm-yy") displays output in 03-04-09 format
' returndate(2,"dd/mm/yy") displays output in 03/04/09 format

retdate=returndate(2,"dd-mm-yy")
msgbox retdate

public Function returndate(intOffset,strDateFormat)
Dim retDate
If isnull(intOffset)=-1 or isnull(strDateFormat)=-1 Then
retDate=date
End If
If isnull(intOffset)<>-1 Then
retDate=CDate(Date+intOffset)
End If
If isnull(strDateFormat)<>-1 Then
Select Case ucase(strDateFormat)
Case "MM-DD-YYYY"
retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" + Right(CStr(Year(retDate)), 4)
Case "MM/DD/YYYY" retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" + Right(CStr(Year(retDate)), 4)
Case "MM-DD-YY" retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" + Right(CStr(Year(retDate)), 2)
Case "MM/DD/YY" retDate=Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" + Right(CStr(Year(retDate)), 2)
Case "DD-MM-YYYY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Right(CStr(Year(retDate)), 4)
Case "DD/MM/YYYY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Right(CStr(Year(retDate)), 4)
Case "DD-MM-YY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"-" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"-" + Right(CStr(Year(retDate)), 2)
Case "DD/MM/YY" retDate=Cstr(Right("0" & CStr(Day(retDate)), 2))+"/" +Cstr(Right("0"+Cstr(month(retDate)),2)) +"/" + Right(CStr(Year(retDate)), 2)
End Select
End If
returndate=retDate
End Function