Edgewords Logo
       
home site map contact

Edgewords Books Image

QuickTest Professional Knowledge Base

Click on problems listed below to expand out the solution. Code is shown in italics.

This knowledge base will be updated on a regular basis so please return or follow us on Twitter to be informed of regular code postings



QTP Knowledge Base

How can I get QTP to maximize the web browser?

1. Whilst recording Test -> Low-Level Recording. This will switch to Low-Level recording mode.
2. Click the maximize button on the browser. A statment similar to the following should be recorded:
  Window("Microsoft Internet Explorer").Maximize

You can also use the Browser FullScreen method:
Example:
' Make browser fullscreen
Browser("Browser").FullScreen

The FullScreen method displays the browser in full-screen mode. This method is applicable when running a test using Internet Explorer only. It is not supported when using Netscape.

How can I check the colour of fonts used on a web page?

Set x = Browser("Welcome: Mercury Tours").Object.Document.all.tags("FONT")
msgbox x.length

For each obj in x
  myVal = obj.getattribute("color")
  If myVal = "#ff0000" Then
    msgbox "found red"
    msgbox obj.getattribute("innerText")
  End If
Next

How can I set the focus to the Internet Explorer window (Activate the window)?

1. Start Recording.
2. Go to Test -> Low-Level Recording. This will switch to Low-Level recording mode.
3. Click on the title bar of Internet Explorer. A statment similar to the following should be recorded.

  Window("Microsoft Internet Explorer").Click 35,5
How do I invoke a browser instance programmitically?

' Starting a web browser:
'Set the variable to "NS" for Netscape or "IE" for Internet Explorer.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Browser1 = "IE"
StartURL = "www.yahoo.com"
IF Browser1 = "IE" THEN
  set IE = CreateObject("InternetExplorer.Application")
   IE.Visible = true
   IE.Navigate StartURL
END IF
IF Browser1 = "NS" THEN
  SystemUtil.Run "netscp6.exe", StartURL,"C:\Program Files\Netscape\Netscape 6\", ""
END IF

How can I terminate all instances of Internet Explorer from QTP?

'Kill Browser Process

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'iexplore.exe'")
For Each objProcess in colProcessList
  objProcess.Terminate()
Next
On Error Goto 0
How do I check if a window exists using QTP?

if Window("Flight Reservation").Exist(60) then
  reporter.ReportEvent micPass, "Login", "Flight Window Displayed Successfully"
else
   reporter.ReportEvent micFail, "Login", "Flight Window Failed to Display"
   ExitGlobalIteration
end if

How do I minimize QTP whilst running a test?

At the top of your script use:

Dim qtApp
Set qtApp = CreateObject("QuickTest.Application")
qtApp.WindowState = "Minimized" ' Minimize QTP
wait 2
qtApp.WindowState = "Normal" ' Restore QTP
wait 2
qtApp.WindowState = "Maximized" ' Maximize QTP
Set qtApp = Nothing

How to send key combinations to an application using QTP?

Use the Windows Scripting SendKeys method:
1. Download and install the Windows Scripting Host, if you do not already have it.
2. Create a WScript.Shell object.
3. Activate the browser in which you want to execute the keys.
4. Use the SendKeys method to type the key combination.

Example:
' This code executes the CTRL+F key combination (search) on a browser.

Set WshShell = CreateObject("WScript.Shell") WshShell.AppActivate "Put the label of the browser" ' Activate the browser window
wait(3)
WshShell.SendKeys "^f" ' The caret (^) represents the CTRL key.
wait(2)

object.SendKeys(string)

The SendKeys method will send keystroke(s) to the active window. To send a single character (for example, x), use "x" as the string argument. To send multiple characters (for example, abc), use "abc" as the string argument. You can also send special characters such as SHIFT, CTRL, and ALT, which are represented by the plus sign (+), the caret (^), and the percent sign (%), respectively.
For more information on the SendKeys method and other special keys, such as the backspace or a function key, please refer to the MSDN SendKeys Method page.
How do I check if a column exists is in the data table before referencing it?

on error resume next

val=DataTable("ParamName",dtGlobalSheet)

if err.number<> 0 then
  'Parameter does not exist
else
  'Parameter exists
end if

On error Goto 0
How do you kill a process using QTP?


Call KillProcess ("'Excel.exe'")

Public Sub KillProcess (strProcess)
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colProcesses = objWMIService.ExecQuery ("Select * from Win32_Process Where Name =" & strProcess)
  For Each objProcess in colProcesses
    objProcess.Terminate()
  Next
End Sub
How can I search for a window or object open on the desktop?

' If you're in QTP and you are looking for windows or anything that might be found at the top level of your machine
' First create a description object for the window or object you are looking for:

Dim oDesc, oColl
Set oDesc = Description.Create()
oDesc("<property>").Value = "<value>"

' get collection of all childObjects that match desciption
Set oColl = Desktop.ChildObjects(oDesc)

'Now you can access the object and use your standard QTP methods etc.
oColl(0).Activate
How to use QTP to send an email through Outlook


Sub SendMailOutlook(aTo, Subject, TextBody, aFrom)

  'Create an Outlook object
  Dim Outlook 'As New Outlook.Application

  Set Outlook = CreateObject("Outlook.Application")

  'Create e new message
  Dim Message 'As Outlook.MailItem
  Set Message = Outlook.CreateItem(olMailItem)
  With Message
     'You can display the message To debug And see state
    '.Display

     .Subject = Subject
     .Body = TextBody
     'Set destination email address
     .Recipients.Add (aTo)
     'Set sender address If specified.
     Const olOriginator = 0
     If Len(aFrom) > 0 Then .Recipients.Add(aFrom).Type = olOriginator
       'Send the message
       .Send
  End With
End Sub
How to compare two string files using QTP

CompareFiles (FilePath1, FilePath2)

FilePath1 The path to the first file to compare
FilePath2 The path to the second file to compare

Public Function CompareFiles (FilePath1, FilePath2)
  Dim FS, File1, File2
  Set FS = CreateObject("Scripting.FileSystemObject")
   If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size Then
    CompareFiles = True
    Exit Function
   End If
   Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
   Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)
   CompareFiles = False
   Do While File1.AtEndOfStream = False
     Str1 = File1.Read(1000)
     Str2 = File2.Read(1000)
     CompareFiles = StrComp(Str1, Str2, 0)
     If CompareFiles <> 0 Then
       CompareFiles = True
      Exit Do
     End If
  Loop
  File1.Close()
   File2.Close()
End Function

Return value:
The function returns 0 or False if the two files are identical, otherwise True.

Example:
File1 = "C:\countries\apple1.jpg"
File2 = "C:\countries\apple3.jpg"
If CompareFiles(File1, File2) = False Then
   MsgBox "Files are identical."
Else
   MsgBox "Files are different."
End If
How to capture transaction times in QTP


' instead of using Services.StartTransaction and Services.EndTransaction, you can capture timings like this:

StartTime = Timer
' perform actions to be timed here...
EndTime = Timer
Reporter.ReportEvent micDone, "Time", EndTime-StartTime

 


New Telford Training suite more..

New Courses
Complete LoadRunner 9.5  more..


Edgewords Training

Tel +44 (0)1746 766246
Fax +44 (0)1746 766541
Email info@edgewords.co.uk
Edgewords on Linked In
Follow us on Twitter

 

home · schedule · overviews · contact · site map
©Copyright 2010 -- Edgewords Ltd