Monday, July 15, 2013

Utility Objects

QTP provides several utility objects to enhance the power of scripting.
Below is a list of all QTP utility objects.

Crypt, DataTable, Description, DTParameter, DTSheet, Environment, Extern, OptionalStep, Parameter, PathFinder, Properties(Collection), QCUtil, Random Number, Recovery, Reporter, Services, Setting, TextUtil, TSLTest, XMLUtil

  •   Crypt Object
  •   OptionalStep Object
  •   PathFinder Object
  •   RandomNumber Object
  •   Setting Object
  •   Add Method
  •   Exists Method
  •   Remove Method
  •   WebUtil Object
  •   SystemUtil Object
  •   RegisterUserFunc Object


Saturday, July 13, 2013

Working With RegExp


The Regular Expression object provides simple regular expression support.

Properties

Global Property
IgnoreCase Property
Pattern Property

Methods

Execute Method
Replace Method

Test Method

'RegExp

Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = "[a-zA-Z0-9]"   ' Set pattern.
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.

'RegExp

lovePoem = "How do I love thee? Let me count the ways."
lovePoem = lovePoem & "I love thee to the depth and breadth and height"
lovePoem = lovePoem & "My soul can reach, when feeling out of sight"
lovePoem = lovePoem & "For the ends of Being and ideal Grace."
lovePoem = lovePoem & "I love thee to the level of everydays"
lovePoem = lovePoem & "Most quiet need, by sun and candle-light."
lovePoem = lovePoem & "I love thee freely, as men strive for Right;"
lovePoem = lovePoem & "I love thee purely, as they turn from Praise."
lovePoem = lovePoem & "I love thee with a passion put to use"
lovePoem = lovePoem & "In my old griefs, and with my childhoods faith."
lovePoem = lovePoem & "I love thee with a love I seemed to lose"
lovePoem = lovePoem & "With my lost saints, — I love thee with the breath,"
lovePoem = lovePoem & "Smiles, tears, of all my life! — and, if God choose,"
lovePoem = lovePoem & "I shall but love thee better after death."

'===============================================================
'Using RegExp — How to count the number of times the word “love” appears in the poem:
'
'To count the number of times a word (or any pattern) appears in a string, you can use QTP with VBScript’s RegExp object, along with the Pattern property and Execute method.
'
'The Pattern property can set or return any regular expression pattern you wish to search for.
'=============================================================
'The Execute method is used to run a search using a regular expression:
'=============================================================
Set re = New RegExp
 re.Global = True
 re.Pattern = "love"
 Set loveMatch = re.Execute(lovePoem)
 MsgBox loveMatch.Count

'==============================================================
'Replace Method
'
'You can also use the Replace method to count the appearances of a word or pattern.
'This method replaces the text found in a search using a regular expression.
'To replace all the occurrences of the word thee with you in the poem you can do the following:
'===============================================================

 Set re = New RegExp
 re.Global = True
 re.Pattern = "thee"
 MsgBox re.Replace(lovePoem,"you")


Function ReplaceTest(patrn, replStr)
  Dim regEx, str1               ' Create variables.
  str1 = "The quick brown fox jumped over the lazy dog."
  Set regEx = New RegExp            ' Create regular expression.
  regEx.Pattern = patrn            ' Set pattern.
  regEx.IgnoreCase = True            ' Make case insensitive.
  ReplaceTest = regEx.Replace(str1, replStr)   ' Make replacement.
End Function

MsgBox(ReplaceTest("fox", "cat"))      ' Replace 'fox' with 'cat'.

'In addition, the Replace method can replace subexpressions in the pattern. The following call to the function shown in the previous example swaps each pair of words in the original string: 

Monday, July 8, 2013

Working with WSCRIPT

Work with command prompt using QTP(vbscript) 
We can use Windows Shell object to start a command prompt and execute the command.

In the following i want to list all files\folders in C drive:
Set wscriptObj=createobject("WScript.Shell")
wscriptObj.Run "cmd /K cd c:\ & dir"

When i execute the above statement, it opens the command prompt and execute dir command.

You are free to use any command you want as per your needs.


Invoke Browser


Set WShelObj=Createobject("WScript.Shell")
WShelObj.Run Chr(34) & "C:\Program Files\Internet Explorer\iexplore.exe" & Chr(34)&" "&"www.yahoo.com"
or
Set WShelObj=Createobject("WScript.Shell")
WShelObj.Run Chr(34) & "C:\Program Files\Internet Explorer\iexplore.exe" & Chr(34)
Browser("name:=.*").Navigate "www.yahoo.com"
Char(34) is used to append double quotes to the IE application path.





Working with Web Links

'***************************** Dynamic descriptive Programing **********************************
'***************************** Page Used: Google - Products*************************************
'**************To Count all the Links on Web Page and Display their Names with URL**************

