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