Showing posts with label QTP Codes.. Show all posts
Showing posts with label QTP Codes.. Show all posts

Tuesday, January 5, 2010

Working with Hashtable using QTP DOT Net Factory

Hashtable is similar to a Dictionary object. It stores values in key-item pairs. Hashtable can be implemented in QTP using DOT Net factory with .net framework mscorlib assembly.

The below line creates an instance of the Hashtable.

Set oHash=DotNetFactory.CreateInstance("System.Collections.Hashtable","mscorlib")

Now we are ready to work with Hashtable methods and properties in QTP.


Adding an element:
Syntax: Object.Add key, value

Example:
Set oHash=DotNetFactory.CreateInstance("System.Collections.Hashtable","mscorlib")
oHash.Add 1,"QTP"


Removing all elements:
Syntax: Object.Clear
Example: oHash.Clear
'This line clears all the elements in the Hashtable object

Counting Property:
The count property returns number of items in the hastable.
Msgbox oHash.Count


For more information about Hashtable, please navigate the below links,
http://msdn.microsoft.com/en-us/library/4yh14awz(VS.80).aspx
http://en.wikipedia.org/wiki/Hash_table
http://www.itl.nist.gov/div897/sqg/dads/HTML/hashtab.html

Saturday, January 2, 2010

Querying Text File

Most of the QTP developers are using delimited text file instead of datatable and data can be retrieved by using FileSystemObject. This is a good idea, but it has limitations which are listed in MSDN by Myer as follows,


No filtering. The nice thing about databases is that you can issue a query like "Select * From Logfile Where Result = 'Error'" and you'll get back only those records where the Result field is equal to error. That can't be done with the FileSystemObject. You might want only the records where Result is equal to Error, but you'll still have to read through the entire file, from top to bottom, checking the value of the Result field each time. That's not necessarily slower (the FileSystemObject is actually pretty darn fast), but it does make your code a bit trickier to write.

Difficulty in calculating statistics. Suppose you have a directory of some kind, and you'd like to count the number of people in each of your departments. With a database, you can issue a single query that will return that information in a flash. With the FileSystemObject, well, no such luck. Instead, you'll have to examine each record, and then use an array or a Dictionary object to manually tally up the number of people in each department. This will work, but it's tedious to code (and even more tedious if you have to change that code somewhere down the road).

One and done. Another problem with the FileSystemObject is that it's a one-way street, and a dead-end street to boot. What does that mean? Well, suppose you use the FileSystemObject to read through a text file and calculate a statistic of some kind. Now you want to read through it a second time, and calculate a second statistic. Oops, with the FileSystemObject there's no turning back: Once you get to the end of the file, you're done. You can't loop back through your file. Instead, you'll have to close the file and re-open it. Are there ways to work around this? Sure, but that's even more code you'll have to write.

Difficulty in getting at the individual fields. When you use the ReadLine method to read in a line from a text file, you get, well, a line from a text file. In other words, you'll get back something that looks like this:

Now we are able to overcome these obstacles by using Microsoft.Jet.OLEDB.4.0.

Step1: Create a text file as shown below and save it as CSV format at any location.

LastName,FirstName,MiddleInitial
Myer,Ken,W
Poe,Deborah,L

Step 2: Execute the code (Make sure to specify your text file's location in your code)
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

'You need to specify the name of the folder where the text file is stored. Note that you must use a trailing \ in the folder name. In the sample script below, the path is C:\Databases.
strPathtoTextFile = " C:\Databases"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""

'In your Microsoft® SQL query, specify the name of the text file you want to work with (in this example, PhoneList.csv).
objRecordset.Open "SELECT * FROM PhoneList.csv", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText

Do Until objRecordset.EOF
Msgbox "Name: " & objRecordset.Fields.Item("LastName")
Msgbox "Department: " & _
objRecordset.Fields.Item("FirstName")
Msgbox "Extension: " & objRecordset.Fields.Item("MiddleInitial")
objRecordset.MoveNext
Loop

Enjoy!!!!!!!!!!!!!
Source: MSDN

Thursday, December 31, 2009

Browser Control in QTP using DOT Net Factory

Create browser control instance in QTP using DOT Net factory, then you are able to browse any web pages without browsers. Execute the below code,


Set obj = DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
Set objBrwsr = DotNetFactory.CreateInstance("System.Windows.Forms.WebBrowser","System.Windows.Forms")

With objBrwsr
.Width=800
.Height=800
.AllowNavigation=True
.AllowWebBrowserDrop=True
.IsWebBrowserContextMenuEnabled=True
End With

With obj .Controls.Add objBrwsrEnd with
objBrwsr.Navigate("
www.google.com")
obj.showdialog

Set obj =Nothing
Set objBrwsr=Nothing


You will get DOT Net Dialog with browser control as same as below,



Thanks and regards,

Asiq Ahamed

Accessing HTML Properties using QTP

Part-1

Getting URL from HTML form.

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

If you want to access URL from a form's action, use the below code.
Syntax: Browser().Page().Object.getElementsByTagName("form")(IndexOfTheForm).action
Example: Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("form")(0).action

Throughout this article I have used Google for all my examples.

How to find hyperlink's color of your webpage

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

Here is the code to find link's color of your webpage.

Example: Msgbox Browser("name:=Google").Page("title:=Google").Object.alinkColor

How to find a textbox's auto-complete property is enabled or not

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

Use the below code to find whether the textbox's auto-complete property is enabled or not.

Example: Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByName("q")(0).autocomplete

Getting default checked property for radio button and checkbox

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

If you want to know radio button's or checkbox's default checked property in QTP, then use the simple code which I use frequently.

Msgbox Browser("name:=Google").Page("title:=Google").WebRadioGroup("name:=meta").Object.defaultChecked

Or

Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementByID("cty").defaultChecked

Finding host or hostname of the location or URL.

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

To find host and port number.

Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("A")(0).Host

To find host name.

Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("A")(0).HostName

Find destination URL

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

The below code to find a destination URL or an anchor point.

Msgbox Browser("name:=Google").Page("title:=Google").Object.getElementsByTagName("A")(0).Href

Part -2 will be published soon

Please refer the below link, if you want to know more about DOM.

http://www.learnqtp.com/document-object-model-qtp-guide/

Masking Password Using DotNet Factory

If you want to mask your password instead of placing in datatable or encrypting it, then use the below code.

Set oForm=DotNetFactory.CreateInstance("System.Windows.Forms.Form","System.Windows.Forms")
Set oTextBox=DotNetFactory.CreateInstance("System.Windows.Forms.TextBox","System.Windows.Forms")Set oButton = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y)

With oPoint
.x = 120
.y = 20
End With

With oButton
.Location = oPoint
.Width = 100
.Text = "OK"
End with

With oPoint

.x = 0
.y = 20
End With

With oTextBox

.Location = oPoint
.UseSystemPasswordChar=True
End with

'Add Controls

With oForm
.CancelButton = oButton
.Controls.Add oButton
.Controls.Add oTextBox
End with

oForm.ShowDialog
Msgbox oTextBox.Text



Set oForm=Nothing
Set oTextBox=Nothing
Set oButton=Nothing
Set oPoint=Nothing
Run the above code you will get a DOT Net dialog like below.


Enter your password and click on OK button. The entered password will be displayed in a message box.
For more information about DOT Net controls, please refer the below link.