Monday, December 6, 2010

QTP-MS Word Scripts

1) create a word document and write some data

dim mw
set mw=CreateObject("Word.Application")
mw.Documents.Add
mw.selection.typetext "hello"
mw.ActiveDocument.SaveAs "e:\gcreddy.doc"
mw.quit
set mw=nothing


2) Create word, Create table and write all the services names

Set mw = CreateObject("Word.Application")
mw.Visible = True
Set dc = mw.Documents.Add()
Set objRange = dc.Range()
dc.Tables.Add
objRange,1,3
Set objTable = dc.Tables(1)
x=1
strComputer = "."
Set wms=GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = wms.ExecQuery("Select * from Win32_Service")
For Each s in colItems
If x > 1 Then
objTable.Rows.Add()
End If
objTable.Cell(x, 1).Range.Font.Bold = True
objTable.Cell(x, 1).Range.Text = s.Name
objTable.Cell(x, 2).Range.text = s.DisplayName
objTable.Cell(x, 3).Range.text = s.State
x = x + 1
Next


3) script to display all the doc files in all the drives in the system
Dim mw
Set mw=CreateObject("Word.Application")
Set fs=createobject("Scripting.FileSystemObject")
Set d=fs.Drives
mw.FileSearch.FileName="*.doc"
For each dr in d
msgbox dr
mw.FileSearch.LookIn=dr
mw.FileSearch.SearchSubFolders=True
mw.FileSearch.Execute
For each i in mw.FileSearch.FoundFiles
print i
Set f=fs.GetFile(i)
print f.Name&" "&f.Size&" "&f.DateCreated
print "-------------------------------------------------------------------"
Next
Next
mw.Quit 

4) Counting the number of times a word appears in a word document
Set objWord=CreateObject("Word.Application")
Set myfile=objWord.Documents.Open ("C:\Documents and Settings\gcr.GCRC-9A12FBD3D9\Desktop\xyz.doc")
strText="gcreddy"
strRead=myfile.Content
Set RegExp=new regexp
regexp.ignorecase=True
regexp.global=True
regexp.pattern=strText
Set matches=regexp.execute(strRead)
matchesFound=matches.count
msgbox matchesfound
myfile.close
set objFso=Nothing

QTP-System Administration Scripts

1) GettingLocalComputerInformation


Set
objComputer=CreateObject("Shell.LocalMachine")

Wscript.Echo
"Computername:"&objComputer.MachineName
Wscript.Echo
"Shutdownallowed:"&objComputer.IsShutdownAllowed
Wscript.Echo
"FriendlyUIenabled:"&objComputer.IsFriendlyUIEnabled
Wscript.Echo
"Guestaccessmode:"&objComputer.IsGuestAccessMode
Wscript.Echo
"Guestaccountenabled:"&_
    objComputer.IsGuestEnabled(0)
Wscript.Echo
"Multipleusersenabled:"&_
    objComputer.IsMultipleUsersEnabled
Wscript.Echo
"Offlinefilesenabled:"&_
    objComputer.IsOfflineFilesEnabled
Wscript.Echo
"Remoteconnectionsenabled:"&_
    objComputer.IsRemoteConnectionsEnabled
Wscript.Echo
"Undockenabled:"&objComputer.IsUndockEnabled


2) RestartaComputer

strComputer
="atl-dc-01"
Set
objWMIService=GetObject("winmgmts:"_
    &"{impersonationLevel=impersonate,(Shutdown)}!\\"&_
        strComputer&"\root\cimv2")

Set
colOperatingSystems=objWMIService.ExecQuery_
    ("Select*fromWin32_OperatingSystem")

For
EachobjOperatingSystemincolOperatingSystems
    objOperatingSystem.Reboot()
Next

3) ShutDownaComputer

strComputer
="."
Set
objWMIService=GetObject_
    ("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\"&_
        strComputer&"\root\cimv2")

Set
colOperatingSystems=objWMIService.ExecQuery_
    ("Select*fromWin32_OperatingSystem")

For
EachobjOperatingSystemincolOperatingSystems
    objOperatingSystem.Win32Shutdown(1)
Next
 
4) ModifySystemStartupDelay

strComputer
="."
Set
objWMIService=GetObject("winmgmts:"_
    &"{impersonationLevel=impersonate}!\\"&strComputer&"\root\cimv2")

Set
colStartupCommands=objWMIService.ExecQuery_
    ("Select*fromWin32_ComputerSystem")

