Powered by: Dharamveer Saxena

Wednesday, January 30, 2013

How to check if a process is still running in your windows


Many times your script need to know if a program or for example a bat file is still running. so, how you can check it using VBScript.

Below is the code which you can use to check if a process/program is still running

sub p_check()
with createobject("shell.application")
 for each wndw in .windows
   locationurl_ = cstr(wndw.LocationURL)
   if InStr(locationurl_, "locationurl") = 0 then
'here in locationurl you can pass the name of the bat file
 
else
'to quit the bat file if needed after script found it
     wndw.quit
end if
next
end with
End sub


Here, using wndw object in for each loop you can check the process based on its application name as well in which it is open for example iexplorer but you need to use correct method to get values based on your need

Reading and writing in XML using QTP - Vbscript


Using VbScript one can read and write into an XML file by creating an object of "Msxml2.DOMDocument"



If one needs to read or write into this XML file, then it can be done using the below procedure where I have used "object_" and "data_file_path" as parameter.
1. object_ can be "_path" if you want to get values inside "_path" tag in above XML
2. data_file_path is XML file path like "c:\data.xml"

Sub Write_read_XML(object_, data_file_path)

'creating object for "Msxml2.DOMDocument"
       Set xmlDoc = CreateObject("Msxml2.DOMDocument")

'load xml file on object
       xmlDoc.load(data_file_path)

'creating an object which can store all values in tag: like in this case all the values inside object_ tag will be saved in ElemList
       Set ElemList = xmlDoc.getElementsByTagName(object_)

'Now loop through all the tags with name object_ in your XML file
       for n = 0 to 1

      'Now to fetch value you can use:
               msgbox ElemList.item(n).text

      'Incase you need to save values in XML then you can use the code:
               ElemList.item(n).text = "value " & cstr(n)

       next
End Sub


Thursday, November 17, 2011

Using DOM in QTP

DOM (Document Object Method) is a method for QTP engineers to access the source of any webpage direct through VB Scripting.

Using the DOM we can find an object on a web page and then can perform any action like click, set values etc.

For example if you need to check total links on a web browser, then you can use the following code:

Set Links = Browser("Google").Page("Google").Object.all.tags("a")
Msgbox "Total links: " & Links.Length


Or you can also use the following code:


Set Links = Browser("Google").Page("Google").Object.Links
Msgbox "Total links: " & Links.Length


Here, Object property of Page object is used where it represents the HTML document in a given browser. This browser contains links and other objects too. Here, I have used Length property to get the number of items in a collection.


----


Using DOM object we can also find the object on the web page by using the methods like GetElementsByTagName, GetElementByID and more and then perform operations on it.

eg.

we can set value in textbox by using the getElementByID method on browser:

Browser("Google").Page("Google").Object.getElementByID("webeditname").Value="Testing"

Saturday, April 2, 2011

How to get clipboard text using VBScript

' Open notepad
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad", 9

' Give Notepad time to load
WScript.Sleep 500

' Do The Paste
WshShell.SendKeys "%e"
WshShell.SendKeys "p"

Tuesday, June 29, 2010

How to handle null strings

In TestComplete whenever DDT diver or array or anything returns null or nothing instead of a string value then it gives error message. For example if you are fetching values using the DDT driver as follow it will return you the data string from the excel file if the value exists:

'path is the path of axcel file
'sheet is the sheet ID of excel file
'A is the column name in excel file

set inputdata = DDT.ExcelDriver(path,sheet)
name = inputData.value("A")

In case if it does not exists then, it will trough an error/exception. You can handle it versy easily by using &"" with the string.

set inputdata = DDT.ExcelDriver(path,sheet)
name = inputData.value("A") &"" 'this will add nothing but will remove the exception you are getting.

How to use Regular Expression to test Email ID and other strings


A regular expression can be used as follow:

'Here x is the string that needed to be tested and y is the pattern against which x will be tested


Function RegExpression(x,y)
Dim reg
Set reg = New RegExp
reg.Pattern = y ' Regular Expression filter
reg.IgnoreCase = True 'ignoring the case sensitive
RegExpression = reg.Test(x) ' Test the captured value against the filter
End Function


The above function can be used in the main procedure as:

Sub RegularExpression
x = "0.10" ' Captured value
y = "0[.]10" ' Regular expression filter and expected value
z = RegExpression(x,y) ' Call the function
if z = -1 then
log.message "The value is valid"
else
log.error "The value is not valid"
end if
End Sub


