Wednesday, 16 September 2015

TOP 20 QTP REAL TIME SCRIPTS


1. HOW TO OPEN AN EXISTING WORD FILE          

 Steps:
Create object using word application
Make the object visible
Open a new word file with help of open  method

Ans.

set word = createobject("word.application")
word.visible=true
Word.Documents.Open("C:\Documents and Settings\sachin\My Documents\abc.docx")

2 - HOW TO LAUNCH A QTP APPLICATION, OPEN A NEW TEST AND  CLOSE QTP THROUGH A  SCRIPT

 Steps:

Declaring the application object variable

Creating the object for qtp application

Invoke the qtp by using launch method

Open new test with help of qtp application object

Make qtp application applicable

Relesing the qtp app object

3 - HOW TO GET DATA FROM EXCEL INTO QTP WITHOUT IMPORTING THE EXCEL   

 Steps:

Assign the path of excelfile to variable
Assign  the 1 to row,column variables
Declare the function for excel by passing the path,sheet,row,column
Create an object of excel
Open the path of excel file by using workbooks object
Assign the sheet data to variable
Closing the excel object
Relese the excel object
Close the function
Using msgbox  for viewing  the variables

Ans:
For this code make sure that you have excel file Book1.xls under c:\ and write some dummy values in its first few cells.

Write the whole above code in a new test in QTP and run it.

4 - HOW TO LAUNCH QTP, OPEN A TEST, RUN IT  AND CLOSE QTP.            

Steps:

Declaring the two variables
Create an object for using quicktest.application.
Invoke the qtp by using launch method
Open the desired test by using open method
Assign the qtptest to an variable
Execute the test by using run method
Close the test by using close method
Close the qtp by using quit method

Ans:
                        Dim qtapp
Dim qttest
Set qtapp= createobject (“quicktest.application”)
Qtapp.launch
Qtapp.visible= true
Qtapp.open”d:\aom1”, true
Set qttest=qtapp.test
Qttest.run
Qttest.close
Qtapp.quit

5.HOW TO  OPEN A NEW WORD FILE           

 Steps:

Create object using word application
Make the object visible
Open a new word file with help of add method

Ans:
set word = createobject("word.application")
word.visible=true
set doc = word.documents.add

6 - HOW TO IMPORT EXCEL INTO DATATABLE IN QTP.           


Steps:

Declare the application object variable
Create the application object
Launch the qtp
Open the test
Import the excel sheet data to first sheet of data table
Export the runtime datatable to excel sheet
Specify the qtp object visible
Relese the application object



7 - HOW TO RUN ALL TESTS IN A SPECIFIC FOLDER IN QUICKTEST PROFESSIONAL (QTP)?            

 Steps:

Declare the variables for qtpobj,fsoobj,dir,testcollection
Create an object using filesysytemobject
Use the getfolder to returs a folder
Assign the subfolders to testcollcetion variable
Create the object using reunresultsoptions
Setting the location for the run results
Execute the test. Instruct QuickTest Professional to wait for the test to finish executing
Close the qtp by  using close method
Relese the application object,runresults object

Ans:
Dim qtp_app
Dim file_sys
Dim Dir
Dim test_collection

Set qtp_app = CreateObject("QuickTest.Application")
qtp_app.Launch
qtp_app.Visible = True

' The file_systemObject object is used to access the file system on the server. This object can manipulate files, folders, and directory paths.
Set file_sys = CreateObject("Scripting.filesystemObject")

'GetFolder: Returns a Folder object for a specified path
Set Dir = file_sys.GetFolder( "C:\Program Files\HP\QuickTest Professional\tests1" )

Set test_collection = Dir.SubFolders

'RunResultsOptions:A collection of properties that indicate preferences for the run results.
Set qtResultsObj = CreateObject("QuickTest.RunResultsOptions")

