General Important scripts 2

1) Launch QTP From a note pad.

After writing the following script In a note pad, save it with "*.vbs" extension
Dim qtApp

Set qtApp = CreateObject( "QuickTest.Application" )
If Not qtApp.Launched Then
qtApp.Launch
qtApp.Visible = True
End If
qtApp.Test.Settings.Run.DisableSmartIdentification = False
Set qtApp = Nothing

2) GetTOProperties Method:
set a=dialog("Login").WinEdit("Agent Name:").GetTOProperties()

a_cnt=a.count
msgbox a_cnt
For i=0 to a_cnt-1
propname=a(i).name
propvalue=a(i).value
msgbox propname&"="&propvalue
Next

General Important scripts1

1) Open a msgbox and close it in a specified time
set messagebox = createobject("wscript.shell")
messagebox.popup"message",time in sec,"Title of message box (Optional)"

2) Find all the child objects with a specified text
Set oDes=description.Create()
oDes("micclass").value="WebElement"
oDes("html tag").value="FONT"
set AAA = Browser("name:=xxx").page("title:=yyy").ChildObjects(oDes)
cnt=AAA.count
For i= 0 To cnt-1
BBB= AAA.item(i).getroproperty("outertext")
If instr(1,BBB,"$") Then 'Displays all texts starting with $
print "Text starting with $ is= "& BBB
End If
Next

3) If u find text field is not enable until you send some keystroke to web edit, in that case we have to send key stroke to webedit by creating it as:
Set ws = createobject("wscript.shell")
Browser(brw).page(pge).webedit(usname).click
ws.sendkeys"username"

4) Find the properties of a text (Eg: Link)
Set a = Browser(browser).page(page).link(link_prop)
Set b = a.object
Color = b.currentStyle.color
BackgrColor = b.currentStyle.backgroundColor
FontSize = b.currentStyle.fontSize
FontStyle = b.currentStyle.fontStyle
FontFamily = b.currentStyle.fontFamily
FontWeight = b.currentStyle.fontWeight

Msgbox Color & vbnewline & BackgrColor & vbnewline & FontSize & vbnewline &FontStyle & vbnewline& FontFamily &vbnewline & FontWeight

5) Find the number of unread messages in inbox of Gmail using simple regular expression
systemutil.Run "https://www.gmail.com"
Set a =browser("name:=Gmail: Email from Google").page("title:=Gmail: Email from Google")
browser("name:=Gmail: Email from Google").sync
a.webedit("name:=Email").Set "sunilkumar284"
a.webedit("name:=Passwd").SetSecure "password"
a.webcheckbox("name:=PersistentCookie").click
a.webbutton("name:=Sign in").click

mails = Browser("name:=Gmail - Inbox *.*").page("title:=Gmail - Inbox *.*").link("name:=Inbox*.*").GetROProperty("outertext")
msgbox (mails)

Working with Excel files

1) In this let us discuss about the Data Driven testing using Excel sheet. Highlight the invalid validation with Red and valid one with green color based on the presence of the error message displaying ...

Set myexcel = createobject("excel.application") 'create an excelsheet object
Set myfile = myexcel.workbooks.open("C:\test1.xls") 'create an excel sheet in a specified folder
myexcel.visible = true
Set mysheet = myfile.worksheets("sheet1") 'assign the sheet in excel
Row_cnt = mysheet.usedrange.rows.count
For i = 2 to Row_cnt

FirstName = mysheet.cells(i,"A")
LastName = mysheet.cells(i,"B")

Set FirName = description.Create
FirName("name").value = "firstName"

Set LasName = description.Create
LasName("name").value = "lastName"

set erfir = description.create
erfir("outertext").value= "Enter valid First Name"
erfir("class").value = "errmessage"
erfir("Class Name").value = "WebElement"