'Launch Google - Products
Systemutil.Run "iexplore.exe","http://www.google.com/intl/en/about/products/"

'Descriptive object to identify  Browser  with a particular title
Set  Dbrowser=Description.Create
Dbrowser("micclass").Value="Browser"
Dbrowser("name").Value="Google - Products"

'Descriptive object to identify  Web page with a particular title
Set  Dpage=Description.Create
Dpage("micclass").Value="Page"
Dpage("title").Value= "Google - Products"

'Descriptive object to identify a  particular Link
Set  DLink=Description.Create
'DLink("micclass").Value="Link"
DLink("html tag").Value="A"

'wait till browser sync
Browser(Dbrowser).Page(Dpage).Sync

Set objLinkCount=Browser(Dbrowser).Page(Dpage).ChildObjects(DLink) 'Returnz the childobjects having 'Linkobject' description.
vrDLinkCount = objLinkCount.Count  'Returns number of links on webpage
MsgBox vrDLinkCount

For i=0 to vrDLinkCount-1
vrTag = objLinkCount(i).GetROProperty("name")
vrHREF = objLinkCount(i).GetROProperty("url")
msgbox vrTag & ": " &vrHREF
Next

'*=================Static descriptive Programing ================
'====================With HTMLTag  ==========================

Set ObjDes = Description.Create() 'Creates a new Empty Description Object
ObjDes("html tag").Value = "A" 'Adding Properties and Values to Descript Object

Systemutil.Run "iexplore.exe","http://www.google.com/intl/en/about/products/"

Set ChildObjCount = Browser("name:=Google - Products").Page("title:=Google - Products").ChildObjects (ObjDes) 'Gets the count of all Child Objects based on added description
NumCounts = ChildObjCount.Count() ' Gets the count of Descript values
Reporter.ReportEvent micPass, "The Number of Links on this page is", "Links Count = "&NumCounts ' Prints the Count of Links of the Page in Test Results
For i = 0 to NumCounts - 1 ' Making a For Loop to get all Similar Descript Values
Htag = ChildObjCount(i).GetROProperty("innertext") ' Gets the inner text of the value
Href =ChildObjCount(i).GetROProperty("href") 'Gets the Href of the inner text
Reporter.ReportEvent micPass, "The Name of the Link with URL", "Name: "& Htag&" and Url:" & Href ' reports the Tag and Href values to Test Results
Next


'*=================Static descriptive Programing ================
'==================== With MicClass ==========================

Systemutil.Run "iexplore.exe","http://www.google.com/intl/en/about/products/"

set linkobject=Description.Create 'This line is used to create description object
linkobject("Micclass").value="link" 'This line is used to identify the property.( In our case its link)
set linkcount=Browser("name:=Google - Products").Page("title:=Google - Products").ChildObjects(linkobject) 'This line is used to return the childobjects having 'Linkobject' description.
a= linkcount.Count  'This line is used get number of links on a webpage
MsgBox a

For i=0 to a-1
tag = linkcount(i).GetROProperty("name")
href = linkcount(i).GetROProperty("url")
msgbox tag & ": " &href
Next

'==============Dynmic  Descriptive Programing ===================
' To Count  Number of Links with a Particular Name on a Web Page
'=============================================================


SystemUtil.Run "iexplore", "http://youngistaancafe.blogspot.com/2013/07/student-registration-formhtml.html"

'Descriptive object to identify  Browser  with a particular title

Set oBrowser = Description.Create
oBrowser("micclass").Value = "Browser"
oBrowser("name").Value = "Youngistaan Cafe: Student Registration Form.html"

Set oPage=Description.Create
oPage("micclass").Value="Page"
oPage("title").Value="Youngistaan Cafe: Student Registration Form.html"

Browser(oBrowser).Page(oPage).Sync

'Descriptive object to identify a  particular WebLink
Set oLink = Description.Create
oLink("micclass").Value = "Link"

Set oWebLinkCount = Browser(oBrowser).Page(oPage).ChildObjects(oLink)
vrCount = oWebLinkCount.Count

Counter = 0

'To Count the Number of Links with Name as "Home"

For i=0 to vrCount-1
vrLinkName = oWebLinkCount(i).GetROProperty("name")
       
If vrLinkName = "Home" Then
Counter = Counter +1
End If

Next

msgbox  "Number of lInks with name Home are: " &Counter

Friday, July 5, 2013

Working with WebList

'============= Ways to select value from WebList =======================

Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:=lr").Select "#3"
Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:=lr").Select (1)
Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:=lr").Select "English"


'========== To Count Number of WebList in a Page Using Mic Class =================

Set oWebList=Description.Create
oWebList("micclass").value="WebList"
set objList=Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").ChildObjects(oWebList)
vrCount = objList.Count

For i=0 to vrCount-1
msgbox objlist(i).getroproperty("name")
Next