'An email ID can be tested with Regular expression using the above function by passing the following patern:

'here y can change as per the characters allowed in the email ID

Sub RegularExpression
x = "abc27@yahoo.com" ' Captured value
y = "^[A-Z0-9]+@[A-Z0-9]+.[A-Z]{3}" ' Regular expression filter and expected value
z = RegExpression(x,y) ' Call the function
if z = -1 then
log.message "The value is valid"
else
log.error "The value is not valid"
end if
End Sub

How to get excel row count


Excel row count can be get by using the following function:

function get_row_count(path,sheet)
dim inputdata
dim rowc
set inputData = DDT.ExcelDriver(path, Sheet)
while not inputdata.EOF
inputdata.Next
rowc = rowc+1
Wend
get_row_count = rowc
end Function

Wednesday, November 25, 2009

Testcomplete: To check if the file exists and to delete a file if it exists

Below is the vbscript code that can be used to check if the file exists on system or not

'In below examples: path is the path of file like "c:\text.txt"

Sub Main
set fs=createObject("Scripting.FileSystemObject")
If not fs.FileExists(path) Then
Log.Message("Fail: file does not exist")
Else
Log.Message("Pass: File exists")
End If

End Sub

Below is the code to delete a file


Sub Delete

set fs=createObject("Scripting.FileSystemObject")
If not fs.FileExists(path) Then
Else
fs.deletefile(path)
End If

End sub

Tuesday, November 24, 2009

TestComplete: To get all the sheets name in your excel workbook

To get all the sheets name from the excel file.

Sub Main

Set p = Sys.WaitProcess("EXCEL")
'this will Make sure you close any Excel work book already open
If p.Exists Then
Call p.Terminate()
End If

'This is setting the driver
Set MsExcel = Sys.OleObject("Excel.Application")

'This will open you workbook
'Specify the location of your excel file
Call MsExcel.Workbooks.Open("c:\test.xls")
MsExcel.Visible = True

'To Loop through all the sheet
for I = 1 to MsExcel.sheets.Count
'To activate the sheet
msexcel.Sheets(I).Activate
'To get the name of the active sheet
MsgBox(MsExcel.activesheet.name)
Next

End Sub

Monday, November 16, 2009

Testcomplete : Data Driven Testing Framework

Data-driven testing is a framework where test input and output values are read from data files (datapools, ODBC sources, cvs files, Excel files, DAO objects, ADO objects, and such) and are loaded into variables in captured or manually coded scripts. In this framework, variables are used for both input values and output verification values. Navigation through the program, reading of the data files, and logging of test status and information are all coded in the test script.

Data-driven testing means using a single test to verify many different test cases by driving the test with input and expected values from an external data source instead of using the same hard-coded values each time the test runs. This way, you can test how the application handles various input without having a lot of similar tests that only have different data sets.


To learn how to perform Data Driven testing using Testcomplete, you can view the following video file: Data Driven Testing Video Tutorial

Thursday, November 5, 2009

TestComplete : To access column of Infragistics Grid using its index

To access column of Infragistics Grid using its index:

------------------------

function get_column(grid, columnindex)
set get_column = grid.DisplayLayout.Bands.Item(0).Columns.Item(columnindex)
end Function

---------------------

You can use this function as bellow to get the header caption of a particular column

sub main
MsgBox( get_column(DIGridControl,4).Header.get_Caption.ToString)
end sub

TestComplete : To access cell of Infragistics UltraGrid using row index and column index

To access Infragistics grid's cell on the basis of row and column index:

-------------------

function get_cell(grid, row, column)
set get_cell = grid.DisplayLayout.Rows.Item(row).Cells.Item(column)
end Function

-------------------

You can use this function as follow to set the value in it:

sub main
'here Ultragrid is the object of grid
call get_cell(Ultragrid, 0, 4).set_Value("checking")
end sub




Wednesday, November 4, 2009

TestComplete : To make a perticular tab selected (Infratistics Ultratabcontrol)

In infragistics Ultratabcontrol clicktab() method do not work correctly to make a tab selected or active. It gets recorded but at the time of play, the testcomplete does not click on the tab.

In this case you can use the following procedure to select a tab.

'--------------------------

'tabcontrol is the name of ultratabcontrol object
'tabname is the name of tab (string)

sub tab_click(tabcontrol, tabname)
dim I
for I = 0 to tabControl.Tabs.Count-1
if tabControl.VisibleTabs.get_Item(I).Text.OleValue= cstr(tabname) then
tabControl.VisibleTabs.get_Item(I).Selected = true
end If
Next
end Sub

