UFT Scripting Examples

UFT Scripting Examples

'1) Data driven Testing for Login functionality by Reading Test data directly from a Database Table
 

Dim objConnection, objRecordset
 'Create Database Connection object 'It is used to connect to Databases
Set objConnection = CreateObject("Adodb.Connection")
' Create Database Recordset object , used to perform operations on Database Tables(Records)
Set objRecordset = CreateObject("Adodb.Recordset")
'Create Connection String for MS Access Database
objConnection.Provider = ("Microsoft.Jet.OLEDB.4.0")

objConnection.Open "C:\Documents and Settings\G C reddy\Desktop\testdb.mdb"
objRecordset.Open "Select * from Login", objConnection

Do Until objRecordset.EOF
    SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
    Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set objRecordset.Fields(0)
Dialog("Login").WinEdit("Password:").Set objRecordset.Fields(1)
wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
objRecordset.MoveNext
Loop
objRecordset.Close 'To close the connection temperrarly
objConnection.Close 'To close the connection temperrarly

Set objRecordset = Nothing
Set objConnection = Nothing

'2) Data driven Testing by Reading Test data directly from a database (Range of Records (2 to 4))
 

Dim objConnection, objRecordset, RecordCount
 'Create Database Connection object 'It is used to connect to Databases
Set objConnection = CreateObject("Adodb.Connection")
' Create Database Recordset object , used to perform operations on Database Tables(Records)
Set objRecordset = CreateObject("Adodb.Recordset")
'Create Connection String for MS Access Database
objConnection.Provider = ("Microsoft.Jet.OLEDB.4.0")

objConnection.Open "C:\Documents and Settings\G C reddy\Desktop\testdb.mdb"
objRecordset.Open "Select * from Login", objConnection

RecordCount = 0
Do Until objRecordset.EOF
    RecordCount = RecordCount+1
    If RecordCount >= 2 And  RecordCount <= 4 Then
        SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
        Dialog("Login").Activate
    Dialog("Login").WinEdit("Agent Name:").Set objRecordset.Fields(0)
    Dialog("Login").WinEdit("Password:").Set objRecordset.Fields(1)
    wait 2
    Dialog("Login").WinButton("OK").Click
    Window("Flight Reservation").Close
    End If
objRecordset.MoveNext
Loop
objRecordset.Close 'To close the connection temporarily
objConnection.Close 'To close the connection temporarily

Set objRecordset = Nothing
Set objConnection = Nothing

'3) Data driven Testing by Reading Test data directly from a database (Specified Records (1 and 4))


Dim objConnection, objRecordset, RecordCount
 'Create Database Connection object 'It is used to connect to Databases
Set objConnection = CreateObject("Adodb.Connection")
' Create Database Recordset object , used to perform operations on Database Tables(Records)
Set objRecordset = CreateObject("Adodb.Recordset")
'Create Connection String for MS Access Database
objConnection.Provider = ("Microsoft.Jet.OLEDB.4.0")

objConnection.Open "C:\Documents and Settings\G C reddy\Desktop\testdb.mdb"
objRecordset.Open "Select * from Login", objConnection

RecordCount = 0
Do Until objRecordset.EOF
    RecordCount = RecordCount+1
    If RecordCount = 1 Or  RecordCount = 4 Then
        SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
        Dialog("Login").Activate
    Dialog("Login").WinEdit("Agent Name:").Set objRecordset.Fields(0)
    Dialog("Login").WinEdit("Password:").Set objRecordset.Fields(1)
    wait 2
    Dialog("Login").WinButton("OK").Click
    Window("Flight Reservation").Close
    End If
objRecordset.MoveNext
Loop
objRecordset.Close 'To close the connection temperrarly
objConnection.Close 'To close the connection temperrarly

Set objRecordset = Nothing
Set objConnection = Nothing

'4) Export data from Database to Excel file
'Export data from Database to Excel file
Dim objconnection, objRecordset,  objExcel, objWorkbook, objWorksheet, i
Set objconnection = Createobject ("Adodb.connection")
Set objRecordset = Createobject ("Adodb.Recordset")

Set objExcel = Createobject ("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Documents and Settings\G C reddy\Desktop\abcd.xls")
Set objworksheet = objWorkbook.Worksheets(1)

objconnection.Provider = ("Microsoft.Jet.OLEDB.4.0")
objconnection.Open "C:\Documents and Settings\G C reddy\Desktop\testdb.mdb"
objRecordset.Open "select * from Login", objconnection

objworksheet.cells(1, 1) = "Agent"  
objworksheet.cells(1, 2) = "Password"  
i = 2
Do While objRecordset.EOF = False
objWorksheet.Cells(i,1) = objRecordset.Fields(0)
objWorksheet.Cells(i,2) = objRecordset.Fields(1)
objRecordset.MoveNext
i = i + 1
Loop
objWorkbook.Save
objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing

objRecordset.Close
objconnection.Close

Set objconnection = nothing
Set objRecordset = nothing

'5) Export data from Database to Text file

'Export data from Database to Text file
Dim objFso, objTextstream, objconnection, objRecordset
Set objconnection = Createobject ("Adodb.connection")
Set objRecordset = Createobject ("Adodb.Recordset")

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile ("C:\Documents and Settings\G C reddy\Desktop\xyz.txt", 2)