'=========== To Count Number of WebList in a Page Using HTMLTag ==============

Set oWebList=Description.Create
oWebList("html tag").Value = "SELECT"
Set objList=Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").ChildObjects(oWebList)
vrCount = objList.Count

For i=0 to vrCount-1
msgbox objlist(i).getroproperty("name")
Next

'================================ To Get Values of  all items present in any particular WebList  ============================================

    vrItemCount = Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:=as_filetype").GetROProperty("items count")

For j=1 to vrItemCount
vrValue = Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:=as_filetype").GetItem(j)
msgbox vrValue
Next

'============== To Get All the Values of  all WebList  in a WebPAge===================

Set oWebList=Description.Create
oWebList("micclass").value="WebList"
set objList=Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").ChildObjects(oWebList)
vrCount = objList.Count

For i=0 to vrCount-1
vrWebListName = objlist(i).getroproperty("name")
vrItemCount = Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:="&vrWebListName).GetROProperty("items count")
msgbox vrItemCount
For j=1 to vrItemCount
vrValue = Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:="&vrWebListName).GetItem(j)
msgbox vrValue
Next
Next

'===========  To Check Existence of Item in Web List ======================

Function fnWebLIstItemExistence(vrSearchItem)

vrItemCount = Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:=as_filetype").GetROProperty("items count")
msgbox vrItemCount


For j=1 to vrItemCount
vrWebItem = Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebList("name:=as_filetype").GetItem(j)
If vrSearchItem = vrWebItem Then
Reporter.ReportEvent micPass, "Result", "vrSearchItem Exist in the Web List"
Else
Reporter.ReportEvent micFail, "Result", "vrSearchItem not Found in the Web List"
End If
Next

End Function


Call fnWebLIstItemExistence("Shockwave Flash (.swf)")


' ======= To Select more than one Item in a WebList ==============================

'The following example uses the ExtendSelect method to select two additional items from a WebList object and then checks
'if the items were actually selected. The results of the
'check are sent to the Test Results.

Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").Select "Rugby Shirt"
Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").ExtendSelect "Leather Jacket"
Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").ExtendSelect "Boots"

'Check if all items that were selected in the previous steps are
'actually selected in the list and send

CurrentSelection = Browser("Find a Flight: Mercury").Page("Fill-Out Form Example").WebList("what-to-wear").GetROProperty("selection")
If CurrentSelection <> "Rugby Shirt;Leather Jacket;Boots" Then
    Reporter.ReportEvent micFail, "ExtendSelect", "The list does not contain all of the selected items."
Else
   Reporter.ReportEvent micPass, "ExtendSelect", "The list contains all selected items."
End If

'============= To check if Items in a WebList are sorted or not=================



Working with WebCheckBox

'============= Ways to select value from WebCheckBox=======================

Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebCheckBox("name:=lr").Set"ON"
Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").WebCheckBox("name:=lr").Set "OFF"

'========== To Count Number of WebCheckBox in a Page Using Mic Class =================

Set oWebCheckBox=Description.Create
oWebCheckBox("micclass").value="WebCheckBox"
set objCheckBox=Browser("name:=Google Advanced Search").Page("title:=Google Advanced Search").ChildObjects(oWebCheckBox)
vrCount = objCheckBox.Count

For i=0 to vrCount-1
msgbox objCheckBox(i).GetROProperty("name")
Next

Thursday, July 4, 2013

Working with WebButton

'Working With WebButton

SystemUtil.Run "iexplore", "http://youngistaancafe.blogspot.com/2013/07/student-registration-formhtml.html"

Set oWebButton = Description.Create 'This line is used to create description object
oWebButton("micclass").Value = "WebButton"  'This line is used to identify the property.( In our case its link)
Set oWebButtonCount = Browser("name:=Youngistaan Cafe: Student Registration Form.html").Page("title:=Youngistaan Cafe: Student Registration Form.html").ChildObjects(oWebButton)  'This line is used to return the childobjects having 'WebButton object' description.
vrCount = oWebButtonCount.Count 'This line is used get number of buttons on a webpage
MsgBox vrCount

For i=0 to vrCount-1
                Button_Name = oWebButtonCount(i).GetROProperty("name")
    MsgBox  "Button: " &Button_Name 

Next

Tuesday, July 2, 2013

Working with WebEdit

'Working with Web Edit

'======== To Count Number of WebEdit in a Page Using MicClass ===============================

Set oWebEdit=Description.Create
oWebEdit("micclass").value="WebEdit"
Set oWebEditCount = Browser("name:=Youngistaan Cafe: Student Registration Form.html").Page("title:=Youngistaan Cafe: Student Registration Form.html").ChildObjects(oWebEdit)
vrCount = oWebEditCount.Count
msgbox vrCount

For i=0 to vrCount-1
print oWebEditCount(i).getroproperty("Name")

Next