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