For
EachobjStartupCommandincolStartupCommands
    objStartupCommand.SystemStartupDelay=10
    objStartupCommand.Put_
Next

QTP-Web Scripts-II


1) Script to get the list of links in Google and do spell check
 
Dim d
set mw=CreateObject("Word.Application")
set d=Description.Create
d("micclass").value="Link"
set a=Browser("Google").page("Google").childobjects(d)
for i=0 to a.count-1 
mw.WordBasic.filenew 
s=a(i).getROProperty("innertext")
mw.WordBasic.insert s 
if mw.ActiveDocument.Spellingerrors.count>0 then 
Reporter.ReportEvent 1,"Spelling","spelling error :"&s 
end if
mw.ActiveDocument.Close(False)
next
mw.quit
set mw=nothing

2) Script to check ON the checkboxes in yahoo mail inbox


Dim d
Set d=Description.Create
d("micclass").value="WebCheckBox"
Set c=Browser("Inbox (17) - Yahoo! Mail").Page("Inbox (17) - Yahoo! Mail").ChildObjects(d)
For i=1 to 10
c(i).set "ON"
Next



3) script to select a mail having subject 'hi' or 'HI'
 

n=Browser("yahoo").Page("yahoo").WebTable("Inbox").RowCount
For i=2 to n
s=Browser("yahoo").Page("yahoo").WebTable("Inbox").GetCellData(i,7)
If lcase(trim(s))="hi" Then
Browser("yahoo").Page("yahoo").WebCheckBox("index:="&i-1).set "ON"
End If
Next
 


4) Function to send a mail
 

Function SendMail(SendTo, Subject, Body, Attachment) 
Set otl=CreateObject("Outlook.Application")
Set m=otl.CreateItem(0) 
m.to=SendTo 
m.Subject=Subject 
m.Body=Body 
If (Attachment <> "") Then 
Mail.Attachments.Add(Attachment)
End If
m.Send 
otl.Quit 
Set m = Nothing 
Set otl = Nothing
End Function
Call SendMail("nagesh.rao46@gmail.com","hi","This is test mail for tsting","")
Adv VBScripts



5) Open Internet Explorer and navigate to yahoo mail
 

Dim ie
Set ie=CreateObject("InternetExplorer.Application")
ie.Visible=True
ie.Navigate "www.yahoomail.com"
x=Browser("CreationTime:=0").GetROProperty("title")
msgbox x

QTP-Web Scripts I


1) Count all opened Browsers on desktop and close them all?


Set oDesc = Description.Create()
oDesc("micclass").Value = "Browser"
Set Browsers =Desktop.ChildObjects (oDesc)
NumberofBrowsers = Browsers.Count()
Reporter.ReportEvent 2,"Res","Number of Browsers are: "&NumberOfBrowsers
For Counter=0 to NumberofBrowsers-1
Browsers(Counter).Close
Next



2) Count, how many links available in Mercury Tours Home Page.


Set oDesc = Description.Create()
oDesc("micclass").Value = "Link"
Set Lists = Browser("Welcome: Mercury").Page("Welcome: Mercury").ChildObjects (oDesc)
NumberOfLinks = Lists.Count()
Reporter.ReportEvent 2,"Res","Number of Links are: "&NumberOfLinks



3) Verify  whether the 'Gmail'  link available or not on Google Homepage 


Option explicit
Dim oLink, Links, TotLinks, i, myLink
Set oLink = description.Create
oLink("micclass").value = "Link"
SystemUtil.Run "D:\Program Files\Internet Explorer\IEXPLORE.EXE"
set Links = Browser("name:=Google").page("title:=Google").ChildObjects(oLink)
TotLinks = Links.count
For i =0 to TotLinks-1
myLink = Links(i).getroproperty("innertext")
If mylink = "Gmail" Then
     reporter.ReportEvent 0,"res","Link Gmail available"
End If
Next



4) Count number of Links, Edit Boxes available on Google Homepage?


Dim oLink
Set oLink=description.Create
Set oEdit=description.Create
oLink("micclass").value="Link"
oEdit("micclass").value="WebEdit"
Set Links=browser("name:=Google").page("title:=Google").ChildObjects(oLink)
Set EditBoxes=browser("name:=Google").page("title:=Google").ChildObjects(oEdit)
TotLinks= Links.count
TotEditBoxes=EditBoxes.count
msgbox TotLinks
msgbox TotEditBoxes
Reporter.ReportEvent 0,"Result","Total Links are: "&TotLinks&"Total edit Boxes are: "&TotEditBoxes


