QTP Interacts with database creating an instance of ADODB
object using ADO. ADO
is a Microsoft technology and stands for ActiveX Data Objects and is a
programming interface to interact and access data from a database.
Connection Object
Before a database can be
accessed by QTP, An object for database connection has to be established.
Set adocon =
createobject(“Adodb.connection”)
Once the object is created,
we need to set connection string to connect to the database. We can define a
connection string with details of database including database provider,
database, and user credentials for access.
Some useful methods and properties of connection object are as follows:
Properties:
adocon.connectionstring – This property sets or returns the details used to create a connection to a data source. Includes details of driver, database,username and password.
Strconnection = "Driver=… "Server=svrnme;uid=username;pwd=password;"
For e.g to connect to a database for excel the strConnection would be like:
strcon = "Driver={Microsoft Excel Driver (*.xls)};Dbqls="+xfilename +";ReadOnly=0;"
For details of connection strings, see www.connectionstrings.com
adocon.ConnectionTimeout – this defines the time to wait for a connection to be established.
adocon.provider – This sets or gets connection provider name.
adocon.state
– gives status whether connection is on or off.
Methods
adocon.open – opens a database connection based on the connection string provided.
adocon.Open connectionstring,userID,password,options
adocon.Execute – execute the sql statement provided
adocon.execute
“Select * from table”
adocon.close
– This closes the adodb connection.
RecordSet Object:
Once a
connection has been established, we can create recordset object to hold a set
of record from database. A recorset consists of records and column
Set rs =
createobject(“”Adodb.recordset”)
Some useful methods and properties of RecordSet
Objects are as follows:
Properties:
BOF property returns True if the current record position is before the first record in the Recordset,
EOF property returns True if the
current record position is after the last record in the Recordset, otherwise it
returns False. For
a empty recordset,i.e no records in the recordset or empty recordset, value of
BOF and EOF is false. So the property can be used in a loop to validate RecordSet
does not return any records.
MaxCount
Property returns the maximum value of records to be returned in a recordset.
rs.MaxCount
= 20 will return 20 rows of data in recordset.
Methods:
rs.cancel – cancels an existing execution.
rs.clone – returns a clone of existing recorset and assigns to an object
set rsclone = rs.clone
rs.Close - closes instance of recordset
rs.open – opens a recordset based on query specified.
rs.open
sqlquery, adocon
where
sqlquery is query executed and adocon is connection object.
the easy way to learn qtp and you covered basic knowledge on qtp . QTP Tutorials
ReplyDeleteThanks srikanth for liking the blog.
ReplyDeleteHI,
ReplyDeletethe above example is very useful.
I have one issue in DB using QTP..I need to a concept export a excel to MS access and selecting a particular row value. I tried this but i m facing is while exporting a DB file to QTP "file already Open. Can please help me to solve/
Hi,
DeleteVerify if the file is already open in another application or instance of access file in the task manager processes when the error is encountered.
try this link if above does not work:
http://support.microsoft.com/kb/174943
Hi Nitin
ReplyDeleteThanks for your reply.. I got it and it was solved.
As mentioned above i need a concept that get a value from a DB table and store a variable. Is it possible in QTP? If yes please help to to solve . I m unable to get the idea.
once you have data in a recordset, you can fetch the data from a recorset as shown bwlow:
Deletestrval = objRecordSet.Fields("FiledName").value
we can also move content of a recordset into an excelfile as shown below:
XLSheet.range("A2").copyfromrecordset rs
Where XLSheet is the reference of required sheet in the excel file and A2 is the location in sheet from which data will be pushed in the sheet
Hi Nitin,
ReplyDeleteCode given by you is worked very well i applied the concepts. Thanks for your valuable time. I have one more doubt. Which about object identification using DOM object in Chrome browser?
I need to identify the object using DOM because My application Web table don't have unique values and no property to identify that WebTable object i applied DOM object concepts using source index in IE it works but Chrome don’t have source index. Hence i have idea of using nextsibling i received error
Dim nexobjecttag,gstrObjectName,table,domobj
set gstrObjectName = objParent.WebTable(vstrObjectName).Object
On error resume next
gstrObjectName = gstrObjectName.Object
On error goto 0
Set domobj = gstrObjectName.document.documentElement '/////error occured object required while executing///
Set nexobjecttag = domobj.nextSibling
If nexobjecttag <> "table" Then
nexobjecttag = gstrObjectName.nextSibling
End If
Please advise to resolve the issue or provide if alternate method?
In this case, try to find a unique object in the table by its id, classname or text of the cell, and then use properties like Parentnode, firstchild ,sibling to create a relation similar to as shown in example below:
Deleteset objTd= Browser("title:=.*").Page("title:=.*").Object.getElementsByTagName("td")
For each Td in objTd
If (Td.innertext ="some text") Then
Set objLnk = Td.parentNode.parentnode
End if
Ok Nitin, as you mentioned "If (Td.innertext ="some text")" but in my application innertext values are dynamic. is that work if value is dynamic
ReplyDeleteIn case of dynamic object, you need to know the value that the cell will have. Are not there any headers in the table ,i.e fixed column names or so
DeleteCurrently i m using the below method in IE to identify the web table but it not working in Chrome
ReplyDeleteintColumncount = objParent.WebTable(vstrObjectName).ColumnCount(1)
Set objHeader = objParent.WebTable(vstrObjectName).Object
On error resume next
objHeader = objHeader.Object
On error goto 0
intHeadersourceindex = objHeader.sourceIndex
Set domobj =objHeader.document.documentElement
Set alltable = domobj.getElementsByTagName("TABLE")
i=0
For each table in alltable
If table.sourceIndex = intHeadersourceindex Then
Exit for
End If
i=i+1
Next
contentindex = Cstr(i+intColumncount)
No, there is no fixed column or any other value that's why i used native property source index for IE, now i need a method for chrome.
ReplyDeleteIn my application Header is Column Header is separate table and content table is displays has separate table. I m able to identify the header.
ReplyDeleteI have a question? Using the Header table if nextsibiling is table. shall we get that as object or that content table has a class value so as u mentioned in XML DOM.
Hey, try some logic similar to this, assuming this table is the next table inside same parent div.(both table have same parent)
DeleteSet objTableColl = Browser("Google").Page("title:=.*").Object.getelementsbytagname("table")
booltblFound = "false"
For each objTbl in objTableColl
Set objTdColl = objTbl.getelementsbytagname("td")
For each objTd in objTdColl
If objTd.innertext = "Expected Text" Then
set objExpectedTbl= objTbl.nextsibling
''' Perform required action
booltblFound = "true"
Exit For
End If
Next
if( booltblFound = "true") then
Exit For
End if
Next
hope it will help, please check the syntax, as I wrote this in a notepad
Hi nitin,
ReplyDeletethanks for the above code.. i have another question? Is it possible like child object, Shall we identify a object using Web element?
like if table cell values is displays as web element and entire table as web table.
Yes,
ReplyDeleteusing child nodes , parent nodes and sibling elements, you can traverse through different elements in the DOM
hi nithin your having excellent subject
ReplyDeleteAzure Online Training
ReplyDeleteAWS Online Training
Salesforce Online Training
Salesforce Admin Online Training
SAP UI5 Online Training
ReplyDeleteSAP HANA Admin Online Training
SAP HANA Online Training
SAP BO Online Training
SAP BO Admin Online Training
SAP BI Online Training
Devops Online Training
Java UI Online Training
Angularjs Online Training
BA Online Training
Pega Online Training
Oracle 11g Online Training
Oracle DBA Online Training
Oracle Finance Functional (R12) Online Training
IOS APP Online training
Android Online Training
SAS Clinical Online Training
SAS BI Online Training
Hadoop Online training
Hadoop ADMIN Online Training
Azure Online Training
AWS Online Training
Salesforce Online Training
Salesforce Admin Online Training
SAP UI5 Online Training
ReplyDeleteSAP HANA Admin Online Training
SAP HANA Online Training
SAP BO Online Training
SAP BO Admin Online Training
SAP BI Online Training
Devops Online Training
Java UI Online Training
Angularjs Online Training
BA Online Training
Pega Online Training
Oracle 11g Online Training
Oracle DBA Online Training
Oracle Finance Functional (R12) Online Training
IOS APP Online training
Android Online Training
SAS Clinical Online Training
SAS BI Online Training
Hadoop Online training
Hadoop ADMIN Online Training
Azure Online Training
AWS Online Training
Salesforce Online Training
Salesforce Admin Online Training
Hi nitin,
ReplyDeletethanks for the above code.. i have another question? Is it possible like child object, Shall we identify a object using Web element?
like if table cell values is displays as web element and entire table as web table.
SAP UI5 Online Training
SAP HANA Admin Online Training
SAP HANA Online Training
SAP BO Online Training
SAP BO Admin Online Training
SAP BI Online Training
Devops Online Training
Java UI Online Training
Angularjs Online Training
BA Online Training
Pega Online Training
Oracle 11g Online Training
Oracle DBA Online Training
Oracle Finance Functional (R12) Online Training
IOS APP Online training
Android Online Training
SAS Clinical Online Training
SAS BI Online Training
Hadoop Online training
Hadoop ADMIN Online Training
Azure Online Training
AWS Online Training
Salesforce Online Training
Salesforce Admin Online Training
How to connect database in qtp without the use of adodb.connection method?
ReplyDeleteHow to connect to DB in qtp without use of adodb.connection method
ReplyDeleteHow to connect database in qtp without the use of adodb.connection method?
ReplyDeleteNice explanation on Testing, testing is required for all domain/ product keep it up for more testing Tools online training
ReplyDelete
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in software testing your post help me a lot.Thanks for your informative article. software testing Training in chennai | Best software testing Training institute in velachery
Thanks For Posting About The concepts of Selenium Grid
ReplyDeleteVery very neatly and nicely explained about Selenium IDE .Once Again thanks for posting.Really Nice Blog With unique content On Qtp.
ReplyDelete