'------------------------------------------

Monday, November 2, 2009

Testcomplete : To get cell value from excel (.xls) file

'To get the cell value from .xls file workbook you can use the following function directly by passing the following parameters:

'file is the path of the excel file (string)
'sheet is the name of sheet (workbook) (string)
'row and colum are the index of cell's row and column for which you need to get the value (int)

---------
Function GetExcelData(file,sheet,row,colum)
Dim xlApp, xlFile, xlSheet,val
dim iRowCount,iColCount,cLoop

Set xlApp = CreateObject ("Excel.Application") 'Create access object

Set xlFile = xlApp.Workbooks.Open(file)
'Open the excel file,like"C:\myTestData.xls"
Set xlSheet = xlFile.Sheets(sheet) 'Set the sheet name in which the testdata locates
iRowCount = row
iColCount = colum

For rLoop = 1 To iRowCount 'Fetch value from the first row to the last row
'For cLoop = 1 To iColCount ' If you want to get the value cell-to-cell, you can add this line code
'numAdd = numAdd&" "&xlSheet.Cells(rLoop,cLoop)
GetExcelData = xlSheet.Cells(rLoop,colum)
'Next
Next
xlFile.Close
xlApp.Quit

Set xlSheet = Nothing
Set xlFile = Nothing
Set xlApp = Nothing

end function
---------

You can also use this function to get all the values in a row for all the columns in excel file or vice versa by passing rows or column accordingly.

eg: to get all the values in all the rows for column(index=2), call the function in this way:

'row_count is the row count of the .xls file.

sub main
for row = 1 to row_count
msgbox(cstr(GetExcelData(path,sheet, row,2)))
end sub

OR
'To get all the values in all the columns in row (index = 2) , all the function in this way:

'col_count is the row count of the .xls file.

sub main
for col = 1 to col_count
msgbox(cstr(GetExcelData(path,sheet, 2,col)))
end sub

TestComplete : to get row count of excel sheet using DDT.exceldriver

'To get row count of excel sheet
'path = path of .xls file (c:/test.xls)
'sheet is the name of workbook sheet

function get_row_count(path,sheet)
dim inputdata
dim rowc
set inputData = DDT.ExcelDriver(path, Sheet)
while not inputdata.EOF
inputdata.Next
rowc = rowc+1
Wend
get_row_count = rowc
end Function

TestComplete : To generate random number between two numbers


'To get random number

'Randomize is used as a seed to Rnd so that it will generate a random number each time.
' A simple formula is used to generate a random number between two number: Int((max-min+1)*Rnd+min)


Function makeRandom(min, max)
Randomize
makeRandom = Int((max-min+1)*Rnd+min)
End function

Testcomplete : To check if the value exists in Grid cell


'To check if the value exists in the grid cell

' p_value is the value which you need to check
' grid is the grid in which you need to check
' colid is the ID (string) or index (int) of the column in which you need to check
' you can get the function GetCellValue () in my previous post "TestComplete: To get cell value using Row number and Column ID"

'This function will return 1 if it founds p_value in any of the cell in grid colid column or else will return 0 if does not found the p_value

function value(p_value, grid, colid)
value = "0"
for row = 0 to grid.rows.count -1
if GetCellValue(grid,row,colid) = CStr(p_value) then
value = "1"
end If
Next
End Function

TestComplete : To get cell value using Row number and Column ID


'To get the cell Value based on Grid, row number and column ID
'working in Infragistics grid as well
' Grid is grid object in which you need value for rowIndex row and ColumnId column
' You can pass either ID (string) or Index (int) of the column for ColumnId parameter

Function GetCellValue (Grid, RowIndex, ColumnId)
GetCellValue = Grid.wValue(rowindex,columnID)
end Function

TestComplete : Connect to SQL database and fetch data by executing query



' Sample Code to Connect to database in Test Complete
' Database connection using Testcomplete


Sub main

Set AConnection = ADO.CreateADOConnection

AConnection.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=DG18\SQLEXPRESS"

AConnection.LoginPrompt = False ' Suppress the login dialog box
AConnection.Open

Set RecSet = AConnection.Execute_("SELECT * FROM holding") ' Execute a simple query
RecSet.MoveFirst
While Not RecSet.EOF
Log.Message RecSet.Fields("Script").Value
RecSet.MoveNext
Wend
AConnection.Close
End sub