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

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

WebTables in UFT - 10 Key points

1. WebTable or html grid are integral part of html pages and a lot of application data is organised in WebTables. QTP provides different methods to interact with the webtable in the web application. In this remaining article, we will discuss key points to interact with the webtables. 
Below image shows how a webTable is divided into column and rows and data can be extracted from cell in the web table.


2. Verify if the table exist in the application
istbleExist=Browser("qaaqtp").Page("title:=.*").WebTable("name:= DataTable").exist

3. In case the table exist in the Page, we can get the count of rows and columns in the application as shown below:

iRowCnt=Browser("qaautmationqtp").Page("title:=.*").WebTable("name:= DataTable").RowCount
'' for each of the  row, get the column count for each row
For rownum=1 to iRowCnt
 iColCnt=Browser("qaautmationqtp").Page("title:=.*").WebTable("name:=DataTable").ColumnCount(rownum) 
 MsgBox iColCnt
Next

4. Now we know the number of rows and columns in the table, we can extract cell information using the code below:
CellData= Browser("qaautmationqtp").Page("title:=.*").WebTable("name:=DataTable").GetCellData(rownum,colcnt)

5. We can perform action on object of specific type in a cell in the webTable based on the cell
ibtnCnt=Browser("qaautmationqtp").Page("title:=.*").WebTable("name:=DataTable")
.ChildItemCount(4,3,"WebButton")
This will get the number of child items in a particular cell of particular type.

6. This will click on the 2nd  webbutton in row 4 and column 3 of the webtable.
Browser("qaautmationqtp").Page("title:=.*").WebTable("name:=DataTable").ChildItem(4,3,"WebButton",1).click

7. Using HTML DOM also, we can get count of objects in a webtable
ielemCnt = Browser("Google").Page("title:=.*").WebTable("name:=DataTable").object.getElementsbyTagName(strTagName).length

8. We can get row count of element based on the cell text as below:
irownum = object.GetRowWithCellText ("text to search",columnnum, startfromrownum)

9. We can work with webtable together with dynamic descriptive programming as shown below:
Set objDesc = Description.Create
objDesc("micclass").value = "WebElement"
objDesc("html tag").value = "TD"
Set childobjdesc=Browser("Google").Page("title:=.*").WebTable("name:=DataTable")ChildObjects(objDesc)

10. Using ChildItemCount and childItem, we can extract information from webTable as shown below:

Public Function findObjectinWebTable(strobjType,iRow,iCol)
 ''get child count of specific object type
 oChildCnt=Browser("qaautmationqtp").Page("title:=.*").WebTable("name:=DataTable").ChildItemCount(iRow,iCol,strobjType)
 If oChildCnt >0
  ''Perform action based on object type
  Set ChldItem = Browser("qaautmationqtp").Page("title:=.*").WebTable("name:=DataTable").ChildItem(iRow,iCol,strobjType,0)
 If (strobjType = "Link" or strobjType ="WebEdit") Then
  ChldItm.Click
  ElseIf(stobjType = "WebCheckBox") Then
  ChldItm.Set "ON"
 End If
End Function

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

Code to get title of all open browsers in QTP/UFT

We can get titles of all the open browser open using descriptive programming in QTP as explained in the code below:


Function GettitleAllbrowsers()
GettitleAllbrowsers = ""
Set objBrowser = Description.Create
objBrowser("micclass").Value = "Browser"
''Get all browsers instances in ObjBrowserLst 
Set objBrowserLst= Desktop.ChildObjects(objBrowser)
''Get the count of all the browsers open
browsercnt = objBrowserLst.count
For i=0 to objBrowserLst.count-1  
'Store title of the Page in a variable 
GettitleAllbrowsers = GettitleAllbrowsers + ">"+objBrowserLst(i).GetROProperty("title") 
Next 
Set objBrowser = nothing
Set objBrowser = nothing
End Function


For further manipulating the browser details using descriptive programming. Please go through the below articles:


Code to close multiple browsers except Quality Center/ALM in UFT/QTP?

How to close the IE browsers and Internet Explorer not responding windows forecefully -VBScript

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




Code to close multiple browsers except Quality Center/ALM in UFT/QTP?

Below Code shows how to close all browsers except browser based on the title of the browser in UFT / QTP.


