Thursday, 16 July 2015

QTP REAL TIME SCRIPTS AND STEPS


How to convert an Excel file to CSV file?



How to convert an Excel file to CSV file?

Write the below code in a notepad and save it as a .VBS file. now drag and drop your excel file onto this .vbs file.

Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1

FullName = objArgs(I)
FileName = Left(objArgs(I), InstrRev(objArgs(I), ".") )

Set objExcel = CreateObject("Excel.application")
set objExcelBook = objExcel.Workbooks.Open(FullName)

objExcel.application.visible=false
objExcel.application.displayalerts=false

objExcelBook.SaveAs FileName & "csv", 23

objExcel.Application.Quit
objExcel.Quit
 

Set objExcel = Nothing
set objExcelBook = Nothing

Next

steps:

1.     Create a new object for Windows Script.
2.     fgdg
3.      



Creating hyperlinks in Excel file with VBScript

Below code opens a file called 1.xlsx (which is on the C:\ drive) and makes the text written in cell A1 as hyperlink. When you click on that hyperlink it goes to Google.com

Set O_EXCEL = WScript.CreateObject("Excel.Application")
O_EXCEL.Visible = TRUE
O_EXCEL.Workbooks.Open "C:\1.xlsx"
O_EXCEL.Workbooks(1).Activate
O_EXCEL.Workbooks(1).Worksheets(1).Range("A1").Select
LINK_TO = "http://www.google.com"
O_EXCEL.Workbooks(1).Worksheets(1).Hyperlinks.Add O_EXCEL.Selection, LINK_TO
O_EXCEL.Workbooks(1).Save
O_EXCEL.Quit
set O_EXCEL = nothing

Steps:
1.   Create a new object for Excel application.
2.   check visibility of object
3.   open workbook
4.   Activate workbook
5.   select the range
6.   assign the value into LINK_TO
7.   set hyperlink in excel
8.   save the workbook
9.   Close the excel file.
How to copy one excel sheet to another

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set Excel1= objExcel.Workbooks.Open("C:\1.xlsx")
Set Excel2= objExcel.Workbooks.Open("C:\2.xlsx")
Excel1.Worksheets("Sheet1").UsedRange.Copy
Excel2.Worksheets("Sheet1").Range("A1").PasteSpecial -4163
Excel1.save
Excel2.save
Excel1.close
Excel2.close
objExcel.quit
set objExcel=nothing


Steps:
1.      Create a new object for Excel application.
2.      check visibility of object
3.      Open workbook and set in the excel1 object.
4.      Open workbook and set in the excel2 object.
5.      Copy the sheet1 information.
6.      Paste information into excel2 sheet1.
7.      Save excel1.
8.      Save excel2.
9.      Close the excel1.
10.  Close the excel2.
11.  Exit excel object.

Select case example

Dim x,y, Operation, Result
x= Inputbox (" Enter x value")
y= Inputbox ("Enter y value")
Operation= Inputbox ("Enter an Operation")

Select Case Operation

       Case "add"
                     Result= cdbl (x)+cdbl (y)
                     Msgbox "Hello SRSS"
                     Msgbox "Addition of x,y values is "&Result

       Case "sub"
                     Result= x-y
                     Msgbox "Hello SRSS"
                     Msgbox "Substraction of x,y values is "&Result

       Case "mul"
                           Result= x*y
                     Msgbox "Hello SRSS"
                     Msgbox "Multiplication of x,y values is "&Result

       Case "div"
                           Result= x/y
                     Msgbox "Hello SRSS"
                     Msgbox "Division of x,y values is "&Result

       Case "mod"
                     Result= x mod y
                     Msgbox "Hello SRSS"
                     Msgbox "Mod of x,y values is "&Result

       Case "expo"
                           Result= x^y
                     Msgbox "Hello SRSS"
                     Msgbox"Exponentation of x,y values is "&Result

       Case Else
                     Msgbox "Hello SRSS"
                     msgbox "Wrong Operation"

