Search uftHelp.com:

Wednesday, 17 September 2014

How to Encyrpt and Decrypt Password in UFT?

 Requirement:-

We don't want to share our application(say Oracle database) login password , but rather encrypt it and while using the SQL query we need it to be decrypted at runtime.

Solution:-

Firstly we will Encrypt the password and then decrypt it.

Encryption:-

Approach 1:-

Using Crypt utlity Object. 
Method used :- Encrypt 

Crypt Syntax
Syntax
Code:-
Crypt Syntax
Code Example
Approach 2:-

Using Password Encoder (Tool in UFT)

Open:- 

1. Open Command Prompt and Type the path "C:\Program Files (x86)\HP\Unified Functional Testing\bin\CryptonApp.exe" to Run it.

2.Start > All Programs > HP Software > HP Unified Functional Testing >Tools >Password Encoder

Note: Path can vary based on the UFT installation folder.

Click on Generate button to create the encrypted password.


Password Encoder UFT
Password Encoder

Decryption:-

Now we have the encrypted password, we  need it to be decrypted before we can utilize in our SQL query connection string.

Hint:- For decrypting the password UFT, internally uses the "SetSecure" method, which automatically decrypt it .So we will leverage this approach to achieve the decryption.

Code:-


'Password Encryption
Dim sPwd ,sEPwd,sDPwd
sPwd = "uftHelp"
sEPwd = Crypt.Encrypt(sPwd)
Msgbox sEPwd,,"Encryption"
'Password Decryption
sDPwd = fn_DecryptMe(sEPwd)
Msgbox sDPwd,,"Decryption"

'*******************************************Function************************************************
'Function Name:-        fn_DecryptMe
'Function Description:- Decrypt the given string 
'Input Parameters:-     Encrypted  String
'Output Parameters:-    Decrypted value 
''**************************************************************************************************
Public Function fn_DecryptMe(sEPwd)
  'Closing the opened IE instance
  Systemutil.CloseProcessByName "iexplore.exe"
  'Launching the IE
  'Mode = 7 (Displays the window as a minimized window. The active window remains active)
  SystemUtil.Run "C:\Program Files (x86)\Internet Explorer\iexplore.exe", "www.google.com", "","",7
  'Setting the value in Google url using SetSecure 
  If Browser("micclass:=Browser", "index:=0").WinEdit("micclass:=WinEdit", "index:=0").Exist Then
    Browser("micclass:=Browser", "index:=0").WinEdit("micclass:=WinEdit", "index:=0").SetSecure sEPwd
    'Taking the Decrypted Value
    fn_DecryptMe = Browser("micclass:=Browser", "index:=0").WinEdit("micclass:=WinEdit", "index:=0").GetROProperty("text")
  Else
    Print "Decryption failed, Please try again"
    fn_DecryptMe = sEPwd
  End If
  'Closing the opened IE instance
  Systemutil.CloseProcessByName "iexplore.exe"

End Function



Sunday, 14 September 2014

Various methodologies to Kill running Process in Task Manager.

Requirement:-
Identify the running process on system and kill the process using Image Name(Running Instance in Task Manager).

Solution:-

Approach 1:- Without using UFT and we will simply create a .VBS file on the system and Run it.

A.1.
We are using "Run" Command to achieve our requirement.

'*******************************************Sub***************************************************
'Function Name:-        fn_KillMe
'Function Description:- Kills the Given process using Run Command
'Input Parameters:-     sProcess,(Process Name)
'Output Parameters:-    Process Killed Message
''**************************************************************************************************
msgbox fn_KillMe("Notepad.exe")

Public Function fn_KillMe(sProcess)
 On Error Resume Next 
 Dim objShell,sStatus
 'Creating WSH object
 Set objShell = CreateObject("WScript.Shell")
 'Executing the Run command to kill the Instance
  sStatus = objShell.Run ("TASKKILL /F /IM "&sProcess)
  'Check the Run command status (0 = Success)
 if sStatus = 0 Then
  fn_KillMe ="Running instances killed for the Application = '"&sProcess&"'"
 Else
  fn_KillMe ="Error occured while killing the Instance for the Application = '"&sProcess&"'"
 End if 

 Set objShell =Nothing

 If Err.Number <> 0 Then
   fn_KillMe= "Not able to execute the process, Error occured = '"&Err.Description &"'"
 End If
 On Error Goto 0
 
End Function

Note:-


1. "TASKKILL /F /IM " 
Taskkill is a command which ends one or more Processes
/F - Forcefully closing the application
/IM - Image Name. Nothing but the visible exe file in the task manager.


Task Manager
ImageName 

2."TASKKILL /IM "
We can also use the command "TASKKILL /IM ", but incase any data is unsaved in the application instance to be killed,it will ask for user action.For example we have unsaved Notepad file, on killing of "Notepad.Exe" operation, we will get the save dialog box, and process won't be killed until and unless we dont take any action on save dialog box.

