Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Showing posts with label QTP. Show all posts
Showing posts with label QTP. Show all posts

Environment variable in UFT : 10 Key points

1. Definition - Environment variables are variables used across QTP Scripts/ all actions in a script/recovery scenario/function libraries in a UFT script.
Understand them as global variables for UFT Test Scripts.

2. Types of Environment Variables:
Environment variables are of 2 types: 
a. Built in Variables
b. User Defined Variables

3. Built -in variables are defined in QTP and can provide information on the QTP scripts and the operating system.

4. User Defined variables are again be divided into two types: 
       a. User Defined - Internal - defined at test level by adding a new environment parameter.
       b. User Defined - External - imported for an external xml file with parameters and values for parameters defined in the external file.




5. Steps to add user defined environment variables:

a.) In the test in which environment variable are to be added, Go to file -> Settings -> Environment.
b.) In variable type, select user defined. 
c.) Click on + icon, and add a new parameter with name and value.

6. To import from external file, check checkbox to load from external file and provide the path of the external file.

7. Environment variable for external file can be imported through code as shown below:

fileName = Environment.ExternalFileName

if (fileName = "") Then

    Environment.LoadFromFile("C:\testEnv.xml")

End If

8. Access and update environment variable in the script:


''Acessing an environment variable:

Testval =  Environment.Value("testing")

''Updating an user defined variable

Environment.Value("testing") = "testing2"

9. Example of environment variable xml file


<Environment>
 <Variable>
  <Name>Environment1</Name>
  <Value>ValueA</Value>
 </Variable>
 <Variable>
  <Name>Variable2</Name>
  <Value>ValueB</Value>
 </Variable>
</Environment>


10. Useful Reference: 


http://gareddy.blogspot.in/2010/12/qtp-environment-variables.html

http://qaautomationqtp.blogspot.in/2013/05/environment-variables-in-qtp.html

Summary

environment variable summary


Properties for object identification in QTP

QTP learns the properties of test objects to uniquely identify the object at run time. There are predefined set of properties for object classes in each environment.


Object are classified as Object classes for each environment. For e.g. environment Standard Windows environment as shown below will have all the test object class defined and defined are the mandatory and assistive properties to identify the object. Also we can set smart identification On for the object and the default ordinal identifier.to identify the object. 


To have a look at Object Identification properties for an object, Navigate to Tools>Object Identifier.


Let us understand the object Identification Window:


1. Environment: Lists all the environment for which objects are defined.


2. Object Classes:
List all the controls available for the environment selected.


3. Mandatory Properties:
These are the properties QTP always learns for the test object class selected.


4. Assistive Properties: In case object is not uniquely identified using the mandatory properties, there are some assistive properties which are learned to uniquely identify the object.


5. Smart Identification
is false by default, but can be checked for identification.


6. Ordinal Identifer: In case object is not identified using the above properties , Ordinal identifier is used to identify the object

List of Regular Expression for UFT/QTP

Regular expression are used to match input text with the pattern provided by regular expression engine. A pattern consists of one or more character literals, operators, or constructs. Below table explains the different regular expression patterns to identify the input text matches the expected pattern


Regular expression are used to validate the input text provided with the expected pattern. e.g : validating date format is correct or telephone number  provided is in correct format.



Reg. Expression

Description

( .)

 A dot matches single character (.) eg test. will match dot followed by any single character.example: testa, test5 

 [ab]

Matches either of the character in the list. e.g 23[ab] will match both 23a or 23b but not 23r

[^ab]

Matches all the characters excluding the the character in the list. e.g 23[^ab] will not match both 23a or 23b but will match23r,235 or 23 followed by any character other

 \b

Matches a word at boundary. e.g test/b will match entest but not entester

 [x-y]

Matches a single character in the range. e.g 23[a-d] will match 23a,23b,23c,23d but not 23r

 [^x-y]

Matches a single character not in the range. e.g 23[^a-d] will not match 23a,23b,23c,23d but 23r

 \B