objconnection.Provider = ("Microsoft.Jet.OLEDB.4.0")
objconnection.Open "C:\Documents and Settings\G C reddy\Desktop\testdb.mdb"
objRecordset.Open "select * from Login", objconnection

objTextstream.WriteLine "Agent Password"
objTextstream.WriteLine "--------------"

Do Until objRecordset.EOF
objTextstream.WriteLine objRecordset.fields("Agent") & ", "& objRecordset.fields("Password")
objRecordset.movenext
Loop

objTextstream.close
Set objTestream = Nothing
Set objFso = Nothing

objRecordset.Close
objconnection.Close

Set objconnection = nothing
Set objRecordset = nothing

'6) Export data from Excel to Text File


Dim objfso, objtextstream, objexcel, objworkbook, objworksheet, RowCount
Set objfso = createobject("scripting.filesystemobject")
Set objtextstream = objfso.OpenTextFile("C:\Documents and Settings\G C reddy\Desktop\xyz.txt", 2)

Set objexcel = CreateObject("Excel.Application")
Set objworkbook = objexcel.Workbooks.Open("C:\Documents and Settings\G C reddy\Desktop\abcd.xls")
Set objworksheet = objworkbook.Worksheets(1)

RowCount = objworksheet.UsedRange.Rows.Count

objtextstream.WriteLine "Agent  password"
For i = 2 To RowCount Step 1
objtextstream.WriteLine objworksheet.cells(i, 1) &", "& objworksheet.cells(i, 2)
Next
objexcel.Quit
objtextstream.Close
Set objtextstream = Nothing
Set objfso = Nothing

Set objworksheet = Nothing
Set objworkbook = Nothing
Set objexcel = Nothing

'7) Export data from Text file to Excel

Dim objfso, objtextstream, objexcel, objworkbook, objworksheet, myLine, myField
Set objfso = createobject("scripting.filesystemobject")
Set objtextstream = objfso.OpenTextFile("C:\Documents and Settings\G C reddy\Desktop\xyz.txt", 1)

Set objexcel = CreateObject("Excel.Application")
Set objworkbook = objexcel.Workbooks.Open("C:\Documents and Settings\G C reddy\Desktop\abcd.xls")
Set objworksheet = objworkbook.Worksheets(2)

objtextstream.SkipLine

objworksheet.Cells(1, 1) = "Agent"
objworksheet.Cells(1, 2) = "Password"

i = 2
Do Until objtextstream.AtEndOfStream
myLine = objtextstream.ReadLine
myField = Split(myLine, ", ")
objworksheet.Cells(i, 1) = myField(0)
objworksheet.Cells(i, 2) = myField(1)
i = i +1
Loop
objworkbook.Save
objexcel.Quit
objtextstream.Close
Set objtextstream = Nothing
Set objfso = Nothing

Set objworksheet = Nothing
Set objworkbook = Nothing
Set objexcel = Nothing

 '
8) Export data from Excel to Database

Dim conn, query, dbResults, strCurrentEnv, dbName, strConnection
Dim objexcel, objworkbook, objworksheet, RowCount

Set objexcel = CreateObject("Excel.Application")
Set objworkbook = objexcel.Workbooks.Open ("C:\Users\Lenovo\Desktop\xyz.xlsx")
Set objworksheet = objworkbook.Worksheets(1)

strCurrentEnv = "localhost"
dbName = "uft"

'My SQL connection string
strConnection = "DRIVER={MySQL ODBC 3.51 Driver}; DATABASE=uft;DSN=uft;OPTION=0;PWD=;PORT=3306;UID=noman;"
Set conn = CreateObject("ADODB.Connection")
conn.Open strConnection

'Create Database Recordset object , used to perform operations on Database Tables(Records)
Set dbResults = CreateObject("Adodb.Recordset")
RowCount = objworksheet.UsedRange.Rows.Count

For i = 2 To Rowcount Step 1
  
dbResults.Open "Insert into login values('" & objworksheet.Cells(i, 1) & "','" & objworksheet.Cells(i, 2)&"')" , conn
  
Next

objExcel.Quit

Set objWorksheet = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing

conn.Close 'To close the connection temperrarly
'dbResults.Close 'To close the connection temperrarly
Set conn = nothing
Set dbResults = nothing
 

'9) Export data from Text file to Database

Dim conn, query, dbResults
Dim objFso, objTextstream, myLine, myField

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\Lenovo\Desktop\abc.txt", 1)

strCurrentEnv = "localhost"
dbName = "uft"

'My SQL connection string
strConnection = "DRIVER={MySQL ODBC 3.51 Driver}; DATABASE=uft;DSN=uft;OPTION=0;PWD=;PORT=3306;UID=noman;"
Set conn = CreateObject("ADODB.Connection")
conn.Open strConnection

'Create Database Recordset object , used to perform operations on Database Tables(Records)
Set dbResults = CreateObject("Adodb.Recordset")

objTextstream.SkipLine
Do Until objTextstream.AtEndOfStream
    myLine = objtextstream.ReadLine
    myField = Split(myLine, ", ")
    dbResults.Open "insert into login values('" & myField(0) &"','"& myField(1)&"')", conn
Loop

objTextstream.close
Set objTestream = Nothing
Set objFso = Nothing

dbResults.Close 'To close the connection temperrarly
conn.Close 'To close the connection temperrarly
Set conn = nothing
Set dbResults = nothing


0 comments:

Post a Comment