For each t_folder in test_collection
qtp_app. Open t_folder.Path, True, False
'Setting the location for the run results
qtResultsObj.ResultsLocation = t_folder.Path & "\Res1" ' Set the results location
' Execute the test. Instruct QuickTest Professional to wait for the test to finish executing.
qtp_app.Test.Run qtResultsObj, True
qtp_app.Test.Close
Next
qtp_app.Quit
Set test_collection = Nothing
Set Dir = Nothing
Set file_sys = Nothing
Set qtp_app = Nothing

8 - How to count and get the path & name of all the object repositories associated with an action ?

Declare the application object variable
Create the application object
Launch the qtp
Open the test
Retrive the object repositaries of collection of action1
Count the no of object repositary files in the collection
Returns the path of object repossitary file
Extract the obkect repositary file name
Getting the whole path in variable
Returh the specified no of chars
Relese the repositorys object , application object, testobject.


9 - HOW TO COUNT TOTAL NUMBER OF LINKS ON A WEB PAGE?   

Steps:
Create object using description object
Assign link to object using value method
Retrive the collection of all objects & assign to variable
Using msgbox function dispay the data present in variable

Ans:
Code:
Set Des_Obj = Description.Create
Des_Obj("micclass").Value = "Link"
Set link_col = Browser("name:=Google").Page("title:=Google").ChildObjects(Des_Obj)
msgbox link_col.count
10 - HOW TO GET LINK NAMES AND URLS FOR ALL THE LINKS ON A WEB PAGE?       

Steps:
Create object using description object
Assign link to object using value method
Using child objects assign to variable
Assigning the data to variable by using child objects
Count the links by using the count function
Use for loop for repeting no of links
Use getroproperty  for capturing the name & url

Ans:
Set Des_Obj = Description.Create
Des_Obj("micclass").Value = "Link"
Set link_col = Browser("name:=Google").Page("title:=Google").ChildObjects(Des_Obj)
a= link_col.count

For i=0 to a-1
tag = link_col(i).GetROProperty("name")
href = link_col(i).GetROProperty("url")
msgbox tag &" " & href
Next
11 - HOW TO RUN MULTIPLE TESTS IN QUICKTEST PROFESSIONAL (QTP)?        

 Steps:

Declare the application object variable
Create the application object
Launch the qtp

Declare the array

Put the multiple tests in array
Create the results run object
Open the tests
Assign the tests to variables
Execute the test by using run method
Close the qtp using close method
Relese the application object, teset object

Ans:
Set App = CreateObject("QuickTest.Application")
App.Launch
App.Visible = True

Dim QTP_Tests(3)
QTP_Tests(1) = "C:\Program Files\HP\QuickTest Professional\Tests\tr1"
QTP_Tests(2) = "C:\Program Files\HP\QuickTest Professional\Tests\tr2"
QTP_Tests(3) = "C:\Program Files\HP\QuickTest Professional\Tests\tr3"

'Creating the RunResultsOptions object. It is a collection of properties that indicate preferences for the run results. E.g. ResultsLocation property as used below which tells the path in which the run results will be stored.
Set res_obj = CreateObject("QuickTest.RunResultsOptions")

For i = 1 to UBound (QTP_Tests)
App.Open QTP_Tests(i), True
Set QTP_Test = App.Test
res_obj.ResultsLocation = QTP_Tests(i) & "\Res1" ' Set the results location
'Using the Run method. Run Method runs the open test or business component and creates results in the specified file or Quality Center path. A Boolean value - true or false can be set. Indicates whether the Test.Run statement waits until the end of the run before performing the next statement in the automation script. Default=True.
QTP_Test.Run res_obj, True
QTP_Test.Close
Next
App.Quit
Set res_obj = nothing
Set QTP_Test = nothing
Set App = nothing

12   HOW TO COMPARE TWO XML FILES.

Steps:

Assign the xml file to variable
Load the first xml file
Assign the xml file to variable
Load the second xml file
Compare the xml files by using compare method and assigning result to variable
If the result variable is having value 1 then document same otherwise not match

