Monday, 29 December 2014

VB Script to find the number of occurences of a char in a string

Method 1:
str="Hello world"
Set regex=new RegExp
regex.pattern="l"
regex.global=true
Set matches=regex.execute(str)
msgbox matches.count

Method 2:
arr=split(str,"l")
msgbox ubound(arr)

Method 3:
counter=0
For i=1 to len(str)
   char=mid(str,i,1)
   If char="l" Then
     counter=counter+1
   End If
Next

msgbox counter

QTP - Sql Server 2005 Connections



Option Explicit
Dim con,rs, i

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.Open"Provider=sqloledb;Server=SYS;Database=csr_db;uid=sa;pwd="
rs.open "select * from csr1",con
i=0
MsgBox rs.Fields.Count
Do while not rs.eof
    print "Row  :  "&i
    print rs.fields(0).Name &" : "&  rs.fields(0).Value
    print rs.fields(1).Name &" : "&  rs.fields(1).Value
    print rs.fields(2).Name &" : "&  rs.fields(2).Value
    rs.movenext
    i = i+1
Loop

'Release objects'Release objects
Set rs= nothing
Set con= nothing

QTP - Accessing Databases



QTP Scripts for connecting to MS Access:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;"

rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects'Release objects
Set rs= nothing
Set con= nothing

Note: The database we are using here is MS Access.Before running this script create a table in MS Acess.In the above script I used table called "emp" and column 'names as "v1" and "v2". "d:testdata.mdb" is path of the table which we created. The main use of this script is to use testdata of table(which is in  ' database) in the application. In the above script we are passing values from database to Textboxes in Windows Application.

QTP Script for connecting to sqlserver: 
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open"Driver={SQL Server};server=MySqlServer;uid=MyUserName;pwd=MyPassword;database=pubs"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects'Release objects
Set rs= nothing
Set con= nothing

QTP Script for connecting to oracle:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open "Driver={Microsoft ODBC for Oracle};Server=QTPWorld; Uid=your_username;Pwd=your_password;"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects
Set rs= nothing
Set con= nothing

QTP Script for connecting to MySQL:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open"Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDB;User=Uname;Password=Pwd;Option=3;"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects
Set rs= nothing
Set con= nothing

QTP Script for connecting to Excel:
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

con.open "DRIVER={Microsoft Excel Driver (*.xls)};DBQ=C:\TestStatus.xls;Readonly=True"
rs.open "SELECT count(*) FROM [Status$] where Status = 'Failed' ",con

Msgbox rs(0)

'Release objects
Set rs= nothing
Set con= nothing


QTP Script for connecting to Sybase:
  
Option Explicit
Dim con,rs

Set con=createobject("adodb.connection")
Set rs=createobject("adodb.recordset")

' Open a session to the database
con.open"Driver={SYBASE SYSTEM 11};Srvr=myServerAddress;Uid=Uname;Pwd=Pwd;Database=myDataBase;"
rs.open "select * from emp",con

Do while not rs.eof
VbWindow("Form1").VbEdit("val1").Set rs.fields("v1")
VbWindow("Form1").VbEdit("val2").Set rs.fields("v2")
VbWindow("Form1").VbButton("ADD").Click
rs.movenext
Loop

'Release objects
Set rs= nothing
Set con= nothing

Friday, 26 December 2014

Opened Notepad files closed using the QTP script


set descObj=description.create
descObj("micclass").value=".*"

set descObjcollection= desktop.ChildObjects(descObj)
MsgBox descObjcollection.count

For i=0 to descObjcollection.count-1
    print descObjcollection(i).getroproperty("title")
    '
descObjcollection(i).highlight
    text = descObjcollection(i).getroproperty("title")
    If instr(text, ".txt") Then
         descObjcollection(i).close
    End If
Next

Data Table Methods

AddSheet: This method is used to add a new sheet to the data table.
Syntax: DataTable.AddSheet(“Sheet Name”)
Eg: DataTable.AddSheet(“MySheet”)
Note: All the methods of data table object will have impact only during Runtime

DeleteSheet: This method is used to delete a sheet from the data table.
Syntax: DataTable.DeleteSheet(“” or )
Eg: DataTable.DeleteSheet(“Global”)
(OR) DataTable.DeleteSheet(1)

Import: This method is used to import the contents of an excel file into data table. While importing from an excel file we need to ensure the following things:
1.The number of sheets in excel file & in data table should be equal or more.
2.The sheet names may not be same. (We can modify the sheet names after importing)
3.The parameters names should be same.
Syntax: DataTable.Import(“”)
Eg: DataTable.Import(“D:\QTP\input.xls”)
Steps to import the data manually:
1.Right click --> Import from file --> Click on OK
3.Select the file name from which the data needs to be imported
4.Click on Open

