Thursday, 16 July 2015

REAL TIME QTP SCRIPTS FOR EXPERTS PART2


How to import data from an Excel File to DataTable in QTP. Only import rows of data in Excel that are not empty rows.

[First delete empty rows as below and them import data.]

Set objExcel = CreateObject("Excel.Application")
objExcel.WorkBooks.Open "C:\Documents and Settings\thirumalv\Desktop\thi.xls"
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
rowcount=objSheet.usedrange.rows.count

For i = 1 To rowcount

If objSheet.cells(i,1).value="" Then
objSheet.Rows(i).delete
i=i-1
rowcount=rowcount-1
End If

If rowcount =i Then
Exit for
End If

Next

objExcel.ActiveWorkbook.save
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
Set objExcel = Nothing
Set objSheet = Nothing 




a) .How to Load an XML File in QTP?

'Microsoft.XMLDOM is a COM object of Microsoft's XML parse
 
Set objxml=createobject("Microsoft.XMLDOM")
'Async property specifies whether asynchronous download of the document is permitted or not
objxml.Async = False
'objxml.Load"Path of the file"
objxml.Load"D:\Datafile.xml"

qtp script to return specific weekday name-VBScript Weekdayname function-script in qtp to display specific weekday name-QTP Scripts-VBScript examples

Dim a

a=WeekDayName(1)

Msgbox a


Output-Sunday


Dim a

a=WeekDayName(5)

Msgbox a


Output-Thursday


Qtp script to return year from specified date-vbscript year function-script in qtp to display year from specified date-scripts in qtp-vbscript

Returns a whole number representing the year

Dim a

a=Year(Now)

Msgbox a


Output-2011



Dim a

a=Year("June 16, 2020")

Msgbox a


Output-2020


qtp script to return second of the minute-vbscript second function-scripts in qtp-VBSCRIPT Functions with examples and outputs

Returns a whole number from 0 to 59


Dim a

a=Second(Now)

Msgbox a



Output-25


Descriptive Programming to check all checkboxes in a webpage

script to check all checkboxes in a webpage.

Set a=Description.Create
a("html tag").value="input"
a("type").value="checkbox"

Set b=Browser("name:=").Page("title:=").childobjects(a)
c=b.count
msgbox c

For i=0 to c-1
b(i).set "on"
Next

Descriptive Programming To Count No. Of Links In A Webpage

SystemUtil.Run "www.funandknowledge.blogspot.com"
Set aa=description.Create
aa("htmltag").value="A"

set bb=Browser("name:=Fun.*").Page("title:=Fun.*").childobjects(aa)
cc=bb.count

msgbox cc


Descriptive Programming to count and close all open browsers

Script to get count,names of all open browsers and to close them.

Set b=Description.Create
b("micclass").value="Browser"
Set obj=Desktop.ChildObjects(b)
msgbox obj.count
For i=0 to obj.count-1
c=obj(i).getroproperty("name")
msgbox(c)
obj(i).Close
Next



QTP Script to write multiplication table

Script to write multiplication table of 5 in notepad.

Set f=createobject("scripting.filesystemobject")
set g=f.createtextfile("d://xyz.txt")
For i=1 to 10
g.writeline i&"*5="&i*5
Next