Matches a word not at boundary. e.g test/b will match entest but not entester

 \W

Matches any non-alphaNumeric character excluding underscore

 \w

Matches any alphaNumeric character including underscore

*

Wildcard character matching the multiple occurence of preceding character. e.g rat* will match ra, rat, rattt, ............i.e multiple occurence of preceeding character t in this example.

 .*

Wildcard character matching the multiple occurence of preceding character.Preceeding character is . in case, so ra.* will match any string starting with ra

+

Similar to * but atleast one occurence of the character should match.e.g : rat+ will match rat but not ra

 ?

 matches zero or one occurrences of the preceding character. For example: ra?t match rt or rat only

 |

Matches either of the character seperated by |.example nes(t|l)ey will match nestey or nesley.

 \d

Matches a digit character

\D

Matches a non- digit/numeric character

\

marks the next character as special character or a literal. e.g \n is for new line ; \\ for \ and \d for a digit character

^

matches the pattern at start of line. e.g: ^test will match testing, tester but not autotest.

 $

matches the pattern at end of line. e.g: test$ will match autotest but not tester.

 |

Matches either of the character seperated by |.example nes(t|l)ey will match nestey or nesley.

 {}

Matches the start and end of qualifier expression. e.g a{4,5} will match aaaa or aaaaa

Reference:  Regular Expression Language - Quick Reference

Types of Environment variables in QTP / UFT

Below are the types of environment variables in QTP


1) Built-in - Used to extract test specific and machine/OS information. E.g ActionName, LocalHostName and OS to name a few.


2) User-defined Internal - Defined for the test and available only to the test in which it is defined.


3) User-defined External - Can be used across tests and can be loaded from an external xml file dynamically at run time or at the test level.


More Details on this topic, click on Environment Variable Loading




Simulating Keyboard and mouse operations in UFT using settings.replay type

In QTP, We can replicate device events, i.e keyboard and mouse operation using the below line of code.

Setting.WebPackage("ReplayType") = 2

By default, value of the Setting is 1, in which UFT interacts with application using the browser through DOM.

Setting.WebPackage("ReplayType") = 1

In case, you are not able to perform click or operation on an element in the page, please give a try by providing setting:

 Setting.WebPackage("ReplayType") =2

Renaming all/specific subfolders in a folder using vbscript FSO

In this post, how to rename all the subfolders in a folder using vbscript is explained

  • Renaming all sub folders in a folder removing spaces from the file

call RemoveblankSpace_Subfolders("D:\TextTest")

 ''strbasefoldername - Folder Path in which file needs to be replaced
 Public Function RemoveblankSpace_Subfolders(strbasefoldername)
 On error resume next
 Dim objFSO
 Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Dim objFolder 
    For Each objSubFolder In objFSO.GetFolder(strbasefoldername).SubFolders
     FlderName = objSubFolder.Name
  FlderName = replace(FlderName," ","")
  objSubFolder.Name = FlderName
    Next 
    Set objSubFolderFolder = Nothing 
    Set objFSO = Nothing 
End Function

  • Rename specific subfolder in a folder using vbscript


call Rename_Subfolders("D:\TextTest","es","twist",false,"test")

'' strbasefoldername - Folder Path in which file needs to be replaced
'' strFindtext - text in the folder name that needs to be replaced
'' strReplaceText - new text to replace the existing test
'' boolAllFlders - boolean flag to indicate if all the subfolder needs to be renamed or
'' specific folders needs to be renamed
'' OnlyfoldersWithText - replace only folder with specific text. will consider only if
'' boolAllFlders =true
 Public Function Rename_Subfolders(strbasefoldername,strFindtext,strReplaceText,boolAllFlders,OnlyfoldersWithText)
 On error resume next
 Dim objFSO
 Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Dim objFolder
    For Each objSubFolder In objFSO.GetFolder(strbasefoldername).SubFolders
 FlderName = objSubFolder.Name
  if boolAllFlders = false then
   if(instr(1,FlderName,OnlyfoldersWithText)>0) then
    FlderName = replace(FlderName,strFindtext,strReplaceText)
    objSubFolder.Name = FlderName
   End If
  else
    FlderName = replace(FlderName,strFindtext,strReplaceText)
    objSubFolder.Name = FlderName
  End If
    Next 
    Set objSubFolderFolder = Nothing 
    Set objFSO = Nothing 