Notepad Save Dialog
Save Dialog Box
3.How to check version of WSH?
we can determine the Windows Script Host version number by typing CScript + press enter in  command prompt.


CMD
Command Prompt


A.2.
Using "Exec" Command


'*******************************************Sub***************************************************
'Function Name:-        fn_KillMeExec
'Function Description:- Kills the Given process using Exec command
'Input Parameters:-     sProcess,(Process Name)
'Output Parameters:-    Process Killed Message
'**************************************************************************************************
msgbox fn_KillMeExec("Notepad.exe")

Public Function fn_KillMeExec(sProcess)
 On Error Resume Next 
 Dim objShell,objExec,sStatus
 'Creating WSH object
 Set objShell = CreateObject("WScript.Shell")
 'Executing the Run command to kill the Instance
 Set objExec = objShell.Exec ("taskkill /IM "&sProcess)
  'Check the Exec command status (1 = Success)
 if objExec.Status = 1 Then
  fn_KillMeExec ="Running instances killed for the Application = '"&sProcess&"'"
 Else
  fn_KillMeExec ="Error occured while killing the Instance for the Application = '"&sProcess&"'"
 End if 
 Set objExec =Nothing
 Set objShell =Nothing

 If Err.Number <> 0 Then
  fn_KillMeExec= "Not able to execute the process, Error occured = '"&Err.Description &"'"
 End If
 On Error Goto 0
 
End Function


B.Using Windows Management Instrumentation (WMI), we will create "WMI Script" to fetch the running process and Terminate it.


'*******************************************Sub***************************************************
'Function Name:-        fn_KillProcess
'Function Description:- Checks if a GIVEN process is running in Task Manager and Terminates it
'Input Parameters:-     sProcess,(Process Name)
'Output Parameters:-    Process Killed Message
'**************************************************************************************************
Msgbox fn_KillProcess("outlook.exe")

Public Function fn_KillProcess(sProcess)

  Dim  objWMI, objProcList
  
  On Error Resume Next
  Err.Clear
  Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
  'Look for the given process instance in Task Manager
  Set objProcList = objWMI.ExecQuery("Select * from Win32_Process Where Name = '" & sProcess & "'")
  'Incase Process is found inthe Task Manager
  If  objProcList.Count <> 0 Then
   Dim iCnt :iCnt = 0
   For Each objProc in objProcList
    objProc.Terminate
    iCnt = iCnt + 1
   Next
    fn_KillProcess = iCnt&" Running instances killed for the Application = '"&sProcess&"'"
  Else
    fn_KillProcess = "No Running Instance found for the Application = '"&sProcess&"'"
  End If
  'Destroying the Objects
  Set objWMI = Nothing
  Set objProcList = Nothing

  If Err.Number <> 0 Then
    fn_KillProcess= "Not able to execute the process, Error occured = '"&Err.Description &"'"
  End If
  On Error Goto 0
End Function


Approach 2:-UFT is required.

A.Creating object of Task Manager


'*******************************************Sub***************************************************
'Function Name:-        fn_KillInstance
'Function Description:- Kills the Given process using Task Manager,UFT Required
'Input Parameters:-     sProcess,(Process Name)
'Output Parameters:-    N/A
''**************************************************************************************************
fn_KillInstance("EXCEL.EXE *32")
Public Function fn_KillInstance(sProcess)
 'Using UFT objects
 On Error Resume Next 
 'Launching the Task Manager Instance
 SystemUtil.Run "taskmgr.exe"
 'Incase Taskmanager is launched, select the Application instance to Kill
 If Dialog("text:=Windows Task Manager").WinListView("nativeclass:=SysListView32").Exist(2) Then
  'Incase We are not able to find the exact Image Name, Uncomment the below line and Run the code to get the Name
  'print Dialog("text:=Windows Task Manager").WinListView("nativeclass:=SysListView32").GetROProperty ("all items")
  'Selecting the instance of running application
  Dialog("text:=Windows Task Manager").WinListView("nativeclass:=SysListView32").Select sProcess
  'Clicking on End Process
  Dialog("text:=Windows Task Manager").WinButton("text:=&End Process").Click
  'Incase End Process Confirmation Window Exists
  If Dialog("is owned window:=False","nativeclass:=#32770","text:=Windows Task Manager").Dialog("is owned window:=True","text:=Windows Task Manager").WinButton("text:=End process").Exist(2) Then
   Dialog("is owned window:=False","nativeclass:=#32770","text:=Windows Task Manager").Dialog("is owned window:=True","text:=Windows Task Manager").WinButton("text:=End process").Click
  End If
 else
  Print "Not able to launch the Task Manager instance so Exiting the Test"
  Exit Function 
 End If
 'Incase no instance is found for the Application in the Task Manager
 If err.Number <> 0 Then
  Print "Sorry no Instance found for the given Process = '"&sProcess&"' in the Task Manager"
  Exit Function
 End If
 Print "Successfully Killed the Instance of Running Application in the Task Manager"
 On Error Goto 0