End Select

Step:

  1. Declare new variable x, y, Operation, Result.
  2.  Gather the input values  from x,y
  3. Gather the any operator (add, sub, mul….)
  4. Create select statement.
  5. Create cases for Add.
  6. Do the add operation and store into ‘result’ variable.
  7. Display message box
  8. Create cases for Sub.
  9. Do the add operation and store into ‘result’ variable.
  10. Display the message box
  11. Create cases for Sub.
  12. Do the add operation and store into ‘result’ variable.
  13. Display the message box
  14. Create cases for Mul.
  15. Do the add operation and store into ‘result’ variable.
  16. Display the message box
  17. Create cases for Div.
  18. Do the add operation and store into ‘result’ variable.
  19. Display the message box
  20. Create cases for Mod.
  21. Do the add operation and store into ‘result’ variable.
  22. Display the message box
  23. Create cases for else.
  24. Display the message.
  25. Display the message box
  26. End select statement.


Read two numbers and display the sum?
 
Dim num1, num2, and sum
num1=inputbox ("Enter num1")
num2=inputbox ("Enter num2")
 
         Sum= Cdbl (num1) + Cdbl (num2) 
              msgbox ("Sum is " &sum)
 
Steps:
 
1.   Declare a variable for number1, number2, and sum.
2.   Gather the value number1
3.   Gather the value number2
4.   Add the two numbers.
5.   Display the message.
 
Read P, T, R values and Calculate the Simple Interest?

Dim p,t, r, si
p=inputbox ("Enter Principle Amount")
t=inputbox ("Enter Time")
r=inputbox ("Enter Rate of Interest")
       si= (p*t*r)/100 ' p= principle amount, t=time in years, r= rate of interest
              Msgbox ("Simple Interest is " &si)

Steps:

1.   Declare a variable p, t, r, si
2.   Gather input value for amount.
3.   Gather input value for time in years.
4.   Gather input value for interest rate
5.   Calculate the total interest.
6.   Display the message.

Read any four digit number and display the number in reverse order?

Dim num, rev
Num= inputbox ("Enter a number")
If len(num)=4 Then

Rev=rev*10 + num mod 10
num=num/10
num= left(num,3)

rev=rev*10 + num mod 10
num=num/10
num= left(num,2)

rev=rev*10 + num mod 10
num=num/10
num= left(num,1)

Rev=rev*10 + num mod 10

       Msgbox "Reverse Order of the number is "&rev
Else
       Msgbox "Number, you entered is not a 4 digit number"
End If


Steps:

1.   Declare a new variable for num,rev
2.   Gather the input value and store into num variable.
3.   Create the if condition.
4.    

Minimum marks 35 for any subject, otherwise 'no grade fail'

Dim e,m,p,c, tot
e=inputbox ("Enter english Marks")
m=inputbox ("Enter maths Marks")
p=inputbox ("Enter physics Marks")
c=inputbox ("Enter chemistry Marks")

tot= cdbl(e) + cdbl(m) + cdbl(p) + cdbl(c)
msgbox tot


If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=300 Then
       msgbox "Grade is Distinction"

else If  cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=240 and tot<300 Then
       msgbox "Grade is First"

else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=200 and tot<240 Then
       msgbox "Grade is Second"

else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=160 and tot<200 Then
       msgbox "Grade is Third"
else
msgbox "No Grade, Fail"

End If
End If
End If
End If

Steps:
1.      Declar the variables e,m,p,c, tot.
2.      Entre the English marks.
3.      Entre the maths marks.
4.      Entre the physics marks.
5.      Entre the chemistry marks.
6.      add the total marks and store into tot variable.
7.      create the if condition.
8.      display the message.
9.      create the if condition.
10.  display the message.
11.  create the if condition.
12.  display the message.
13.  create the if condition.
14.  display the message.
15.  end if condition
16.  end if condition
17.  end if condition.


