Saturday, 27 April 2013

Script to check how many link are present of any Webpage

'//Script to check How many link are present on any Webpage


'Step1:- Create Description object for link

Set olink=Description.Create
olink("micclass").value="Link"
'olink("html tag").value="A"


'2. Collect all links in variable
Dim alllink

set alllink=browser("title:=.*").Page("title:=.*").ChildObjects(olink)

'3. Count how many links are present


msgbox alllink.count

'4.Get name of all the links

For i=0 to alllink.count-1

    msgbox alllink(i).getroproperty("innertext")


Next

Sunday, 21 April 2013

Change the Sequence of Action Execution in QTP

Suppose, We have Test called  "Sample.mts" and it contains 3 Actions.
Say Action 1 , Action 2 and Action 3.

When we run this test, actions are executed in Sequence:

Action 1
Action 2
Action 3

This is Default Setting

What if we want to change the Execution Order? Means, Say Action 3 will get executed first then Action 2
and Action 1 at last.

This can be easily achieved by making change in "Script" file present on ACTION 0 folder.

Open this file with notepad and make below changes shown in Notepad image.
Restart QTP and Execute same test again.

Now,  Execution of Actions will be in order
Action3
Action 2
Action 1

QTP -Simple Program to navigate all the pages using Pagination link

Suppose there is thread in a forum and we wish to navigate all pages of the forum.
Below code can be useful

Thread url:
http://www.indianguitartabs.com/f6/hindi-chords-tabs-submitted-amit82cse-10624.html

This thread has 25 pages right now.


There is pagination panel provided at top part and near bottom part of this thread for navigation purpose.

To navigate to next page, user just need to click on next arrow, i.e symbol.
First of all, we need to this object in OR.

Below code clicks icon till it is visible. As user reaches 25th Page, this symbol disappears and control comes out of the loop.

While (browser("Hindi Chords & Tabs Submitted").Page("Hindi Chords & Tabs Submitted").Image("Next").Exist(5) )

           browser("Hindi Chords & Tabs Submitted").Page("Hindi Chords & Tabs Submitted").Image("Next").Click

    Wend



Saturday, 20 April 2013

Storing Webtable data into Excel file using QTP

Below Code demonstrates how to  save scorecard data in MS Excel file

Webtable that I am using can be seen here  http://imsports.rediff.com/score/in_asa_101463.html
Our objective here is to store  Batting Scorecard of Kolkata Knight riders into Excel File.




'//Creating object for getting  webtable objects

 Set odesc=description.Create
 odesc("html tag").value="table"

'// Get total webtable count present on webpage

set otablecollector =browser("title:=.*").Page("title:=.*").ChildObjects(odesc)

print otablecollector.count

'//Returns 6 indicating there are 6 webtales on given page

//We are interested in  webtable with index 4. As it stores Batting score Card for Kolkata

'//Lets get how many rows are present there in this table

rc=otablecollector(4).rowcount
print rc '//Gives o/p as 12 (11 players + header row )

'//Lets get how many columns are present in row 1 for same table table
cc=otablecollector(4).columncount(1)
print cc

'//Gives o/p as 8. Hence 8 columns are there in row 1
'// Table has 12 rows and 8 columns


''Lets print  all data of row 1
'''It gives header column information
'
'For c=1 to cc
'
'print otablecollector(4).getcelldata(1,c)
'
'Next
'
''o/p
'
''Kolkata
''119-9 (20)
''Runs
''Balls
''4s
''6s
''SR

'For dc=1 to cc
'
'dcolumn= otablecollector(4).getcelldata(1,dc)
'
'var1=datatable.GetSheet(1).AddParameter(dcolumn, "")
'
'Next



Set oexl=createobject("excel.application")
oexl.Visible=true

set oworkbook=oexl.Workbooks.Add()

set oworksheet=oworkbook.worksheets.add()


'
For r=1 to rc

For c=1  to cc
dcolumn= otablecollector(4).getcelldata(r,c)
oworksheet.cells(r,c)=dcolumn
Next

Next



oworkbook.saveas "c:\ss.xls"
Set oexl=nothing


Output file snap:






































Friday, 19 April 2013

Finding how many times a character appears in a string

Below code can be used to find how many times a character appears in a string

Lets say, we want to find how many times "t" appears in string "sumit raut". This can be achieved by following steps written below:

1.Count Length of Original String
dim teststring
teststring="sumit raut"

lengthofstring=len(teststring)


2. Replace "t" with blank in original string
din newstring

newstring=replace(teststring,"t","")

3.Take difference of length of two string

actualcount=lengthofstring - newstring

print actualcount

o/p=2

Function to Log onto Gmail using QTP

//Action for Logging in:----

If  browser("Gmail: Email from Google").Page("Gmail: Email from Google").Link("Can't access your account?").Exist(5) then

Call glogin("yourusername","yourpassword")


Else

Msgbox "User is already logged on"

End If


// Function Definition
Function glogin(byval struname,byval strpwd)


 browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebEdit("Email").Set struname
 browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebEdit("Passwd").Set strpwd
 browser("Gmail: Email from Google").Page("Gmail: Email from Google").WebButton("Sign in").Click


End Function



















Finding Total number of Unread emails present in Gmail Inbox

1. Finding Total number of Unread emails present in Gmail Inbox


Our objective is to find how many unread emails are there in our Gmail Inbox.
This can be done if we can get number out of Inbox (xxx) string.

Steps to follow:
1.Log onto Gmail
2.From Inbox Page, get text Inbox (xxx) in a variable
3.Capture xxx in a variable. It would give exact number of unread emails.

// If we add Inbox(569) link in OR then we get below hierarchy.

'browser("Inbox (569) - sumitraut01@gmai").Page("Inbox (569) - sumitraut01@gmai").Link("Inbox (569)")

//The problem here is our code would not work if number of unread mail is not 569.This problem can be solved easily if Descriptive Programming is used

//Code given below makes our script generic:
//I have used Regular expression and Descriptive Programming here. 

//^Inbo= any  string sarting with Inbo.
//.* anything

dim Inboxstring
Inboxstring=browser("title:=^Inbo.*").Page("title:=^Inbo.*").Link("innertext:=^Inbo.*").GetROProperty("innertext")
// Inbox string has value Inbox (xxx)

totalstring=split(Inboxstring)
countm=totalstring(1)

//Countm now  has value (xxx)

newcountm=replace(countm,"(","")
//newcountm has value  xxx)

newcountm2=replace(newcountm,")","")
//newcountm2 has value  xxx
Print  newcountm2





Spring Boot : Exception Handler 14