Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Working with DataBase in QTP - Understanding Connection and Recordset object

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.

rs.move – moves the pointer in a recordset by specified count as defined in numrec
rs.move numrec, start.

Also  movenext,moveprevious, movefirst, movelast can be used to move to specified location in recordset.


rs.fields.count gives number of items in the fields collection.


rs.field.item(i) returns specified item from the collection.




28 comments:

  1. the easy way to learn qtp and you covered basic knowledge on qtp . QTP Tutorials

    ReplyDelete
  2. Thanks srikanth for liking the blog.

    ReplyDelete
  3. HI,

    the 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/

    ReplyDelete
    Replies
    1. Hi,
      Verify 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

      Delete
  4. Hi Nitin

    Thanks 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.

    ReplyDelete
    Replies
    1. once you have data in a recordset, you can fetch the data from a recorset as shown bwlow:

      strval = 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

      Delete
  5. Hi Nitin,

    Code 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?

    ReplyDelete
    Replies
    1. 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:

      set 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

      Delete
  6. 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

    ReplyDelete
    Replies
    1. In 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

      Delete
  7. Currently i m using the below method in IE to identify the web table but it not working in Chrome

    intColumncount = 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)

    ReplyDelete
  8. 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.

    ReplyDelete
  9. In my application Header is Column Header is separate table and content table is displays has separate table. I m able to identify the header.
    I 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.

    ReplyDelete
    Replies
    1. Hey, try some logic similar to this, assuming this table is the next table inside same parent div.(both table have same parent)

      Set 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

      Delete
  10. Hi nitin,
    thanks 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.

    ReplyDelete
  11. Yes,
    using child nodes , parent nodes and sibling elements, you can traverse through different elements in the DOM

    ReplyDelete
  12. hi nithin your having excellent subject

    ReplyDelete
  13. How to connect database in qtp without the use of adodb.connection method?

    ReplyDelete
  14. How to connect to DB in qtp without use of adodb.connection method

    ReplyDelete
  15. How to connect database in qtp without the use of adodb.connection method?

    ReplyDelete
  16. Nice explanation on Testing, testing is required for all domain/ product keep it up for more testing Tools online training

    ReplyDelete


  17. It 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

    ReplyDelete
  18. Very very neatly and nicely explained about Selenium IDE .Once Again thanks for posting.Really Nice Blog With unique content On Qtp.

    ReplyDelete