Sunday, 1 June 2014

QTP - UFT - How to handle multiple objects with same properties - example


Solution - Use Ordinal Identifiers

we can use the following ordinal identifiers depending on the object
1) Creation Time ( only for browsers)
2) Index
3) Location

here is a scenario that describes it. I have created two buttons with same properties and when you spy using object spy on the buttons object spy displays same object properties.

Here is the above snapshot html code ( Copy the code in notepad and save with extension html)

<html>
<Head> <title> Object Identification </title> </head>
<body>
<Center>
<br>When you use object Spy on both the objects the properties will be same.
<br>
<br><Input type=Button name="s1" value="Button1"></Input>
<br>
<br>
<br><Input type=Button name="s1" value="Button1"></Input>
</Center>
</body>
</html>

When you use object Spy the following is displayed

Object Spy - Properties of first button"Class Name:=WebButton",
"abs_x:=649",
"abs_y:=118",
"class:=",
"disabled:=0",
"height:=23",
"html id:=",
"html tag:=INPUT",
"innerhtml:=",
"innertext:=",
"name:=Button1",
"outerhtml:=<input name=""s1"" type=""Button"" value=""Button1"">",
"outertext:=",
"title:=",
"type:=button",
"value:=Button1",
"visible:=True",
"width:=68",
"x:=649",
"y:=63"

Object Spy - Properties of second button
"Class Name:=WebButton",
"abs_x:=649",
"abs_y:=178",
"class:=",
"disabled:=0",
"height:=23",
"html id:=",
"html tag:=INPUT",
"innerhtml:=",
"innertext:=",
"name:=Button1",
"outerhtml:=<input name=""s1"" type=""Button"" value=""Button1"">",
"outertext:=",
"title:=",
"type:=button",
"value:=Button1",
"visible:=True",
"width:=68",
"x:=649",
"y:=123"

If you ignore the "y" and "abs_y" property, rest of the properties are unique.

If you add these properties to Object Repository, UFT/QTP adds the objects to OR with ordinal identifiers as Index.

 
 

'when recording
Browser("Object Identification").Page("Object Identification").WebButton("Button1").Click
Browser("Object Identification").Page("Object Identification").WebButton("Button1_2").Click

' when using descriptive programming
Browser("name:=Object Identification").page("title:=Object Identification").webbutton("name:=Button1","Index:=0").Click
Browser("name:=Object Identification").page("title:=Object Identification").webbutton("name:=Button1","Index:=1").Click

 

9 comments:

  1. Thank you. This is exactly what I was looking for. Good post!!!

    ReplyDelete
  2. instead of button, if it is a link??

    ReplyDelete
    Replies
    1. 'when using descriptive programming
      Browser("name:=Object Identification").page("title:=Object Identification").Link("name:=Link1","Index:=0").Click
      Browser("name:=Object Identification").page("title:=Object Identification").Link("name:=Link1","Index:=1").Click

      Delete
  3. Depending on the names we add the number of indexes changes.The names are passed via excel sheet. for eg html_id value is individual[1].name. How do you find the index values for this on run time.

    ReplyDelete
  4. I am not able to identify the second web button at all even thru OR or DP

    ReplyDelete
  5. Au lieu du bouton, s'il s'agit d'une image (y'a pas d'index dans mon image :/ )?

    ReplyDelete
  6. Its giving me error.. even if i use index.. currently using descriptive programming. Is there any other way to recognise 2 similar object? Reply asap

    ReplyDelete