Friday, June 6, 2008

Import and Export Methods

Here i am giving the script for Importing data from data table and exporting the data table into Excel File.

SystemUtil.Run "C:\ProgramFiles\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe"

Dialog("text:= Login").Activate
Dialog("text: = Login").WinEdit("Attached text:=Agent Name").set "rajaputra"
Dialog("text: = Login").WinEdit("Attached text:=Password").set "mercury"
Dialog("text:=Login").WinButton("text:=OK").Click
Window("text:=Flight Reservation").Activate
datatable.importSheet "C:\example.xls",1,1
rowcount=datatable.GetRowCount
For i=1 to rowcount
Window("text:=Flight Reservation").WinButton("window id:=5").Click
Dialog("text:=Open Order").Activate
Dialog("text:=Open Order").WinCheckBox("text:=&Order No.").set "ON"
datatable.SetCurrentRow(i)
n=datatable(1,1)
Dialog("text:=Open Order").WinEdit("window id:=1016").set n
Dialog("text:=Open Order").WinButton("text:=OK").Click
If(window("text:=Flight Reservation").Dialog("text:=Open Order").Dialog("text:=Flight Reservation").exist)then
window("text:=Flight Reservation").Dialog("text:=Open Order").WinButton("text:=Cancel").Click
datatable(2,1)="Not Exist"
Else
datatable(2,1)="Exist"
End If
Next
Datatable.Export ("C:\flights.xls")
window("text:=Flight Reservation").Close


Before running this script, in "example.xls" you have to put two columns.
one is OrderNo and one is Result.
In OrderNo you can enter any valid or invalid OrderNo's and Result you can leave as it is.
We will out put the data into Results.

What is Input Parameters and OutPut Parameters

Input parameter allows you to run a test using different set of data input values.
When you use a datatable parameter, you must instruct QuickTest on where the input data will come from.

An output parameter is a value which comes back from the application under test. When you run the test QuickTest retrieves the current value of the property and enters it in the run-time datatable as an output value.

You can subsequently use this output value as a input value in your test. We call this data correlation. This enables you to use data retrieved during other parts of test.

How to run the batch file through Script

I have taken 3 tests as a example for doing batch run. They are

1. Display -It will display a message in a word doc.
2. DisplayDate - It will display a date and time in a Notepad
3. Multiplication - It will display a mathematical table in Notepad

I converted them in respective funcitons and saved in notepad as a .vbs file.
(in C:\VBLibraryFiles\)
And i wrote a VBS Finction that is called above 3 functions and saved as a callAll.vbs
(in C:\VBLibraryFiles\)
Before running the test I loaded all files into resource tab

callAll() 'Here i am just calling callAll function which is defined in callAll.vbs file

**********************

Display.vbs -

public funciton display()

set WordObj = CreateObject("Word.Application")
WordObj.visible = true
Set wordDoc = WordObj.Documents.Add
WordDoc.Range.InsertBefore "Hi Good Morning"
WordDoc.SaveAs "C:\Greetings.doc"
WordDoc.quit
Set wordObj = Nothing

End Function

***********************

DisplayDate.Vbs -

Public Function displayDate()

Set fso = createObject("Scripting.filesystemObject")
Set fptr = fso.createTextfile("C:\Sample.txt")
a = FormatDateTime(Date,1)
fptr.writeLine a

End Function

*******************************

Multiplication.Vbs -

Public Function multiplication()

Set obj = createObject("Scripting.filesystemObject")
Set of = obj.createTextFile("C:\mat.txt")
For i=1 to 10
x=5*1
of.writeLine "5 * "&i&" = "&x
Next

End Function

*****************************

CallAll.Vbs -

Public Function callAll()

display()
msgbox "Test 1 is pass"
displayDate()
msgbox "Test 2 is pass"
multiplication()
msgbox "Test 3 is pass"

End Function

How to copy the file from one location to another location

dim filesys
Set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("C:\sourcefolder\anyfile.ext") then
filesys.CopyFile "C:\sourcefolder\anyfile.ext", C:\destinationfolder\"
End If