ImportSheet: This method is used to import the data from the specific sheet of the excel file to the specific sheet of the data table.
Syntax: DataTable.ImportSheet “”,,
Eg: DataTable.ImportSheet “D:\QTP\input.xls”,2,1

Steps to import manually:
1.Right click in the required sheet of data table
2.Select Sheet --> Import --> From File --> Click on OK
3.Select the required file from which the data needs to be imported
4.Select the required sheet name from the dropdown
5.Click on OK

Export: This method is used to export the contents of the data table to an excel file. Even if the file is not available, it will create the file & export. If the file is available it will over ride.
Syntax: DataTable.Export(“”)
Eg: DataTable.Export(“D:\QTP\output.xls”)

Steps to export manually:
1.Right click in the data table
2.Select File --> Export
3.Provide the File Name --> Click on Save

ExportSheet: This method is used to export the contents of a specific sheet of the data table to the excel file.
Syntax: DataTable.ExportSheet “”,
Eg: DataTable. ExportSheet “D:\QTP\output.xls”,2

Steps to import manually:
1.Right click in the required sheet of data table
2.Select Sheet --> Export
3.Provide the File Name --> Click on Save

GetCurrentRow: This method is used to identify the current active row number
Eg: ActRow=DataTable.GetCurrentRow
msgbox ActRow

GetRowCount: This method is used to get the number of records available in the data table.
Eg: RowCnt=DataTable.GetRowCount
msgbox RowCnt
Note: Methods like GetCurrentRow, GetRowCount, etc defaultly works on global sheets.


LocalSheet: This method is used to work with the corresponding local sheet from which action we execute the script.
Eg: RowCnt=DataTable.LocalSheet.GetRowCount
msgbox RowCnt

GetSheetCount: This method is used to get the number of sheets available in the data table.
Eg: SheetCnt=DataTable.GetSheetCount
msgbox SheetCnt

SetCurrentRow: This method is used to set the focus to the specified row of the ‘data table-global sheet’.
Syntax: Datatable.SetCurrentRow()
Eg: Datatable.SetCurrentRow(17)

SetNextRow: This method is used to set the focus to the immediate next row of the current active row.
Syntax: DataTable.SetNextRow

SetPrevRow: This method is used to set the focus to the previous row of the current active row.
Syntax: DataTable.SetPrevRow

Value: This method is used for 2 purposes.
1. To read the value from the data table.
Syntax: Val=DataTable.Value(“”,“”)
Eg: Val=DataTable.Value(“Num1”,2)
msgbox Val
Note: We will not define the row number. By default it will pick the value of the current focus row.

2. To write the value into the data table.
Syntax: DataTable.Value(“”,“”)= “Value”
Eg: DataTable.Value(“Result”,”Action1”)=“Pass”
(OR) DataTable.Value(3,2)=“Pass”

Script to perform DDT manually:

DataTable.Import("D:\Practise\QTP Testing\input.xls")
RC=DataTable.GetRowCount
msgbox RC
For i=1 to RC
DataTable.SetCurrentRow(i)
VbWindow(“VbWindow”).VbEdit(“VbEdit”).SetDataTable.Value (“num1”,1)
VbWindow(“VbWindow”).VbEdit(“VbEdit_2”).SetDataTable.Value(“num2”,dtGlobalSheet)
VbWindow(“VbWindow”).VbButton(“Add”).Click
ExpVal=Datatable.Value(“ExpRes”,1)
ActVal=VbWindow(“VbWindow”).VbEdit(“VbEdit_3”).GetROProperty(“text”)
DataTable.Value(“ActRes”,1)=ActVal
If ExpVal=ActVal Then
DataTable.Value(“Result”,1)=”Pass”
Else
DataTable.Value(“Result”,1)=”Fail”
End If
Next
DataTable.Export(“D:\Practise\QTP Testing\output.xls”)

Thursday, 25 December 2014

How to update XML data from QTP.


'How to rename the title of first book
Const XMLDataFile = "C:\TestData.xml"
Const XMLNewFile = "C:\TestData2.xml"

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
xmlDoc.Load(XMLDataFile)

' update the title of the first book
Set node = xmlDoc.SelectSingleNode("/bookstore/book[0]/title")
node.Text = "Romeo and Juliet - Salvation"

' save changes
xmlDoc.Save(XMLNewFile)

' update the attribute of the second book
Set node = xmlDoc.SelectSingleNode("/bookstore/book[1]/title/@published")
node.Text = "2009"

' select a parent node
Set parentNode = xmlDoc.SelectSingleNode("/bookstore/book[2]")

