Read and store the values from Excel rows to variables/arrays using VBscripting in UFT

3 min read 07-10-2024
Read and store the values from Excel rows to variables/arrays using VBscripting in UFT


Automating Excel Data Extraction with VBScript in UFT: A Step-by-Step Guide

Problem: You need to extract data from an Excel spreadsheet and use it within your UFT test automation script. Manually copying and pasting data is time-consuming and error-prone.

Solution: Leverage the power of VBScript to read and store Excel data directly into your UFT script, automating the process and improving efficiency.

Scenario: Let's say you have an Excel spreadsheet containing test data for a web application login. You want to use UFT to automate login tests, dynamically pulling credentials from the spreadsheet.

Original Code (Illustrative Example):

'Set up Excel object
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\TestData.xlsx")
Set objSheet = objWorkbook.Worksheets("Sheet1")

'Read data from first row
strUsername = objSheet.Cells(1, 1).Value
strPassword = objSheet.Cells(1, 2).Value

'Close Excel
objWorkbook.Close
objExcel.Quit

'Use the read values in UFT test
Browser("title:=Login").Page("title:=Login").WebEdit("id:=username").Set strUsername
Browser("title:=Login").Page("title:=Login").WebEdit("id:=password").Set strPassword

Analysis and Clarification:

  1. Excel Object Model: VBScript uses the Excel object model to interact with Excel files. This model allows you to manipulate worksheets, cells, and other Excel components.
  2. Reading Cell Values: The code reads the values from specific cells using Cells(row, column).Value.
  3. Dynamic Data: This example demonstrates how to dynamically read data from the spreadsheet and use it within your UFT script, allowing for test cases to be driven by data.

Optimizing for Readability and Robustness:

  1. Error Handling: Always incorporate error handling to gracefully manage unexpected scenarios, like missing files or invalid data.
  2. Looping through Rows: To process multiple rows of data, implement a loop to iterate through the desired range.
  3. Data Storage: Store data in arrays for easy access and manipulation within your UFT script.

Enhanced Example:

'Set up Excel object
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False  'Hide Excel window
Set objWorkbook = objExcel.Workbooks.Open("C:\TestData.xlsx")
Set objSheet = objWorkbook.Worksheets("Sheet1")

'Declare an array to store data
Dim arrTestData()

'Get the last used row in the sheet
lastRow = objSheet.UsedRange.Rows.Count

'Resize the array to hold data from all rows
ReDim arrTestData(lastRow - 1, 1)

'Loop through rows and store data in array
For i = 2 To lastRow
    arrTestData(i - 2, 0) = objSheet.Cells(i, 1).Value 'Username
    arrTestData(i - 2, 1) = objSheet.Cells(i, 2).Value 'Password
Next

'Close Excel
objWorkbook.Close
objExcel.Quit

'Use the stored data in UFT test
For i = 0 To UBound(arrTestData)
    Browser("title:=Login").Page("title:=Login").WebEdit("id:=username").Set arrTestData(i, 0)
    Browser("title:=Login").Page("title:=Login").WebEdit("id:=password").Set arrTestData(i, 1)
    'Execute login steps...
Next

Additional Value:

  • Test Data Management: This approach simplifies test data management, allowing for easy updates and modifications to test scenarios.
  • Data-Driven Testing: The ability to read data from an external source empowers you to implement data-driven testing techniques, enhancing your automation efforts.

Resources:

Conclusion: Reading and storing data from Excel using VBScript in UFT enables powerful automation and data-driven testing. By following the steps outlined in this article, you can easily extract and use data from Excel spreadsheets, improving your test efficiency and effectiveness.