End Function

To view or not the test results in UFT

Once a test is executed in QTP/UFT, test results are displayed. UFT provides us the option to view the test results once the test execution completes. Follow following steps for the same.
  • Navigate to Options>General.
  • There is checkbox "View results when run session ends". In case the checkbox is checked, UFT results are launched automatically once the test execution completes. In case not checked, the results are not launched automatically once execution completes.


viewrunresults

Sorting excel data multiple times based on column Name using VBScript


''Input Information - Workbook file Name with path
''WorkSheet Name
'' Provide the sort criteria based on which data needs to be filtered seperated by |

WorkBookFile = "C:\\testing.xls"
SheetName = "testing"
MultipleSortCriteria = "TestName|TestClass|TestPath"
'' Define the constants to be used in the script
  Const xlYes = 1
  Const xlAscending = 1
  Const xlDescending = 2
  Set oXL = CreateObject("Excel.Application")
  Set oWB = oXL.WorkBooks.Open(WorkBookFile)
  Set oWS = oWB.Worksheets(SheetName)  
  Set oRnge = oWS.UsedRange
  ColCnt = oRnge.columns.count
  '' Create an array based on the sort criteria provided 
  aSrtCriteria = split(MultipleSortCriteria,"|")
  For i = ubound(aSrtCriteria) to 0 step -1
  ''Loop the times data needs to be sorted
   For j = 1 to ColCount step 1
    
    If (oWS.cells(1,j).value =aSrtCriteria(i)) Then
     '' Sort the data based on the column name
     Set oRngeSrt = oXL.Range(Chr(asc("A")- 1+j) & "1")
     oRnge.Sort oRngeSrt, xlDescending, , , , , , xlYes 
     oWB.save
      Exit For
    End If    
   Next
  Next
   oWB.Close
   oXL.Quit
   Set oWB = Nothing
   Set oXL = Nothing
 

Manipulating and working with Word document using vbscript in QTP

In this article, we will discuss how to manipulate microsoft word document using QTP and VBScript. Learning working with microsoft word can help the users to generate reports of test results in word document . We will discuss on code for following problems in this article 

a. Creating a new Word document using vbscript in QTP
b. Opening an existing word document
c. Appending at the beginning and end of a word document. 
d. Reading content of a word document.
e. Formatting text of a word document.

We will discuss on capturing images and working with table in MS word using vbscript in some another article: 


Example 1 - Creating a new word document using vbscript. Writing some text in the document and closing the same


' Instantiate an object of word application 
Set objWord = CreateObject("Word.Application")
' Add a new item of word application object
objWord.Documents.Add
' Adding some text in the word document
objWord.Selection.TypeText "testing time"
' Save the document as provided
objWord.ActiveDocument.SaveAs "C:\qtptesting.docx"
' Close instance of word application
objWord.Quit
' Release the object for word document.
Set objWord=Nothing

Example 2 - The above was example of adding a new document in case we wish to update an existing document, we can use the below code:


' Instantiate an object of word application 
Set objWord = CreateObject("Word.Application")
' Open an existing word document
objWord.Documents.open "C:\qtptesting.docx"
' Adding some text in the word document
objWord.Selection.TypeText "testing time 2"
' Save the document as provided
objWord.ActiveDocument.Save
' Close instance of word application
objWord.Quit
'Release the object for word document.
Set objWord=Nothing


Example 3 : Now we have seen the text in above example 2 writes "testing time 2" just before testingtime. Suppose we want to write the text testing 2 in a new line after text testing, we can write the code as



Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"

