WORKING WITH QTP

TestEveryThinG

WEB Tree Control VB script QTP

Posted by rajivkumarnandvani on April 8, 2009

REM *************************************************************************
REM  Function  Click the Tree Node link  as per given Node name
REM  Input    objPage          := Paranet object  eg. webpage
REM  Input    ElementId          :=  Tree Control Web Element ID  (html id  value)   e.g  “ctl00_contRMSMaster_ucConnectionBasicInfo_trvwPopulateTree”
REM  Input    sName          := Name of the link But alway s give  the value in heriachy manner  by seprated comma  like “Utility,India,Hayana”
rem if  your Tree Stucture is Like This  Utility–>India–>Hayana or  Utility–>India–>Delhi
rem for click  India give the value  like This  sName =”Utility,India”
rem for click  Hayana  give the value  like This  sName =”Utility,India,Hayana”   not  alone Hayana always give the value from Root node in herarchy manner
REM  Input    nWaitTime          := time ( second)  for expand the tree control because On expand page get refresh  if  your page Get refresh on expand give the value as per refresh Time  Or If Page does not get refresh then give the minimum value
REM  Output(ReturnType) := return  none
REM  used functions are  fn_ExpandTree
REM  Created:    13/Mrach/2009    Rajiv Kumar Nandvani ## Changed:MM/DD/YYYY
REM  Example  fn_ClickTreeNode (objPageRMS ,”ctl00_contRMSMaster_ucConnectionBasicInfo_trvwPopulateTree”,”Utility,India” ,5 )

REM *************************************************************************

Public function fn_ClickTreeNode (byval objPage,byval ElementId ,byval sName ,byval nWaitTime )
rem store the all given node name in Array  seprated by comma using Split method
sGaahire =split(sName,”,”)
rem Run the Loop for  by counting  all given node name in Array
For i=0 to ubound(sGaahire)
rem call  function  fn_ExpandTree for  check Node name Exist or not  if Exist  and expandable  then expand the node
blCheck =   fn_ExpandTree( objPage, ElementId , trim(sGaahire(i)),nWaitTime)
rem check the array  sGaahire  last  value because last node value  we have to Click  (e.g  “Utility,India”  India will be last node )
If blCheck = True and i= ubound(sGaahire) Then
rem Call function  fn_clickTreelink for click the Last node Value
call   fn_clickTreelink(  objPage, ElementId , trim(sGaahire(i)))
End If
Next
Rem  Clear All  the Refrences to the Objects
Set  objPage = nothing
Set  objDiv = nothing
Set  sMatchName = nothing
Set objchildNodes =nothing
Set sMatchName =nothing
Set objTD =nothing
Set objIMG =nothing

End Function

REM *************************************************************************
REM  Function  Check  given  Tree Node exist  or Not
REM  Input    objPage          := Paranet object  eg. webpage
REM  Input    ElementId          :=  Tree Control Web Element ID  (html id  value)   e.g  “ctl00_contRMSMaster_ucConnectionBasicInfo_trvwPopulateTree”
REM  Input    sName          := Name of the node  But alway s give  the value in heriachy manner  by seprated comma  like “Utility,India,Hayana”
rem if  your Tree Stucture is Like This  Utility–>India–>Hayana or  Utility–>India–>Delhi
rem for Check   India give the value  like This  sName =”Utility,India”
rem for Check  Hayana  give the value  like This  sName =”Utility,India,Hayana”   not  alone Hayana always give the value from Root node in herarchy manner
REM  Input    nWaitTime          := time ( second)  for expand the tree control because On expand page get refresh  if  your page Get refresh on expand give the value as per refresh Time  Or If Page does not get refresh then give the minimum value
REM  Output(ReturnType) := True OR False  if node exist return True  Or False
REM  used functions are  fn_ExpandTree
REM  Created:    15/Mrach/2009    Rajiv Kumar Nandvani ## Changed:MM/DD/YYYY
REM  Example  fn_CheckTreeLinkExist (objPageRMS ,”ctl00_contRMSMaster_ucConnectionBasicInfo_trvwPopulateTree”,”Utility,India” ,5 )

REM *************************************************************************

Public function fn_CheckTreeLinkExist (byval objPage,byval ElementId ,byval sName ,byval nWaitTime )
rem store the all given node name in Array  seprated by comma using Split method
sGaahire =split(sName,”,”)
rem Run the Loop for  by counting  all given node name in Array
For i=0 to ubound(sGaahire)
rem call  function  fn_ExpandTree for  check Node name Exist or not  if Exist  and expandable  then expand the node
blCheck =   fn_ExpandTree( objPage, ElementId , trim(sGaahire(i)),nWaitTime)
rem return value to Function
fn_CheckTreeLinkExist =blCheck