createtextfile("d://xyz.txt") creates notepad by name "xyz"in D drive 
output of this script in notepad will be
(path-d://xyz.txt) 

1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40
9*5=45
10*5=50

D.P for checkbox

Script to check whether yahoologinpage(www.yahoomail.com)checkbox is checked or not

Set g=Browser("name:=Yahoo.*").Page("title:=Yahoo.*")
Set obj=Description.Create
obj("htmltag").value="Input"
obj("ClassName").value="webcheckbox"
obj("type").value="checkbox"
Set a=g.childobjects(obj)
c=a(0).getroproperty("checked")
msgbox(c)
If c=0 Then
msgbox "checkbox is not checked"
else
msgbox"checkbox is checked"
End If

Descriptive Programming for Yahoo Login Page

SystemUtil.Run"iexplore","http://www.yahoomail.com"
Set g=Browser("name:=Yahoo.*").Page("title:=Yahoo.*")
g.WebEdit("name:=login").Set "aaa"
g.WebEdit("name:=passwd").SetSecure "bbb"
g.WebButton("name:=Sign In").Click
g.Link("name:=Inbox.*",
"html id:=WelcomeInboxFolderLink").Click
g.Link("name:=Sign Out").Click

SystemUtil.Run"iexplore","http://www.yahoomail.com" statement opens yahoo login page
aaa=username
bbb=password


d.p to create folder in system

Descriptive Programming to create folder in System.

Set obj=createobject ("scripting.filesystemobject")
Set notepad=obj.createfolder("d:\\abc")

this script will create a folder named "abc" in "d" drive.


QTP Script to Get Names of Subfolders in a Folder

Script in QTP to get Collection(names) of Subfolders in a Folder.

Below script is to get Subfolders names in Folder V1
Where Folder V1 is in Fdrive.
Dim a,b, c, d, e
Set a = CreateObject("Scripting.FileSystemObject")
Set b = a.GetFolder("F:\V1")
Set c = b.SubFolders
For Each d in c
e=e&d.name&vbnewline
Next
msgbox e

QTP Script to Export Database Data to Excel Sheet

Import data from database to datatable

Script to export database data to excel sheet in qtp.

Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.provider="microsoft.jet.oledb.4.0"
con.open"d:\db.mdb"
rs.open"select*from EMP",con
Set ex=createobject("Excel.Application")
Set a=ex.workbooks.open("D:\Lak.xls")
Set b=a.worksheets("sheet1")
i=1
Do While Not rs.EOF
b.cells (i,1).value=rs.fields("empno")
b.cells(i,2).value=rs.fields("empname")
b.cells(i,3).value=rs.fields("empsal")
rs.movenext
i=i+1
Loop
a.save
a.close

Above written script is for exporting database data to already exisiting excel sheet.
Database has a table called EMP with columns empno,empname,empsal.The values of these columns are exported to excel sheet.


Script to Get Cell Value of Particular Row and Column from Data Table in QTP

How to get data from a particular row and a particular column from Data Table in QTP?

Check this Post for Datatable Methods.

QTP Script to get particular cell value from datatable in the specified row.

Below script is for retrieving 2rd row,2rd column cell value from datatable.

In datatable we have values as shown below

A B C
1 QQ xx
2 TT yy
3 PP zz

Script to get value "TT" from datatable is

Method 1

Syntax:Datatable.getsheet("Sheet Name"/SheetId).getparameter("Column Name").valuebyrow(Row Number)

msgbox Datatable.getsheet("Global").getparameter("B").valuebyrow(2)

Method 2

Syntax:datatable.SetCurrentRow(rownumber)

Cell Vaue=datatable.Value("ColumnName"/ColumnNumber,"SheetName"/Sheet Id)

datatable.SetCurrentRow(2)

msgbox datatable.Value(2,1)


Script to Import Data From Database to Datatable in QTP

Below Script is for importing database(MS Access)data to runtime datatable in QTP.

Dim con,rs
Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")
con.provider="microsoft.jet.oledb.4.0"
con.open"d:\db.mdb"
rs.open"select*from EMP",con
datatable.GetSheet(1).addparameter"EMP_No",""
datatable.GetSheet(1).addparameter"EMP_Name",""
datatable.GetSheet(1).addparameter"EMP_Sal",""
row=1
Do While Not rs.EOF
datatable.SetCurrentRow(row)
datatable.Value (1,1)=rs.fields("empno")
datatable.Value(2,1)=rs.fields("empname")
datatable.Value(3,1)=rs.fields("empsal")
row=row+1
rs.movenext
Loop

"db" is one of the database in MS Access and "EMP" is table in db.
EMP Table has 3 columns(empno,empname,empsal).

empno empname empsal
1 aaa 1000
2 bbb 2000
3 ccc 3000

After running the script u can see EMP table data in runtime datatable.

Output:
EMP_No EMP_Name EMP_Sal
1 aaa 1000
2 bbb 2000
3 ccc 3000

QTP Script to Get Created Date and Time,Modified Date and Time & Accessed Date and Time of a File.

How to get created date and time of a particular file?

Set f1 = CreateObject("Scripting.FileSystemObject")
Set f2 = f1.GetFile("E:\Lak.txt")
S = "File was Created on: "&f2.DateCreated
Msgbox S

Output->File was Created on: 10/12/2008 3:45:16 PM

How to get last Modified date and time of a particular file?

Set f1 = CreateObject("Scripting.FileSystemObject")
Set f2 = f1.GetFile("E:\Lak.txt")
S = "File was Last Modified on: "&f2.Datelastmodified
Msgbox S

Output->File was Last Modified on: 10/12/2008 3:51:24 PM

How to get last accessed date and time of a particular file?

Set f1 = CreateObject("Scripting.FileSystemObject")
Set f2 = f1.GetFile("E:\Lak.txt")
S = "File was Last Accessed on: "&f2.Datelastaccessed
Msgbox S

Output->File was Last Accessed on: 10/12/2008 3:55:15 PM




Navigation To Get Line Numbers In Expert View

Follow below navigation to get line numbers in expert view(QTP).


Tools -> Editor Options -> General tab -> Check show line numbers check box and click on OK button.


Descriptive Programming to Capture Tool Tip Text in QTP

Tooltip:A tooltip is a small pop-up window that appears when a user pauses the mouse pointer over an element.

QTP Script to capture tooltip.
Below script is for capturing tooltip of textbox in google Site.
SystemUtil.Run"iexplore","http://www.google.com/"
a=Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Object.title
msgbox a

QTP Script to capture tooltip of image.
SystemUtil.Run"iexplore","http://www.yahoomail.com/"
b=Browser("name:=Yahoo.*").Page("title:=Yahoo.*").Image("src:=https://a248.e.akamai.net/sec.yimg.com/i/reg/bnr_28.jpg").getROProperty("alt")
msgbox b

QTP Script to capture tooltip of link.
SystemUtil.Run"iexplore","http://www.yahoomail.com/"
c=Browser("name:=Yahoo.*").Page("title:=Yahoo.*").Link("name:=Yahoo!").object.title
msgbox c

Datatable Methods
Script for Connecting to Database
Movie Songs Lyrics
SQL Queries Collection

Descriptive Programming For Playing a Video in Youtube

QTP Script to play a video(qtp scripts)in Youtube.

Systemutil.Run"iexplore","www.youtube.com"
Browser("name:=YouTube - Broadcast Yourself.").Page("title:=YouTube.*").WebEdit("name:=search_query","html id:=masthead-search-term").Set "qtp scripts"
Browser("name:=YouTube - Broadcast Yourself.").Page("title:=YouTube.*").WebButton("name:=Search","html id:=search-button").Click
Browser("name:=YouTube - Broadcast Yourself.").Page("title:=YouTube.*").Link("name:=QTP Scripts","html id:=video-long-title-qI_5rL7iGkI").Click

.

QTP Script For Composing Mail in Yahoo

SystemUtil.Run "iexplore","www.yahoo.com"
Set b= Browser("Name:=yahoo.*").Page("Title:=Yahoo.*")
b.Link("Name:=Sign In").Click
b.WebEdit("Name:=Login").set "abc"
b.WebEdit("Name:=Passwd").set "def"
b.WebButton("Name:=Sign In").Click
b.Link("Name:=Mail").Click
b.Webtable("Name:=Check Mail").WebButton("Name:=Compose").Click
b.WebEdit("Name:=To").Click
b.WebEdit("Name:=To").Set"xyz@gmail.com"
b.WebEdit("Name:=Subj").Set"helloooo"
b.WebEdit("Name:=BODY").Set"See This Message"

Descriptive Programming(QTP Script) for Yahoo Login Page

QTP Script to get DataTable ColumnCount,Adding new Column to Datatable,Getting Datatable Column Names

QTP Script to get datatable column count

Syntax:-DataTable.GetSheet("sheet name"/sheetid).getparametercount

Example:-DataTable.GetSheet(1).getparametercount

Script to add new parameter(column)to datatable

Syntax:-DataTable.GetSheet("sheet name"/sheetid).AddParameter "columnname","value"

Example:-DataTable.GetSheet(1).AddParameter "AAA","4"

QTP Script to get all parameter(column)names

For i=1 to a
b=DataTable.GetSheet(1).GetParameter(i).Name
msgbox (b)
Next

where a=parametercount(columncount)

To get rowcount of datatable

DataTable.GetRowCount

Datatable Methods

QTP Script to create Excel Sheet and to enter data into it

QTP Script to create Excel Sheet and to enter data into it(using script).

To create Excel Sheet

Set Excel=createObject("Excel.Application")
Set ExcelSheet=createObject("Excel.sheet")
ExcelSheet.Application.visible=true

'script to enter data(into excel sheet)
Excel.ActiveSheet.cells(1,1).value=1111
Excel.ActiveSheet.cells(1,2).value=2222
Excel.ActiveSheet.cells(2,1).value=3333
Excel.ActiveSheet.cells(2,2).value=4444

ExcelSheet.SaveAs "C:/XYZ.xls"

Excel Sheet is saved in C folder with following data.

A B
1111 2222 
3333 4444

Data Driven Testing using Excel Sheet

Datatable Methods(2)

For first part of this post check Datatable Methods(1)

SetCurrentRow:-This method is used for setting the focus of QTP to a specified row.

Syntax:-DataTable.SetCurrentRow(RowNumber)

Example:-DataTable.SetCurrentRow(1)


SetPrevRow:-This method is used for setting the focus of QTP to theprevious row of the currently focused row.

Syntax:-DataTable.SetPrevRow

Example:-DataTable.SetPrevRow


SetNextRow:-This method is used for setting the focus of QTP to the next row of the currently focused row.

Syntax:-DataTable.SetNextRow

Example:-DataTable.SetNextRow


GetSheet:-This method is used for making QTP to focus on a specified sheet.

Syntax:-DataTable.GetSheet(SheetId)

Example:-DataTable.GetSheet(1)


GetSheetCount:-This method is used to get count(number) of sheets in datatable.

Syntax:-DataTable.GetSheetCount

Example:-DataTable.GetSheetCount


GetRowCount:-This method is used for getting row count of a sheet.

Syntax:-DataTable.GetRowCount

Example:-DataTable.GetRowCount

By default it will get row count of Globalsheet.

If u want to get row count of a specified sheet then we have to use GetSheet method.

Syntax:-DataTable.GetSheet(SheetId).GetRowCount

Example:-DataTable.GetSheet(2).GetRowCount


Value:-It is used for capturing a value from a specified column,specified sheet and currently focused row.

Syntax:-DataTable.Value("column name"/column id,SheetId)

Example:-DataTable.Value("A",2) or DataTable.Value(1,2)

DataTable Methods(1)

1.AddSheet:-It is used for adding a new sheet to runtime datatable.

Syntax:-DataTable.AddSheet("Sheetname")

Example:-DataTable.AddSheet("laxmi")

2.DeleteSheet:-It is used for deleting a specified sheet from runtime datatable.

Syntax:-DataTable.DeleteSheet("Sheetname")

Example:-DataTable.DeleteSheet("laxmi")

3.Import:-It is used for importing all the sheets from Excel sheet to runtime datatable.

Syntax:-DataTable.Import"Path of Excel sheet"

Example:-DataTable.Import"d:\excel1.xls"

4.ImportSheet:-It is used for importing a specified sheet from Excel sheet to runtime datatable.

Syntax:-DataTable.ImportSheet"Path of Excel Sheet",Source sheetid,Destination sheetid

Example:-DataTable.ImportSheet"d:\excel2.xls",1,1

Here Source and Destination Sheet ids are 1(in this example)

1 is for Global Sheet.

5.Export:-It is used for exporting a complete runtime datatable(all sheets) to a specified location(path).

Syntax:-DataTable.Export"Path where excel sheet has to be created"

Example:-DataTable.Export"d:\excel3.xls"

6.ExportSheet:-It is used for exporting a specified sheet from a runtime datatable to specified location.

Syntax:-DataTable.ExportSheet"Path of Excel Sheet",Sheet id which has to be exported.

Example:-DataTable.ExportSheet"d:\excel4.xls",1

Datatable Methods(2)

QTP Script for RadioButton

QTP Script for RadioButton Control



Radiobutton allows us to select only one option

Syntax for Set method is ObjectHierarchy.Set

Window("Flight Reservation").WinRadioButton("Economy").Set



To Set other option

Window("Flight Reservation").WinRadioButton("First").Set



QTP Script for Combo box and other Controls

QTP Script for Button

QTP Script for Button control



Script to Click on OK Button

Dialog("Login").WinButton("OK").Click



Qtp Script to check whether Button is enabled or not(Disabled).

a=Dialog("Login").WinButton("OK").GetROProperty("enabled")
If a=true then
msgbox"ok button is enabled"
else
msgbox"ok button is not enabled"
End if

QTP Script for Combo box and other Controls

QTP Script for Checkbox

QTP Script for checkbox

Syntax for checking or unchecking checkbox is

ObjectHierarchy.Set"on/off"
 One

 Two

 Three

 Four


Script for above scenario will be

Window().Checkbox("one").Set "on"
Window().Checkbox("three").Set"on"

if u want to uncheck 3rd checkbox then 

Window().Checkbox("three").Set"off"
 One

 Two

 Three

 Four

QTP Script for Textbox

Script to enter value into Textbox

Dialog("Login").WinEdit("Agent Name:").Set "laxmiii"



Script to check whether particular textbox exist or not

If Dialog("Login").WinEdit("Agent Name:").Exist Then 
Msgbox "The Textbox exists"
Else 
Msgbox "The Textbox does not exist"
End if

If Textbox is present we get message as Textbox exist else we get message as Textbox doesnt exist.

QTP Script for Combo box and other Controls

QTP Script for Combo Box Control

QTP Script for Combobox



In combo box items index start from 0

1.Script for selecting an item from combobox using its value

Window("Flight Reservation").WinComboBox("Fly From:").Select “Frankfurt”



2. Script for selecting an item from combobox using its index

Window("Flight Reservation").WinComboBox("Fly From:").Select 1

1 indicates second item in combobox(0 for 1st item).

3.Script to get total items count of combobox

count = Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount

output will be 10.

4.Script to get value of selected item

selecteditem=Window("Flight Reservation").WinComboBox("Fly From:").GetSelection
Msgbox selecteditem

Output will be Frankfurt

5.Script to get all items(one by one) from combobox 

count = Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount
msgbox count
For i = 0 to count-1 
item =Window("Flight Reservation").WinComboBox("Fly From:").GetItem(i) 
Msgbox item
Next

Output -- single item will be displayed in messagebox
Total 10 message boxes will be displayed one after other.

6.Script to get names(content) of all items(at a time) from combobox

Content = Window("Flight Reservation").WinComboBox("Fly From:").GetContent 
Msgbox Content

Output will be
Denver
Frankfurt
London
Los Angeles
Paris
Portland
San Francisco
Seattle
Sydney
Zurich

All items in one messagebox.


Descriptive Programming to check all checkboxes in a webpage

script to check all checkboxes in a webpage.

Set a=Description.Create
a("html tag").value="input"
a("type").value="checkbox"

Set b=Browser("name:=").Page("title:=").childobjects(a)
c=b.count
msgbox c

For i=0 to c-1
b(i).set "on"
Next

QTP Faqs 
Manual Faqs 
Testing Faqs Resources 
Descriptive Programming for Yahoo Login Page 
D.P for getting checkbox checked status

D.P for checkbox

Script to check whether yahoologinpage(www.yahoomail.com) checkbox is checked or not

Set g=Browser("name:=Yahoo.*").Page("title:=Yahoo.*")
Set obj=Description.Create
obj("htmltag").value="Input"
obj("ClassName").value="webcheckbox"
obj("type").value="checkbox"
Set a=g.childobjects(obj)
c=a(0).getroproperty("checked")
msgbox(c)
If c=0 Then
msgbox "checkbox is not checked"
else
msgbox"checkbox is checked"
End If


Descriptive Programming for Gmail Login Page

Browser("name:=Gmail.*").Page("title:=Gmail.*").WebEdit("name:=Email").Set "abc"
Browser("name:=Gmail.*").Page("title:=Gmail.*").WebEdit("name:=Passwd").SetSecure "xyz"
Browser("name:=Gmail.*").Page("title:=Gmail.*").WebButton("name:=Sign in").Click
Browser("name:=Gmail.*").Page("title:=Gmail.*").Frame("name:=v1")
.Link("name:=Sign out").Click



No comments:

Post a Comment