' Adding some text in the word document at the end of document:
objWord.Selection.EndKey 6,0
objWord.Selection.TypeText vbCrLf & "testing time 3"

objWord.ActiveDocument.Save
' Close instance of word application
objWord.Quit
'Release the object for word document.
Set objWord=Nothing

Example 4 : Next we want to format the text in the word document.


Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"
' Adding formatting to the object, there will be lot of method to format , can refer to reference vbscript in QTP or word.
objWord.Selection.Font.Size ="18"
objWord.Selection.Font.Name ="18"
objWord.Selection.Font.bold = true
objWord.Selection.EndKey 6,0
' formatting changes in the code completes
objWord.Selection.TypeText vbCrLf & "testing time 3"
objWord.ActiveDocument.Save
' Close instance of word application
objWord.Quit
' Release the object for word document.
Set objWord=Nothing

Example 5: Reading content of a word document and storing in a string


Set objWord = CreateObject("Word.Application")
objWord.Documents.open "C:\qtptesting.docx"
set objdoc = objWord.Activedocument
strWordtext = ""

' read the number of words in the document and loop through the number of characters
iWordCnt= objdoc.Words.count
For i = 1 to iWordCnt
strWordtext = strWordtext + objdoc.Words(i)
Next
msgbox strWordtext
' specific changes in the code completes

objWord.Quit
'Release the object for word document.
Set objWord=Nothing

All about working with browser object in QTP

This article discusses some useful code snippets working with browser object in QTP and useful tips while working with browser in QTP.

1.  How to launch a webpage in any of the browser.

Well, there are multiple ways to launch a webpage in browser of choice

a.  Using Systemutil command - SystemUtil.Run is an inbuilt QTP command used to launch an application. We can launch a browser using Systemutil as:

                  SystemUtil.Run "Chrome.exe", “http://www.thoughtscollectionme.blogspot.in/

Using systemutil.run we can launch other application, e.g.: we can open a word document

                  SystemUtil.run “winword.exe”, “C:\meraword.docx”

Other methods for Systemutil command are:

CloseProcessByName: Closes the process based on the name of the process. E.g.: If we want to close chrome in the machine, we can use:
                 SystemUtil.CloseProcessByName "chrome.exe"

CloseProcessByHwnd: Closes the process based on the window handle of the process.
                  SystemUtil.CloseProcessByHWND handleid

b.  Another method is to launch the browser using Wscript.shell object. The code to launch the application using wscript.shell object is as follows:

                 Set objShell = CreateObject("Wscript.shell")
                 objShell.run “chrome.exe  http://www.thoughtscollectionme.blogspot.in/
                 set objShell = Nothing

    2. How to know how many browsers instances are open at a particular instance:

We can get the number of open instance of browser using below function
  Function getbrowsercount()
               ‘’Create an object of description class
              Set objBrowser = Description.Create
             ‘’ We have created an description object with micclass as browser
             ObjBrowser(“micclass”).Value = “Browser”
            ‘’objbr stores collection of objects which matches property of micclass as browser
             Set objbr = Desktop.ChildObjects(ObjBrowser)
           ‘’ return the count of browsers in the function itself.
            getbrowsercount = objbr.count
            End Function

3. How to know the title of each of the  open browser

     Set objBrowser = Description.Create
    ObjBrowser(“micclass”).Value = “Browser”
    Set objbr = Desktop.ChildObjects(ObjectBrowser)
    getbrowsercount = objbr.count
    For i= 1 to objbr.count
   ’’Loop through each of the browser and print the title of browser in message box
            Msgbox objbr(i).getRoproperty(“title”)
    Next

    4. Now we need to close all the open browsers?

  ‘’We use ordinal identifier to identify an test object by assigning a numerical    value which indicates object’s  location or order relative to its group. Different types of Ordinal identifier are:
·         Index
·         Location
·         CreationTime.
While Browser(“Creationtime:=0”).exist(0)
Browser("creationtime:=0").Close
Wend

 5. How to close all browser except Quality Center?