Ans:
There is very small difference between these two XML files. Just try to understand how Compare method gives results. You can always change/modify the code according to your requirements.
Using different XML files:

Set doc=XMLUtil.CreateXML
doc.LoadFile "C:\ENV.xml"
Set docx=XMLUtil.CreateXML
docx.LoadFile "C:\ENV1.xml"

res = docx.Compare(doc,resultDoc,micXMLValues+micXMLCDataSections)
msgbox resultDoc
if res = 1 then
msgbox "Documents match :)"
else
msgbox "do not match "
end if

Results of running the above code:

 Using same XML files:

Set doc=XMLUtil.CreateXML
doc.LoadFile "C:\ENV.xml"
Set docx=XMLUtil.CreateXML
docx.LoadFile "C:\ENV.xml"
res = docx.Compare(doc,resultDoc,micXMLValues+micXMLCDataSections)
msgbox resultDoc
if res = 1 then
msgbox "Documents match :)"
else
msgbox "do not match "
end if

Results of running the above code:

13. HOW TO OPEN A NEW WORD DOCUMENT AND WRITING TEXT TO IT AND SAVING IT

Steps:

Create object using word application
Make the object visible
Open a new word file with help of add method
Assign the word selection to variable
Write the text by using
 
Ans:
set word = createobject("word.application")
word.visible=true
set doc = word.documents.add

Set objectSelection = Word.Selection
objectSelection.TypeText "This is sample text"
doc.saveas("c:\test.docx")


14.HOW TO PRINT A WORD FILE       

Steps:

Create object using word application
Open the desired document by using open method
Print the document by using printout method
Close the word document

  Ans:
Set word = CreateObject("Word.Application")
Set objectdocument = word.Documents.Open("c:\test.docx")
objectdocument.PrintOut()
word.Quit


15 - HOW CAN I ADD ADDITIONAL WORKSHEETS TO AN EXCEL WORKBOOK?

 Steps:

Create an object for excel application
Assign the visible property as true
Adding a sheet by using workbooks.add method & assigning to variable
Assigning sheet to variable
Adding to the excelsheet
Ans:
This script adds the nine new worksheets directly after worksheet 1 (thus pushing sheets 2 and 3 to the end of the collection):

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)
Set colSheets = objWorkbook.Sheets
colSheets.Add ,objWorksheet,9

16 - HOW CAN I INSERT A COLUMN INTO A SPREADSHEET?

Here's a script that writes a column header to columns A, B, and C, then inserts a new, blank column between columns B and C:

Steps:

Create an object for excel application
Assign the visible property as true
Assign the worksheet using workbooks object
Assign the data  to cells using work sheet  methods
Using insert method insert data  by specifying
Const xlShiftToRight = -4161

Ans:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)
objWorksheet.Cells(1,1) = "Dataset 1"
objWorksheet.Cells(1,2) = "Dataset 2"
objWorksheet.Cells(1,3) = "Dataset 4"
Set objRange = objExcel.Range("C1").EntireColumn
objRange.Insert(xlShiftToRight)

17 - HOW CAN I REPLACE TEXT IN AN EXCEL SPREADSHEET IN QTP?     


Steps:

Create an object for excel application
Assign the visible property as true
Assign the excel file to the variable by using workbooks & open function
Focus on particular sheet by using workbook object
With the help of replace method we can replace  the text

Ans:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\Test.xls")
Set objWorksheet = objWorkbook.Worksheets(1)
Set objRange = objWorksheet.UsedRange
objRange.Replace "C:\Test\Image.jpg", "C:\Backup\Image.jpg"

18 – HOW TO OPEN AN EXCEL SPREADSHEET     

Steps:

Declare the variables
Create the object using excel application
Assign the excel file to the variable by using workbooks & open function

Ans:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Scripts\New_users.xls")


No comments:

Post a Comment