Next
Rem  Clear All  the Refrences to the Objects
Set  objPage = nothing
Set  objDiv = nothing
Set  sMatchName = nothing
Set objchildNodes =nothing
Set sMatchName =nothing
Set objTD =nothing
Set objIMG =nothing

End Function

REM *************************************************************************

REM *************************************************************************
REM  Function  Check  given  check Node name Exist or not  if Exist  and expandable  then expand the node
REM  Input    objPage          := Paranet object  eg. webpage
REM  Input    ElementId          :=  Tree Control Web Element ID  (html id  value)   e.g  “ctl00_contRMSMaster_ucConnectionBasicInfo_trvwPopulateTree”
REM  Input    sName          := Name of the node  But alway s give  the value in heriachy manner  by seprated comma  like “Utility,India,Hayana”
rem if  your Tree Stucture is Like This  Utility–>India–>Hayana or  Utility–>India–>Delhi
rem for Check   India give the value  like This  sName =”Utility,India”
rem for Check  Hayana  give the value  like This  sName =”Utility,India,Hayana”   not  alone Hayana always give the value from Root node in herarchy manner
REM  Input    nWaitTime          := time ( second)  for expand the tree control because On expand page get refresh  if  your page Get refresh on expand give the value as per refresh Time  Or If Page does not get refresh then give the minimum value
REM  Output(ReturnType) := True OR False  if node exist return True  Or False
REM  used functions are   fn_GetDIVid( objPage,ElementId,sName)
REM  Created:    15/Mrach/2009    Rajiv Kumar Nandvani ## Changed:MM/DD/YYYY
REM  Example Call  fn_ExpandTree (objPageRMS ,”ctl00_contRMSMaster_ucConnectionBasicInfo_trvwPopulateTree”,”Utility,India” ,5 )

REM *************************************************************************

Function fn_ExpandTree( byval objPage,byref ElementId ,byval sName ,byval nWaitTime)
rem  set  Div Element
Set objDiv = objPage.Object.getElementById(ElementId)
rem  Set childobject  under  Div Element object
Set objchildNodes =objDiv.children
rem Loop upto   child node  count
For a =0 to objchildNodes.length-1
rem  get  childnode Tagname
sTagName=objchildNodes(a).tagName
If sTagName=”TABLE”  Then
rem set  object  TR Element  under  child node
Set sMatchName =objchildNodes(a).getElementsByTagName(“TR”)
rem Loop upto   sMatchName   count
For p=0 to sMatchName.length-1
rem  check element  innertext  equal to given node name then set  blncheck =1
If sMatchName(p).innerText = sName  Then
blncheck =1
rem set  object  TD  Element  under  element  sMatchName
set objTD = sMatchName(p).getElementsByTagName(“TD”)
If  objTD.length >1 Then
rem set  object  IMG  Element  under  element  sMatchName
set objIMG =  objchildNodes(a).getElementsByTagName(“IMG”)
rem Loop upto   IMG  Element     count
For s =0 to objIMG.length-1
rem get Alt property  value ( tool tip)
sExpand = objIMG(s).alt
rem if node alt proerty value  Expand then Expand the Node
If InStr(1, sExpand, “Expand”, 1) > 0  Then

objIMG(s).parentElement.click
wait(nWaitTime)
rem call function for Next node Div element ID
Call  fn_GetDIVid( objPage,ElementId,sName)
Exit For
End If
If InStr(1, sExpand, “Collapse”, 1) > 0  Then
rem call function for Next node Div element ID
Call  fn_GetDIVid( objPage,ElementId,sName)
Exit For
End If
Next
End If
End If
If blncheck =1 Then
fn_ExpandTree =True
Exit for
else
fn_ExpandTree =False
End If
Next

End If
if   blncheck =1 Then
blncheck=0
Exit for
End If

Next

End Function