When we run a test in QTP, we do not require QC to close but other browsers to close. The code can easily be derived using the codes explained above.
The code will be something like:
Function CloseAllbrowsersExceptIE(browsertitle)
Set objBrowser = Description.Create
objBrowser("micclass").Value = "Browser"
'Get all browsers
Set objBrowserLst= Desktop.ChildObjects(objBrowser)
For i=0 to objBrowserLst.count-1  
 'Verify the title of the browser contains "Quality Center"  
If InStr(objBrowserLst(i).GetROProperty("title"), "Quality Center”) = 0 Then
                                objBrowserLst(i).close  
            End If  
            Next 
Set objBrowser = nothing
Set objBrowser = nothing
End Function

6. Next question is how to close only a particular browser only and keeping the other browsers open.


The code is very much similar to above problem where we close all the browser except QC.
Just need to change the if statement in the code above:

If InStr(objBrowserLst(i).GetROProperty("title"), "Quality Center”) = 0
to
If InStr(objBrowserLst(i).GetROProperty("title"), "Quality Center”) > 0

will close only the particular browser and keep the other browser open.


Working with add-ins in UFT/ QTP

QTP add-ins helps to identify objects in a variety of development environments. For e.g. if we require to create tests for an application in Java environment, we need to be install add-in for Java to identify objects in Java application. To use an add-in, we need to install the add-in in UFT.


How is add-ins installed and used in QTP/UFT?


Add-ins can be installed together with UFT installation or modify the existing UFT installation. Using Add-in for a particular requires the add-in to be installed. When UFT opens, a dialog box opens which asks which add-ins is to be loaded as shown below. For better performance only the add-ins which is used in the test session should be loaded when QTP/UFT is launched.

Once an add-in is added from UFT Add-in Manager, we can define the run and record setting for the add-in. below image shows the record and run setting for Java add-in. In case we want to do testing on any open application, and not to open a fixed pre-defined application, use radio button for not recording and run on any open java application.



UFT includes built-in support for testing standard Windows application. In case the required add-in not loaded, QTP tries to identify the object as standard window object. With UFT 11.5, No separate license is required for the add-ins and can use any of the add-ins to work with.

Understanding different type of add-ins - The add-ins are broadly classified as :


Web-based Add-ins – 

  • Add-ins designed to support special objects that are generally available in Web applications.
  • Most of the capabilities of objects in this category are identical or similar.

 Web-based add-in used in UFT 11.5 are:

  •  .Net Web Forms Add-in  The .NET Add-in functions like a Web-based add-in when testing .NET Web Forms controls.
  • PeopleSoft Add-in    Identify and test PeopleSoft user-interface object
  • Siebel Add-in   Identifies Siebel objects in the application. Using Siebel Add-in provides the user to use Siebel Test Express to automatically generate a new shared object repository, or to update an existing object repository.
  • .NET Silverlight Add-in   Identifies and test silverlight application.
  • Web 2.0 Toolkit Support
  • Web Add-in
  • Web based SAP Support
  •  Oracle Add-in

 Windows based Add-ins –

  • Add-ins designed to support special objects that are generally available in Window applications.
  • Most of the capabilities of objects in this category are identical or similar.

 Window based Add-ins in UFT 11.5 are:

  • ActiveX Add-in
  • Delphi Add-in
  • .Net Windows form Add-in
  • Power builder Add-in
  • Qt Add-in
  • Windows based SAP Support
  • Stringray Add-in
  • Terminal Emulator Add-in
  • Visual Age Smalltalk Add-in
  • Visual basic Add-in. 

Java Add-in   provides Java test objects, methods, and properties that can be used when testing objects in Java applications.

Flex Add-in – Using Flex Add-in provides Flex test objects, methods, and properties, Flex objects in Flex applications are identified and automated.

In case the objects in the environment are not identified properly using the add-ins, we can use extensibility to identify the objects.