How to get the Count of each file in a particual folder

Set fso = CreateObject("Scripting.FileSystemObject")
Set fold = fso.getFolder("D:\testingfiles")
Set fc = fold.files

word=0
xls=0
txt=0

datatable.getSheet(1).addParameter "Word",""
datatable.getSheet(1).addParameter "Excel",""
datatable.getSheet(1).addParameter "text",""
datatable.getSheet(1).addParameter "Total Files",""

For each f in fold.files

if lcase(right(f.name,4)) = ".doc" then
word=word+1
end if

if lcase(right(f.name,4))= ".xls" then
xls=xls+1
end if

if lcase(right(f.name,4))= ".txt" then
txt=txt+1
end if
next

datatable(1,1)=word
datatable(2,1)=xls
datatable(3,1)=txt
datatable(4,1)=fc.count

'Run this script and see the runtime datatable

Select RadioButton through Datatable

Browser("micClass:=Browser").Page("micClass:=Page").WebRadioGroup("name:=servClass").Select
Datatable("FlightClass, dtGlobalSheet)

'Here FlightClass is (Coach,Business, First) in the datatable.

How to Run and Close the application during Run time

We can Invoke the application during run session using

SystemUtil.Run "NameOfFile"

Systemutil.Run Statement enables you to open or run any application from a given location.
Similarly, you can use SystemUtil to close the application like this:

SystemUtil.CloseProcessByName "explorer.exe"

Working With Excell

Here I am discussing
How to create a Excel Sheet
Storing the data into Excel Sheet
Reading the data from Excel Sheet

Creation of Excel Sheet -

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

Storing the data into Excel Sheet -

Excel.ActiveSheet.Cells(1,1).Value="UserName"
Excel.ActiveSheet.Cells(1,2).Value="Password"
Excel.ActiveSheet.Cells(2,1).Value="arunsingh"
Excel.ActiveSheet.Cells(2,2).Value="rajaputra"
ExcelSheet.SaveAs "C:\text.xls"
datatable.Import "C:\text.xls" 'Importing excel sheet into datatable

Reading the data from Excel Sheet -

msgbox "UserName is="+Excel.ActiveSheet.Cells(2,1)
msgbox "Password is="+Excel.AcitveSheet.Cells(2,2)

How to Capture the Screen Shot

By using the below script we can capture the screen shot at run time -

Browser("BrowserName").Page("PageName").captureBitmap "c:\test\ScreeShot1.bmp"

Below example will help you to get familiar with this-
'Prerequisite: Open the FlightReservation Login screen
'Then Run this Script

Set Dlg=Dialog("text:=Login")
set Wnd=Window("text:=Flight Reseravation")
Set oDlg=wnd.Dialog("text:=Open Order")

Dlg.WinEdit("attached text:=Agent Name").set "rajaputra"
Dlg.WinEdit("attached text:=Password").setSecure "mercury"
Dlg.WinButton("text:=OK").Click

wait (7)

wnd.Activate
wnd.WinButton("window id:=5").Click
oDlg.WinCheckBox("text:=&Order No.").Set "ON"
oDlg.WinEdit("window id:=1016").set "100"
oDlg.WinButton("text:=OK").Click

if(oDlg.Dialog("text:=Flight Reservations").Exist) then
oDlg.Dialog("text:=Flight Reservations").CaptureBitmap "C:\ScreenShot.bmp"
oDlg.Dialog("text:=Flight Reservations").WinButton("text:=OK").Click
oDlg.WinButton("text:=Cancel").Click
End If

wnd.Close

About Me

My photo
Hi all,I am Arun Singh Rajaputra. I am working as a Project Lead in a reputed organization in Hyderabad, AP, India. I have little bit knowledge on Quick Test Professional and like to share my knowledge to all through this blog.

Subscribe to get updates on this blog

Enter your email address:

NOTE

Some of the posts posted in this blog are collected and most of them have been prepared by me. If any one have objections regarding any copied posts. Please mail me so that I can take a necessary action on that post.