Function fn_GetDIVid( byval objPage,byref ElementId ,byval sName)
Set objDiv = objPage.Object.getElementById(ElementId)
Set objchildNodes =objDiv.children
set ObDivLink = objchildNodes
For a =0 to objchildNodes.length-1
sTagName=objchildNodes(a).tagName
If sTagName=”TABLE”  Then
Set sMatchName =objchildNodes(a).getElementsByTagName(“TR”)
For p=0 to sMatchName.length-1
If sMatchName(p).innerText = sName  Then
blncheck =1
Exit for
End If
next
elseif  sTagName=”DIV” and  blncheck =1 Then
ElementId =objchildNodes(a).id
blncheck=0
Exit for
End If

Next
End Function

Function fn_clickTreelink(  byval objPage,byval ElementId ,byval sName)
If objPage.Object.getElementById(ElementId).children.length=1  Then
Set objDiv = objPage.Object.getElementById(ElementId).parentElement
else
Set objDiv = objPage.Object.getElementById(ElementId)
End If
Set objchildNodes =objDiv.children

For a =0 to objchildNodes.length-1
sTagName=objchildNodes(a).tagName
If sTagName=”TABLE”  Then
Set sMatchName =objchildNodes(a).getElementsByTagName(“TR”)
For p=0 to sMatchName.length-1
If sMatchName(p).innerText = sName  Then
set objTD =   sMatchName(p).getElementsByTagName(“TD”)
If  objTD.length >1 Then
set objLink =  sMatchName(p).getElementsByTagName(“A”)
For s =0 to objLink.length-1
sExpand = objLink(s).innerText
If   sExpand= sName Then
objLink(s).click
nExitFlag =1
Exit for
End If
Next
End If
End If
If nExitFlag =1 Then
Exit for
End If
next

End If

If nExitFlag =1 Then
Exit for
End If
If sTagName=”DIV” Then
ElementId = objchildNodes(a).id
call  fn_clickLastlink(  objPage,ElementId ,sName)
Exit for
End If

Next

End Function

Function fn_clickLastlink(  byval objPage,byval ElementId ,byval sName)

Set objDiv = objPage.Object.getElementById(ElementId)
Set objchildNodes =objDiv.children

For a =0 to objchildNodes.length-1
sTagName=objchildNodes(a).tagName
If sTagName=”TABLE”  Then
Set sMatchName =objchildNodes(a).getElementsByTagName(“TR”)
For p=0 to sMatchName.length-1
If sMatchName(p).innerText = sName  Then
set objTD =   sMatchName(p).getElementsByTagName(“TD”)
If  objTD.length >1 Then
set objLink =  sMatchName(p).getElementsByTagName(“A”)
For s =0 to objLink.length-1
sExpand = objLink(s).innerText
If   sExpand= sName Then
objLink(s).click
nExitFlag =1
Exit for
End If
Next
End If
End If
If nExitFlag =1 Then
Exit for
End If
next

End If

If nExitFlag =1 Then
Exit for
End If
Next

End Function

Advertisements

8 Responses to “WEB Tree Control VB script QTP”

  1. ChasL said

    hi, thanks for the code sample. tried it in QTP 11, the following line errors out:

    Set objchildNodes =objDiv.children

  2. Hi,

    Thanks for reading the blog. it seems under objDiv there is no child items, check through debug. or you can do first check the child count then create the children object if child count greater than 0

    Thanks & best of luck

  3. Manika said

    Hi Rajiv,
    I am new to QTP. I am facing an issue in my project. I need to automate the VB script for flex based objects. One object is Data driven Tree control. I am not able to expand the tree as “expand” is not provided by QTP. So can I use above portion to expand it and can take all the objects?

    Manika

  4. Hi manika,

    No it will not work for flex based object. For this you have to add QTP flex addin and your application flex control must be accessible.

    Thanks

  5. Manika said

    Thanks for your response Rajiv !

    Actually I already have all flex plug-ins with QTP 10.0.
    I have a combo box with objects that can be expanded in the tree view. I cannot click and expand the object as it is not being recognized by QTP with that Expand()method is not available in QTP suggession list.
    Can you please suggest any command/method to expand the flex tree object?

    Manika

  6. Hi manika,

    First you have to check with object spy on that object and find out the which method/event is doing the expand operation you can check this with firebug OR IE developer tool bar as well . OR you can ask to developer which method they are using to expand the flex tree object.

    Thanks & best of luck

    http://testeverythingrajiv.com

  7. Anonymous said

    Hey,

    Can i get little info what exactly this code for.. I was actually looking for the code to expand the tree by double click on each node so I can get to the leaf node.. thanks

  8. Preeti said

    Is there any way to identify our application used webtree using qtp 11 if you are not aware of respective addins. Please advice

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: