Quick test professional

QTP Tips QTP codes QTP Faqs and more

Prompt for password entry and hide while it is being typed

some times we need to prompt input box for a password and we need to  hide a password while it is being typed.
 But it is not possible with normal VBScript . we can use the AOM for IE with VBScript to do this.

Following is the code

strPw = GetPassword( “Please enter your password:” )
msgbox  “Your password is: ” & strPw

Function GetPassword( myPrompt )
‘ This function uses Internet Explorer to
‘ create a dialog and prompt for a password.

    Dim objIE
    ‘ Create an IE object
    Set objIE = CreateObject( “InternetExplorer.Application” )
    ‘ specify  the IE  settings
    objIE.Navigate “about:blank”
    objIE.Document.Title = “Password”
    objIE.ToolBar        = False
    objIE.Resizable      = False
    objIE.StatusBar      = False
    objIE.Width          = 300
    objIE.Height         = 180
    ‘ Center the dialog window on the screen
    With objIE.Document.ParentWindow.Screen
        objIE.Left = (.AvailWidth  – objIE.Width ) \ 2
        objIE.Top  = (.Availheight – objIE.Height) \ 2
    End With
  
    ‘ Insert the HTML code to prompt for a password
    objIE.Document.Body.InnerHTML = “<DIV align=”"center”"><P>” & myPrompt _
                                  & “</P>” & vbCrLf _
                                  & “<P><INPUT TYPE=”"password”" SIZE=”"20″” ” _
                                  & “ID=”"Password”"></P>” & vbCrLf _
                                  & “<P><INPUT TYPE=”"hidden”" ID=”"OK”" ” _
                                  & “NAME=”"OK”" VALUE=”"0″”>” _
                                  & “<INPUT TYPE=”"submit”" VALUE=”" OK “” ” _
                                  & “OnClick=”"VBScript:OK.Value=1″”></P></DIV>”
    ‘ Make the window visible
    objIE.Visible = True
    ‘ Wait till the OK button has been clicked
    Do While objIE.Document.All.OK.Value = 0
        WScript.Sleep 200
    Loop
    ‘ Read the password from the dialog window
    GetPassword = objIE.Document.All.Password.Value
    ‘ Close and release the object
    objIE.Quit
    Set objIE = Nothing
End Function

The code will create the screen like this

ie_password_dialog.gif

Author: Robvan der Woude

February 15, 2008 Posted by quicktestprofessional | VBScript and IE Automation | , , , | 5 Comments

Can we create the window application with VBScipt?

Can we create the window application with VBScipt?

yes: using HTML Application (HTA)

An HTML Application (HTA) is a Microsoft Windows application written with HTML and Dynamic HTML. The ability to write HTAs was introduced with Microsoft Internet Explorer 5.0.

HTAs can be made from regular HTML files by simply changing the file extension to .hta. A regular HTML file is confined to the security model of the web browser – i.e. to communicating with the server, manipulating the page’s object model (usually to validate forms and / or create interesting visual effects) and reading / writing cookies. An HTA runs as a fully trusted application and therefore has more privileges than a normal HTML file – for example an HTA can create / edit / remove files and registry entries.

To customize the appearance of an HTA, a new tag, with attributes, is introduced: <hta:application …>. This tag appears in the <head>…</head> section of an HTA document.

Because an HTA has more privileges than an HTML page, it cannot be executed via http. Rather, the HTA must be downloaded (just like an EXE file) and executed from local file system.

 QTP code Generates for a HTML Application

QTP generates the code as similar to the normal web applications, except that it shows window instead of browser 

Example: If the  HTML Application has a edit box then code will be

Window(..).Webedit(…).set “value”

Sample code :

This example code creates a HTML app with 3 edit boxes.

<SCRIPT Language=”VBScript”>
Sub SetNameColor

If Len(UserName.Value) <> 0 Then

UserName.style.background = “cyan”

Else

UserName.style.background = “yellow”

End If

End Sub

</SCRIPT>

<body>

Name<br>

<input type=”text” name=”UserName” size=”25″ style=”background-color:yellow” onChange=”SetNameColor”><p>

Address<br>

<input type=”text” name=”Address” size=”25″><p>

Phone<br>

<input type=”text” name=”Phone” size=”25″><p>

</body>

For More information on HTML Application Refer:

http://msdn2.microsoft.com/en-us/library/ms536496.aspx

http://www.microsoft.com/technet/scriptcenter/resources/qanda/htas.mspx

References:MSDN 

February 15, 2008 Posted by quicktestprofessional | Extra Topics | | 2 Comments

Get all files within a time range or specified time

Dim From

From = TimeSerial( 11, 0, 0 ) ‘ to emphasize 24 hour clock! 11 AM

Dim Till

Till = TimeSerial( 13, 0, 0 )

 Dim oFS

Set oFS = CreateObject( “Scripting.FileSystemObject” )

Dim oFile, tFile, Range

For Each oFile In oFS.getFolder( “.\” ).Files

tmFile = TimeValue( oFile.DateCreated )

bInRange = tFile >= From And tFile <= Till

msgbox oFile.DateCreated &”–”& oFile.Name
   Next

February 15, 2008 Posted by quicktestprofessional | Files and Folders | , , , , , , , , , , | 4 Comments

Dictonary Object

 Dictionary Object 

Dictionary Object  stores data key, item pairs. A Dictionary object stores the items in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.

 

 Adavntages of using it in QTP:

1. can be used as Global variable declaration. so that any test can access the values from it in the run time.

2. You can store and retrive any number of run time values in to dictonary.

3. It is one of the Parameterization techique we can use in QTP

 

 Disadvantages:

we can not specify the values in the desingn time like Datatable , Action parameters, environment variable.

So it is useful only in Run time , not design time

Methods:

Add Method

Adds a key and item pair to a Dictionary

object. object.Add (key, item)

Arguments 

object Required. Always the name of a Dictionary object.

key Required. The key associated with the item being added.item Required.

The item associated with the key being added. 

Remarks

An error occurs if the key already exists.

The following example illustrates the use of the Add method.

Dim d   ‘ Create a variable. 

Set d = CreateObject(“Scripting.Dictionary”) 

 d.Add “a”, “Athens”   ‘ Add some keys and items.  

d.Add “b”, “Belgrade”

 

 Items Method

 Returns an array containing all the items in a Dictionary object. 

object.Items( )

 Remarks The object is always the name of a Dictionary object.The following code illustrates use of the Items method:

Set d = CreateObject(“Scripting.Dictionary”)   
 d.Add “a”, “Athens”   ‘ Add some keys and items.  
  d.Add “b”, “Belgrade”    
   a = d.Items   ‘ Get the items.  
  For i = 0 To d.Count -1 ‘ Iterate the array.
       s = s & a(i) & “<BR>” ‘ Create return string.  
      Next
Msgbox s  

     Exists Method

 Returns true if a specified key exists in the Dictionary object, false if it does not.

object.Exists(key)

 Arguments

object Required. Always the name of a Dictionary object.

key Required. Key value being searched for in the Dictionary object.

 Remarks

The following example illustrates the use of the Exists method.

Set d = CreateObject(“Scripting.Dictionary”)  
  d.Add “a”, “Athens”   ‘ Add some   keys and items.   
 d.Add “b”, “Belgrade”   
   If d.Exists(“c”) Then   
    Msgbox  “Specified key exists.”   
 Else      
 Msgbox  “Specified key doesn’t exist.” 
   End If

  Keys Method

Returns an array containing all existing keys in a Dictionary object.

 object.Keys( )

Remarks

The object is always the name of a Dictionary object.

The following code illustrates use of the Keys method:   

Dim a, d, i   ‘ Create some variables.   
 Set d = CreateObject(“Scripting.Dictionary”)   
 d.Add “a”, “Athens”   ‘ Add some keys and items.  
  d.Add “b”, “Belgrade”   
  a = d.Keys   ‘ Get the keys. 
   For i = 0 To d.Count -1 ‘ Iterate the array.    
   s = s & a(i) & “<BR>” ‘ Return results.   
  Next
Msgbox s

 Remove Method  

Removes a key, item pair from a Dictionary object. 

object.Remove(key) 

Arguments

 object Required. Always the name of a Dictionary object.

key Required. Key associated with the key, item pair you want to remove from the Dictionary object.

Remarks

An error occurs if the specified key, item pair does not exist.

The following code illustrates use of the Remove method:

Dim a, d   ‘ Create some variables. 
Set d = CreateObject(“Scripting.Dictionary”) 
d.Add “a”, “Athens”   ‘ Add some keys and items. 
d.Add “b”, “Belgrade”
 d.Add “c”, “Cairo”  
d.Remove(“b”)   ‘ Remove second pair.

RemoveAll Method

 The RemoveAll method removes all key, item pairs from a Dictionary object.

 object.RemoveAll( ) 

Dim a, d, i    Create some variables.
 Set d = CreateObject(“Scripting.Dictionary”) 
d.Add “a”, “Athens”   ‘ Add some keys and items.
 d.Add “b”, “Belgrade” 
d.Add “c”, “Cairo”  
a = d.RemoveAll   ‘ Clear the dictionary.

Author: Mohan Kakarla

Reference:MSDN

February 15, 2008 Posted by quicktestprofessional | Dictonary Object | , , , , , , , , , | No Comments Yet

Get The Latest Created File

   Const cdtFirst = #1/1/100#

   Dim sFolder  : sFolder    = “.\”
   Dim oFS      : Set oFS    = CreateObject( “Scripting.FileSystemObject” )
   Dim dtLatest : dtLatest   = cdtFirst
   Dim sExt     : sExt       = “ppt” ‘ not entirely sure about this extension
   Dim oLFile   : Set oLFile = Nothing
   Dim oFile
   For Each oFile in oFS.GetFolder( sFolder ).Files
       If sExt = LCase( oFS.GetExtensionName( oFile.Name ) ) Then
          If dtLatest < oFile.DateLastAccessed Then ‘ maybe DateCreated/DateLastModified
             dtLatest = oFile.DateLastAccessed
             Set oLFile = oFile
          End If
       End If
   Next
   If oLFile Is Nothing Then
      WScript.Echo “No file with extension”, sExt, “found.”
   Else
      WScript.Echo “found”, oLFile.Name, oLFile.DateLastAccessed
   End If

February 15, 2008 Posted by quicktestprofessional | Files and Folders | , , , , , , , , , , | No Comments Yet

Run an .exe file and wait till it finish

To run path\abc .exe and wait to contunie with next statement in the .vbs-file until after abc.exe has finished.

add the bWaitOnReturn  parameter to the Run statement …

object.Run strCommand, [intWindowStyle], [bWaitOnReturn]

February 15, 2008 Posted by quicktestprofessional | Uncategorized | | 1 Comment

VBScript Faqs and Useful resources

February 15, 2008 Posted by quicktestprofessional | VBScript Faqs and Useful resources | | 2 Comments