End Function

B. Utilizing Send Keys Method and Descriptive Programming


'*******************************************Sub***************************************************
'Function Name:-        fn_KillUsingSendKeys
'Function Description:- Kills the Given process using Task Manager using UFT
'Input Parameters:-     sProcess,(Process Name)
'Output Parameters:-    Status 
''**************************************************************************************************
Msgbox fn_KillUsingSendKeys ("excel.exe")
Public Function fn_KillUsingSendKeys(sProcess)
 'Using SendKeys Method
 On Error Resume Next 
 Dim objShell,sStatus,objProcesses,iProcessId
 'Creating WSH object
 Set objShell = CreateObject("WScript.Shell")
 'Executing the Run command to Launch Command Prompt
 sStatus = objShell.Run ("cmd")
 'Check the Run command status (0 = Success)
 if sStatus = 0 Then
  'Activates an application windows
  Window("regexpwndtitle:=C:\\Windows\\System32\\cmd\.exe").Activate
  'Using SendKeys Method to send Keystrokes to Cmd Prompt
     objShell.SendKeys "cd.."
     'Press Enter to Execute the command
     objShell.SendKeys "{Enter}"
   objShell.SendKeys "TASKKILL /F /IM "&sProcess
   objShell.SendKeys "{Enter}"
  fn_KillUsingSendKeys ="Running instances killed for the Application = '"&sProcess&"'"
 Else
  fn_KillUsingSendKeys ="Error occured while killing the Instance for the Application = '"&sProcess&"'"
 End if 

 Set objShell =Nothing 
End Function

Note :- 

We can also use SytemUtil(Utlity Object) to kill the running processes
Example:-To Kill process based on the Image Name(excel.exe) in task manager,

 Systemutil.CloseProcessByName "excel.exe"


Sytemtutil Methods
Sytemtutil Methods

Monday, 8 September 2014

How to download a file from ALM in UFT using OTA?

Requirement:-
How we can download a give file from ALM ("Test Resources" Tab) using OTA?

Search in Test Resources Tab ALM
Test Resources Tab


Solution:-
We will utilize "QCUtil" , a utility object available in UFT, to leverage the methods and properties of OTA(Open Test Architecture) for downloading the user given file.

Logic:-
Our approach is to search a given file in "Test Resources" tab, incase it is found we will download the file to the user given location(this is optional,if not given file will be downloaded in temp folders).

Code:-
fn_QCDownloadResource "Config.xml","C:\Temp\Test"
'*******************************************Sub***************************************************
'Function Name:-        fn_QCDownloadResource
'Function Description:- Search in "Test Resources" tab and download it 
'Input Parameters:-     sFileName(File to Download),sDestination(Optional, Folder to download the File)
'Output Parameters:-    None
'**************************************************************************************************
Public Function fn_QCDownloadResource(sFileName, sDestination)

 Dim objQC, objRes, objFilter, objFileList, objFile, sFilterPair, iCount
 'Check the ALM + UFT connection
 If Not QCUtil.IsConnected Then
  Msgbox "Please Connect ALM with UFT"
  Exit Function
 End If 
 'Creating ALM OTA QCConnection object,for accessing the ALM object model. 
 Set objQC = QCUtil.QCConnection
 'Assessing the Resources inside the Test Resources
 Set objRes = objQC.QCResourceFactory
 'Activating the Filter object
 Set objFilter =  objRes.Filter
 'Matching the given FileName in the Test Resources files
 objFilter.Filter("RSC_FILE_NAME") = """" & Cstr(sFileName) & """"
 'Creates a list of objects according to the specified filter.
 Set objFileList = objFilter.NewList
 'Incase Download path is empty, file is downloaded into Temp Folder
 If sDestination = "" Then
   sDestination = "C:\Temp"
 End if 
 'Downloading the found File
 If objFileList.Count = 1 Then
   Set objFile = objFileList.Item(1)
   objFile.FileName = sFileName
   'Downlaod the file and sync= True , for completion of process
   objFile.DownloadResource sDestination, True
   Msgbox "'"&sFileName&"',Successfully downloaded to '"&sDestination&"'"
 Else
   Msgbox "Sorry No Match found in ALM for the FileName = '"&sFileName&"'"
 End If
 'Destroying the Objects  
 Set objQC = Nothing
 Set objRes = Nothing
 Set objFilter = Nothing
 Set objFileList = Nothing
 Set objFile = Nothing
End Function

More OTA Examples

Sunday, 7 September 2014

How to make Password field visible to user in Angular JS?