5) Count how many links available in a Web Page(any web page, using Regular Expressions)


Dim oLink,Links, TotLinks
Set oLink=Description.Create
oLink("micclass").value="Link"
Set Links=Browser("title:=.*").page("title:=.*").ChildObjects(oLink)
TotLinks=Links.count
msgbox TotLinks
Reporter.ReportEvent 2,"Res","Total Links are: "&TotLinks



Function To Create HTML Report


Dim StartTime,stTime, enTime
Dim fso, ts,intCnt, intPass,intFail
intPass=0
intFail=0
Const ForWriting = 2
Function OpenFile (strFileName)
StartTime = Timer
stTime = Time
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(strFileName,2)
'OpenFile = strFileURL
CreateHeader
End Function



Function To Close File

Dim objIE,strFileURL
Function CloseFile ( strFileURL,strEnv )
Footer(strEnv)
ts.close()
Set objIE = CreateObject("InternetExplorer.Application")
objIE.visible = True
objIE.Navigate strFileURL
wait(5)

Set objIE=nothing
End Function



Function to Clear Cookies()

Function ClearCookies()
SystemUtil.Run "iexplore.exe"
WebUtil.DeleteCookies
Set objBrowser=Description.Create
objBrowser("micclass").value="Browser"
Set objBCount = Desktop.ChildObjects(objBrowser)
intBrowserCount = objBCount.Count
strHwnd=Browser("creationtime:=" & intBrowserCount).GetROProperty("hwnd")
SystemUtil.CloseProcessByHwnd(strHwnd)
End Function





Function to send Email



Function SendMail()

If k=0 Then
'Nothing
Else
msgsub = " Geo_PostalCode pattern searchTest Results:" &vbcrLf &"One or more of the Test Cases Failed." &vbcrLf &"See attachment for details."
End If
Set objConf = CreateObject("CDO.Configuration")
cdoSendUsingPort=2
sMailServerName="smtp.phx.move.com"
cdoAnonymous=cdoNONE
objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sMailServerName
objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous 'cdoBasic
objConf.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
objConf.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "corp\ NRaoJ "
'objConf.fields.item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "123K!r45"
objConf.fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objConf.fields.Update
Set objMsg = CreateObject("CDO.Message")
objMsg.Configuration = objConf

objMsg.to = "gcreddy@gmail.com"
objMsg.From = "gcreddy@gmail.com"
objMsg.Subject = "~Geo_PostalCode pattern search"
objMsg.TextBody =msgsub
doc=strFilepath
objMsg.AddAttachment(doc)
objMsg.Send
Set objMsg = Nothing
NewMail = True
End Function



Function to create append excel

Function LogFile

Set objExcel=CreateObject("Excel.Application")
objExcel.visible=False
objExcel.workbooks.open(strFilePath)
r=1
Do Until len(objExcel.cells(r,1))=0
r=r+1
Loop

objExcel.cells(r,1).value="Step Name"
objExcel.cells(r,2).value="Step Description"
objExcel.cells(r,3).value="Test Results"
objExcel.DisplayAlerts = False
objExcel.Save
objExcel.Quit
Set objExcel = Nothing
End Function




Function to get the Application Environment on which the script is running

Function GetEnv()

Set fso=Createobject("Scripting.FileSystemObject")
sFile="C:\WINDOWS\system32\drivers\etc\hosts"
Set MyFile=fso.OpenTextFile(sFile,1, True)
Do
sEnv=MyFile.ReadLine
If instr(sEnv,"PRODUCTION")>0 Then
GetEnv="Production"
Exit do
Elseif instr(sEnv,"Staging")>0 Then
GetEnv="Staging"
Exit do
Elseif instr(sEnv,"QA MAINTENANCE")>0 Then
GetEnv="QAM"
Exit do
Elseif instr(sEnv,"qap.")>0 Then
GetEnv="qap"
Exit do
End If
Loop While MyFile.AtEndOfStream=False
End Function


Function To Create HTML Report

Dim StartTime,stTime, enTime

Dim fso, ts,intCnt, intPass,intFail
intPass=0
intFail=0
Const ForWriting = 2
Function OpenFile (strFileName)
StartTime = Timer
stTime = Time
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(strFileName,2)
'OpenFile = strFileURL
CreateHeader
End Function