Set qtp=createobject("quicktest.application")
qtp.Launch
qtp.Visible=true
qtp.Open "D:\Tests\action parameter"
a=qtp.Test.Actions.Count
msgbox a
For i=1 to a
msgbox qtp.Test.Actions.Item(i).Name
msgbox qtp.Test.Actions.Item(i).Type
Next
p=qtp.Test.ParameterDefinitions.count
msgbox p
For i=1 to p
msgbox qtp.Test.ParameterDefinitions.Item(i).Name
msgbox qtp.Test.ParameterDefinitions.Item(i).Type
msgbox qtp.Test.ParameterDefinitions.Item(i).DefaultValue
Next
qtp.Quit

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

Steps:

1.   Declare a variable‘d’.
2.   Create an object ‘mw’ for word application.
3.   Create an object‘d’.
4.   Set the property in ‘d’ object for link.
5.   Create object a for child object.
6.   Create for loop.
7.   Create new file.
8.   get inner text property value and store into s variable
9.   Insert the value into new file.
10.        Create if condition and check spellings.
11.        Print report for spelling error.
12.        end if condition
13.        Close the word document.
14.        Set next statement for for loop.
15.        Close the documents.
16.        Set object nothing.






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("srss.srss46@gmail.com","hi","This is test mail for tsting","")

Steps:
1.   Create function for send mails.
2.   Set object for outlook application.
3.   Set object for item.
4.   Assign the variables ‘sendto’.
5.   Assign the variables ‘body’.
6.   Create if condition.
7.   Create attachments.
8.   End if condition.
9.   Send mail.
10.        Close the out look application.
11.        Set m object empty.
12.        Set otl object empty.
13.        end function
14.         call sendmail function


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
steps:
1.   declare an variable for InternetExplorer ‘ie’
2.   Create object for InternetExplorer.Application.
3.   Check visibile property.
4.   Navigate yahoomail.com.
5.   Create variable x for getting title prpperty value.
6.   Display the message box.


Function for Counting Objects from any opened web page

Function Objects_Count(myObject)
   Dim Objects
   Set Objects=Description.Create
 Objects("micclass").value=myObject
 Set Object=Browser ("title :=.*") .Page ("title :=.*") .ChildObjects (Objects)
 TotObjects=Object.Count
 Msgbox TotObjects
End Function
Call Objects_Count("WebButton")

Steps:

1.   Create function for objects count.
2.   Declare variable objects.
3.   Set object for description programming.
4.    Assign properties for object.
5.   set object description to find
6.   Count total object.
7.   Display total objects count in message box.
8.   End function.
9.   Call function object count.


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

Steps:
1.   Create object for description programming.
2.   Assign the properties.
3.   Set child objects.
4.   Print number of browsers.
5.   Create for loop.
6.   Close the browser.
7.   Set next statement for loop.



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

Steps:

1.      Set option explicit for new variables.
2.      Declare a variable.
3.      Set object for description programming.
4.      Set the property for link.
5.      Invoke the Internet Explorer browser.
6.      Set child object for link.
7.      Count total links.
8.      Create for loop.
9.      Get link property values.
10.  Create if condition for Gmail link.
11.  Print report in result html page.
12.  End if condition.
13.  Set next condition for loop.

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

Steps:
1.      Declare variables.
2.      set object for description programming.
3.       
Insert Data into a database table using Database Command Object

Dim objCon,objCom
Set objCon=Createobject("ADODB.connection")

objCon.open"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\gcreddy.mdb;"

Set objCom=Createobject("ADODB.Command")
objCom.ActiveConnection=objCon

objCom.CommandText="insert into Emp values('G C Reddy',88233,30000)"
objCom.Execute

objCon.Close
Set objCom=Nothing
Set objCon=Nothing



No comments:

Post a Comment