' add a new author
Set newNode = xmlDoc.CreateElement("author")
newNode.Text = "Mr. Noname"
parentNode.AppendChild (newNode)

' select a parent node
Set parentNode = xmlDoc.SelectSingleNode("/bookstore/book[2]")

' add its attribute
Set newAttrib = xmlDoc.CreateAttribute("bestseller")
newAttrib.Text = "yes"
parentNode.Attributes.SetNamedItem(newAttrib)

-------------XML file content ------------------

<?xml version="1.0"?>
<bookstore>
    <book>
        <title published="1595">Romeo and Juliet</title>
        <author>William Shakespeare</author>
    </book>
    <book>
        <title published="1997">Yet Another Book</title>
        <author>John Smith</author>
    </book>
    <book>
        <title published="2008">Absolute Knowledge</title>
        <author>John Smith</author>
        <author>Mark Coverdy</author>
    </book>
</bookstore>

How to read XML file from QTP


XMLDataFile = "C:\Documents and Settings\qa\Desktop\TestData.xml"

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
xmlDoc.Load(XMLDataFile)

Set nodes = xmlDoc.SelectNodes("/bookstore/book")
MsgBox "Total books : " & nodes.Length

' get all titles
Set nodes = xmlDoc.SelectNodes("/bookstore/book/title/text()")

' get their values
For i = 0 To (nodes.Length - 1)
    Title = nodes(i).NodeValue
    MsgBox "Title #" & (i + 1) & ": " & Title
Next

Set node = xmlDoc.SelectSingleNode("/bookstore/book[0]/title/text()")
MsgBox "Title of 1st book: " & node.NodeValue

' get list of John Smith's books
Set nodes = xmlDoc.SelectNodes("/bookstore/book/title[../author = 'John Smith']/text()")

' get their titles
For i = 0 To (nodes.Length - 1)
    Title = nodes(i).NodeValue
    MsgBox "Title #" & (i + 1) & ": " & Title
Next

' get list of books published after 2003
Set nodes = xmlDoc.SelectNodes("/bookstore/book/title[@published > 2003]/text()")

' get their titles
For i = 0 To (nodes.Length - 1)
    Title = nodes(i).NodeValue
    MsgBox "Title #" & (i + 1) & ": " & Title
Next



-------------XML file content ------------------

<?xml version="1.0"?>
<bookstore>
    <book>
        <title published="1595">Romeo and Juliet</title>
        <author>William Shakespeare</author>
    </book>
    <book>
        <title published="1997">Yet Another Book</title>
        <author>John Smith</author>
    </book>
    <book>
        <title published="2008">Absolute Knowledge</title>
        <author>John Smith</author>
        <author>Mark Coverdy</author>
    </book>
</bookstore>

Friday, 19 December 2014

Popup appear/disappear with time gap

set objShell = createobject("wscript.shell")
objShell.Popup "Love u mom",2, "Kid says",64

Thursday, 18 December 2014

QTP alert window styles

MsgBox "Hello there 0",0,"Greetings From csr"
MsgBox "Hello there 1",1,"Greetings From csr"
MsgBox "Hello there 2",2,"Greetings From csr"
MsgBox "Hello there 3",3,"Greetings From csr"
MsgBox "Hello there 4",4,"Greetings From csr"
MsgBox "Hello there 5",5,"Greetings From csr"
MsgBox "Hello there 16",16,"Greetings From csr"
MsgBox "Hello there 32",32,"Greetings From csr"
MsgBox "Hello there 64",64,"Greetings From csr"
MsgBox "Hello there 256",256,"Greetings From csr"
MsgBox "Hello there 512",512,"Greetings From csr"
MsgBox "Hello there 768",768,"Greetings From csr"


0
0x0
Show OK button.
1
0x1
Show OK and Cancel buttons.
2
0x2
Show Abort, Retry, and Ignore buttons.
3
0x3
Show Yes, No, and Cancel buttons.
4
0x4
Show Yes and No buttons.
5
0x5
Show Retry and Cancel buttons.
6
0x6
Show Cancel, Try Again, and Continue buttons.
16
0x10
Show "Stop Mark" icon.
32
0x20
Show "Question Mark" icon.
48
0x30
Show "Exclamation Mark" icon.
64
0x40
Show "Information Mark" icon.
256
0x100
The second button is the default button.
512
0x200
The third button is the default button.
4096
0x1000
The message box is a system modal message box and appears in a topmost window.
524288
0x80000
The text is right-justified.
1048576
0x100000
The message and caption text display in right-to-left reading order, which is useful for some languages.
-1
The user did not click a button before nSecondsToWait seconds elapsed.
1
OK button
2
Cancel button
3
Abort button
4
Retry button
5
Ignore button
6
Yes button
7
No button
10
Try Again button
11
Continue button