Function CloseAllOpenbrowsersExcept(browsertitle)
Set oBrowser = Description.Create
oBrowser("micclass").Value = "Browser"
''Get all browsers open 
Set oBrowserLst= Desktop.ChildObjects(oBrowser)
For i=0 to oBrowserLst.count-1  
'Verify the title of the browser and close the browser if it does not the title passed
'in the function
  If InStr(oBrowserLst(i).GetROProperty("title"), browsertitle) = 0 Then
    oBrowserLst(i).close  
  End If  
Next 
Set oBrowser = nothing
Set oBrowser = nothing
End Function


Similarly we can tweak the above code to close only a particular browser as shown below:


Function CloseOpenbrowserWithTitle(browsertitle)
Set oBrowser = Description.Create
oBrowser("micclass").Value = "Browser"
''Get all browsers open 
Set oBrowserLst= Desktop.ChildObjects(oBrowser)
For i=0 to oBrowserLst.count-1  
'Verify the title of the browser and close the browser if it does not the title passed
'in the function
  If InStr(oBrowserLst(i).GetROProperty("title"), browsertitle) <> 0 Then
    oBrowserLst(i).close  
  End If  
Next 
Set oBrowser = nothing
Set oBrowser = nothing
End Function


Similarly we can close all browsers by removing the condition to check for title in the above code.


Function CloseAllOpenbrowsers()
Set oBrowser = Description.Create
oBrowser("micclass").Value = "Browser"
''Get all browsers open 
Set oBrowserLst= Desktop.ChildObjects(oBrowser)
For i=0 to oBrowserLst.count-1  
'Verify the title of the browser and close the browser if it does not the title passed
'in the function
      oBrowserLst(i).close  
Next 
Set oBrowser = nothing
Set oBrowser = nothing
End Function

Difference between Actions and Functions in QTP/UFT 11.5


Comparison between Action and functions in QTP/ UFT 11.5 is explained in this article. We discuss on how action and functions are similar and difference between two in UFT 11.5/ QTP



Parameter
Action
Functions
Feature Type
Action is a feature provided by QTP/UFT
Function is a feature of VBScript
Return Value
We can pass as well as return multiple values using input and output parameters
A function returns a single value although values of multiple variable can be passed using public variable with global scope
Assets
An Action has local repositories, datatable, Active screen etc  associated with it
Function cannot have local repositories, datatable  and other QTP features associated with it
Multiple function can be created in an action
Multiple Actions cannot be created in a function.
Size and Performance
Size of the test increases with increase in number of actions as there is data table, OR associated with each of the test.
Performance of using function is better compared to action and consumes less size.
Actions
An Action is associated with a single test
Function library can be associated with multiple tests and can be loaded at runtime.
Maximum Allowed
There can be maximum 120 actions in a test and If any test has 120 actions in it, it will definitely not a good test script
There is no limit of number of function used in a test.
Maintenance Cost
Maintenance cost is higher in case of Action, as we need to open each test to make change at action. While multiple function library being vbs files can be updated easily.
Similarity between the Two
The purpose of using Actions or function is to provide modularity to the test scripts and should be used to create structured tests.
Extension
Extension of action script is .mts while function library can have extension as .vbs, .qfl.



VBScript Code - Function to convert CSV file into excel and viceversa in QTP using VBScript

We at times are required to convert excel files into csv to read as flat files and sometime require to convert a csv file into excel file to use excel features on the data.

 
Below function shows how to convert an csv file into excel file and vice versa. We can also convert to other formats based on constants

Here constant value 23 is used to create a csv file and constant -4143 to save a file as xls file.Once the destination file is created, we can delete the source file as shown below. In case of any issue in understanding the code, please add in comment section


 Call func_ConversionCSVExcel("E:\Test.csv", "E:\Test_converted.xls", "csvtoexcel")  
 Public Function func_ConversionCSVExcel(strSrcFile, strDestFile, Conversion)  
 on error resume next  
 Set objExcel = CreateObject("Excel.application")  
 set objExcelBook = objExcel.Workbooks.Open(strSrcFile)  
 objExcel.application.visible=false  
 objExcel.application.displayalerts=false  
 If(Conversion = "ExceltoCSV") Then  
   objExcelBook.SaveAs strDestFile, 23  
 else  
   objExcel.ActiveWorkbook.SaveAs strDestFile,-4143  
 End If  
 objExcel.Application.Quit  
 objExcel.Quit    
 Set objExcel = Nothing  
 set objExcelBook = Nothing  
 Set objFSO = CreateObject("scripting.FileSystemObject")  
 objFSO.DeleteFile(strSrcFile)  
 Set objFSO =nothing  
 End Function  

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.