UFT Tutorial 33

UFT Class 33 (Debugging Tests, Database Objects and VBScript Examples)

What is Debugging?

Locating and isolating errors thru Step by step execution.

Debugging is optional.

When Debugging is required?

Whenever Test is not showing any errors and not providing correct output.

How to debug?
Using VBScript debug commands and breakpoints.

i) Step into (F11)
    a) It starts the execution
    b) It executes one statement at a time.
    c) If it is function call, it opens the function and executes one function statement at a time.
-------------------------
ii) Step over (F10)
    a) It executes one statement at a time.
    b) It executes all functions statement at a time.

iii) Step out (Shift + F11)
------------
    a) It executes all remaining function statements at a time

Breakpoint (F9)

Types of Test Execution:

i) At a time execution (using Run Command)

ii) Step by step execution (Using Debug commands)

iii) Hybrid execution (Using Run command, debug commands and Breakpoints)
------------------------
Ex:
Dim a, b, c
a = 10
b = 20
c = a + b
a = 5
b = 3
c = a ^ b
Call Login("abcd", "mercury")
a = 10
b = 20
c = a - b
a = 5
b = 3
c = a * b
a=100
------------------------------
Database Objects
-----------------
i) Database Connection Object

It is used to connect to a Database (any database)

Create Database Connection Object:

Set Variable = CreateObject("Adodb.Connection")
------------------
ii) Database Recordset Object
It is used to perform operations on database tables.

Create Database Recordset Object:

Set variable = CreateObject("Adodb.Recordset")
-------------------------------------
Generate Connection String for MS Access Database:
Launch Notepad -> Save as udl file -> Click on udl File
-> Select database as MS Access-> OK

Open the File with Note Pad -> get the connection string

Examples:
------------
'Read Test data from a database and perform Data driven Testing for Login operation
Dim objConnection, objRecordset
Set objConnection = CreateObject("Adodb.Connection")
Set objRecordset = CreateObject("Adodb.Recordset")

'Create Connection String for MS Access Database
objConnection.Provider =("Microsoft.ACE.OLEDB.12.0")
objConnection.Open ("C:\Users\G C Reddy\Desktop\testdb.accdb")
objRecordset.Open "Select Agent, Password from Login", objConnection

Do Until objRecordset.EOF = True
SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe”
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set objRecordset.Fields("Agent")
Dialog("Login").WinEdit("Password:").Set objRecordset.Fields("Password")
Wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
objRecordset.MoveNext
Loop
objRecordset.Close
objConnection.Close

Set objRecordset = Nothing
Set objConnection = Nothing
--------------------------------------------
i) Export data from a database to an excel file
Dim objConnection, objRecordset, objExcel, objWorkbook, objWorksheet
Set objConnection = CreateObject("Adodb.Connection")
Set objRecordset = CreateObject("Adodb.Recordset")

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\G C Reddy\Desktop\abc.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:\Users\G C Reddy\Desktop\testdb.accdb"
objRecordset.Open "Select Agent, Password from Login", objConnection

objWorksheet.Cells(1, 1) = "Agent"
objWorksheet.Cells(1, 2) = "Password"
i = 2
Do Until objRecordset.EOF = True
objWorksheet.Cells(i, 1) = objRecordset.Fields("Agent")
objWorksheet.Cells(i, 2) = objRecordset.Fields("Password")
objRecordset.MoveNext
i= i+ 1
Loop
objWorkbook.Save
objExcel.Quit
objRecordset.Close
objConnection.Close
Set objWorksheet= Nothing
Set objWorkbook= Nothing
Set objExcel= Nothing

Set objRecordset= Nothing
Set objConnection= Nothing
-------------------------------------------------------
ii) Export data from a database to a text file.
Dim objConnection, objRecordset, objFso, objTextstream
Set objConnection = CreateObject("Adodb.Connection")
Set objRecordset = CreateObject("Adodb.Recordset")

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objfso.OpenTextFile("C:\Users\G C Reddy\Desktop\xyz.txt", 2)

objConnection.Provider = ("Microsoft.ACE.OLEDB.12.0")
objConnection.Open "C:\Users\G C Reddy\Desktop\testdb.accdb"
objRecordset.Open "Select Agent, Password from Login", objConnection

objTextstream.WriteLine "Agent Password"

Do Until objRecordset.EOF = True
objTextstream.WriteLine  objRecordset.Fields("Agent") &", "& objRecordset.Fields("Password")
objRecordset.MoveNext
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
objRecordset.Close
objConnection.Close
Set objRecordset= Nothing
Set objConnection= Nothing
---------------------------------------
iii) Export data from an excel file to a text file
Dim objFso, objTextstream, objExcel, objWorkbook, objWorksheet, RowCount
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\G C Reddy\Desktop\abc.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objfso.OpenTextFile("C:\Users\G C Reddy\Desktop\xyz.txt", 2)

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

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing

objExcel.Quit
Set objWorksheet= Nothing
Set objWorkbook= Nothing
Set objExcel= Nothing
----------------------------------------------
iv) export data from a text file to an excel file.
--------------------------------------------
Dim objFso, objTextstream, objExcel, objWorkbook, objWorksheet
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\G C Reddy\Desktop\abc.xlsx")
Set objWorksheet = objWorkbook.Worksheets(1)

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objfso.OpenTextFile("C:\Users\G C Reddy\Desktop\xyz.txt")
objTextstream.SkipLine
objWorksheet.Cells(1, 1) = "Agent"
objWorksheet.Cells(1, 2) = "Password"
i = 2
Do While objTextstream.AtEndOfStream = False
    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
Set objWorksheet= Nothing
Set objWorkbook= Nothing
Set objExcel= Nothing

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
-----------------------------------------
Assignments:
i) Export data from Excel file to database

ii) Export data from Text file to database
-------------------------------------------

0 comments:

Post a Comment