From long back we were planning to share our knowledge on the emerging technologies in the world. To quench your thirst we are launching our first post on Angular JS pragmatic scenarios.Hope your feedback and suggestions will motivate us to share more with the technology lovers.

Requirement:- How a user can check the password entered in the text-box and rectify it in-case entered wrongly.

Solution:- 
Our approach is to change the Type of a text-box from "Text" to "Password" or vice versa , as per the convenience of the user.

Approach 1:-
Creating a directive which handles the change event of the user action(We have given "checkbox" option to user to trigger the change event).Check the option "Check Me" and the Password is visible to user.


Password field
Check Me, to make Password visible


Code:-

HTML:-
HTML Code
HTML Code 


JS:-

var app = angular.module("MyApp", []);

app.controller("AppCtrl", function($scope) {
});

app.directive('passwordCheck', [

    function() {
        return {
            restrict: 'A',
            link: function($scope, $element) {
                $element.bind("change", function() {
                    var chkState = $element[0].checked;
                    chkState ? $('#pwd').attr('type', 'text') : $('#pwd').attr('type', 'password')
                });
            }
        };
    }
        ])

Working Demo

Approach 2:-
We have leveraged "Angular Expressions", to achieve the required functionality.

Here we set a flag , and its value is getting changed based on the change event (which is fired  by a user on selection of checkbox).It's default value is "False", meaning that textbox default behaviour is "Password" type. 

Code:-

HTML:-
Html code
HTML Code(Using Angular Expressions)


JS:-


var app = angular.module("MyApp", []);

app.controller("AppCtrl", function($scope) {
});



Monday, 1 September 2014

How to log a Bug in ALM using OTA?

Requirement: - Logging Bug in ALM/QC programmatically, without any manual intervention.

Solution: - We will utilize the capabilities of OTA (Open Test Architecture) of ALM to log a Bug.

Approach: - Key is “TDConnection object” which is the entry point for using OTA; with this object we can access most of the functionalities of QC/ALM.


Step1:-
Connect to the project, Like we use to connect to ALM manually, we will follow the same steps but this time we will be using OTA .For this we need the Login Name, Password, Domain and Project details and the TDConnection object.

ALM Login window
ALM Login window
Step2:-
Create BugFactory object to Log a new Bug. Fill all the Bug data as we use to fill it manually.
Note: - Pass the entire compulsory field to log a Bug; otherwise it will generate an error.
New Bug Window
New Bug Window


'Function call to log the Bug
fn_LogBug("TestUser","TestPwd","UFT","Help")

'**********************************************************************************************
'Function Name:-        fn_LogBug
'Function Description:- Logging Bug in ALM using OTA 
'Input Parameters:-     UserName,Password,Domain,Project( ALM)
'Output Parameters:-    None
'Created By :-          uftHelp Team
'***********************************************************************************************

Public Function fn_LogBug(sUName,sPwd,sDomain,sProject)
 Dim QCconn
 Dim BugFact,NewBug
 
 Set QCconn = CreateObject("TDApiOle80.TDConnection")
 'Intilizing the QC connection
 QCconn.InitConnectionEx "https://alm.com/qcbin"
 'Verifying User is connected
 If QCconn.Connected Then
  Print "User Connected to ALM Server"
 Else
  Print "User is not Connected to ALM Server"
  Exit Function 
 End If
 'Filling the UserName and Password for ALM
 QCconn.Login sUName,sPwd
 'Verifying User is Logged into ALM
 If QCconn.LoggedIn Then
  Print "User is Logged into ALM Server"
 Else
  Print "User is not Logged into ALM Server"
  Exit Function 
 End If
 'Connects user to specified Domain and Project
 QCconn.Connect sDomain,sProject
 
 'Using BugFactory object to log a defect
 Set BugFact = QCconn.BugFactory
 Set NewBug = BugFact.AddItem(Null)
 On Error Resume Next
 'Adding the Bug data
 With NewBug
  .Summary = "Issue in the Application"
  .DetectedBy = "TestUser1"
  .Field("BG_DETECTION_DATE") = Date
  .Status = "New"
  .Field("BG_DESCRIPTION") = "Testing"
  .Field("BG_DEV_COMMENTS") = "Ignore it is Test Bug"
  .Field("BG_PRIORITY") = "2-Medium"
  .AssignedTo = "TestUser2"
  .Field("BG_SEVERITY") = "3-High"
  .Field("BG_USER_02") = "Cycle 1"
  .Field("BG_USER_03")  = "Enhancement"
 End With
 'Adding the Bug to ALM
 NewBug.Post
 If Err.Number <> "0" Then
  Print "Error in logging the Bug = "&Err.Description
 Else 
  Print "Successfully Logged the Bug"
 End If
 'Destroying the objects
 Set NewBug = Nothing
 Set BugFact = Nothing
 Set QCconn = Nothing
End Function