Browser(hmbro).page(hmpage).webedit( FirName).set FirstName
If Browser(hmbro).page(hmpage).WebElement(erfir).Exist Then
print "Invalid First name: " & FirstName
mysheet.cells(i,"A").font.color =vbred
else
mysheet.cells(i,"A").font.color = vbgreen
End If

Browser(hmbro).page(hmpage).webedit(LasName).Set LastName
set erlast = description.create()
erlast("outertext").value= "Enter valid Last Name"
erlast("class").value = "errmessage"
erlast("Class Name").value = "WebElement"
If Browser(hmbro).page(hmpage).WebElement(erlast).Exist Then
print "Invalid Last name: " & LastName
mysheet.cells(i,"B").font.color = vbred
else
mysheet.cells(i,"B").font.color =vbgreen

End If
Next
myexcel.quit
set myexcel = nothing

2) Compare two excel sheets:

Set a = createobject ("excel.application")
a.visible= True
Set b = a.workbooks.open ("D:\test1.xls")
Set c = a.workbooks.open ("D:\test2.xls")
Set d = b.worksheets (1)
Set e = c.worksheets (1)
For each cell in d.usedRange
If cell.value<>e.range (cell.address).value Then
cell.interior.colorindex=3
cell.font.color = vbgreen
else
cell.interior.colorindex=5
cell.font.color = vbred
End If
Next
a.quit ' If u want to close the excel then use this syntax
Set a=nothing

3) Create an Excel, Edit and provide a hyperlink to the edited text

Set excel = createobject("excel.application")
excel.visible = true
Set myfile = excel.workbooks.add
Set sheet = excel.worksheets(1)
sheet.cells(1,1) = "testing"
Set range = excel.range("A1")
Set hyperlink = sheet.hyperlinks.add(range,"http://www.ssofttesting.blogspot.com")

Working with Text files

1) Creating a Flat File:
set objfso = Createobject("Scripting.filesystemobject")
objfso.createtextfile("D:\testing.txt")


2) Check for existence of text file, if present delete it:
strdir = ("F:\Testing.txt")
set objfso = createobject("Scripting.filesystemobject")
if objfso.fileexists(strdir) = false Then
wscript.echo "Text file Not present" & Vbnewline & "Created now"
set txtfile = objfso.createtextfile("F:\Testing.txt")
else
objfso.deletefile("F:\Testing.txt")
wscript.echo "Present " &vbnewline &"But Text file deleted"
End if


3) Read content line by line in a text file:
Note: Initially create text file you want to read text from...
strdir = ("F:\test.txt")
set objfso = createobject("Scripting.filesystemobject")
set objfile = objfso.opentextfile(strdir)
Do Until objfile.AtEndOfStream
strtext = objfile.readline
wscript.echo (Strtext)
Loop


File system scripts

Script to create a file and write in it:
set a = createobject("scripting.filesystemobject")
set b = a.createtextfile("path of the file")
b.writeline("message here")

Script to delete a file :
Set a = createobject("scripting.filesystemobject")
a.deletefile("path of the file")

Checking weather the folder available or not, if not creating the folder and if present delete it:
strdir = ("D:\testing")
set objfso = createobject("scripting.filesystemobject")
if objfso.folderexists(strdir) = false then
objfso.createfolder("D:\testing")
wscript.echo "not present and created now"
else
objfso.deletefolder("D:\testing")
wscript.echo "present but deleted"
end if

Derive the names of drives in local machine:
set objfso = createobject("Scripting.filesystemobject")
set drivename = objfso.drives
For each drive in drivename
wscript.echo "Drive name: " & drive.driveletter
Next

Derive the Space available in local machine drives:
set objfso = createobject("Scripting.filesystemobject")
set drivespace = objfso.drives
For each drive in drivespace
wscript.echo "Available space in " & drive.driveletter & " Drive is: "& drive.availablespace
Next

Copy the folder from a drive and paste in other location
strdir = ("D:\testing")
set objfso = createobject("scripting.filesystemobject")
objfso.copyfolder(strdir),("E:\testing"),True