2

I have the following HTML tag, which QTP correctly identifies as a WebEdit object:

<input style="width: 228px;" aria-describedby="x-auto-0" _id="Tenant" name=""
tabindex="1" id="x-auto-23-input" class="x-form-field x-form-text x-form-invalid"
type="text">

How do I get the _id property from the HTML tag into an object property in QTP? I've used the Object Identification dialog to add both _id and html _id properties to the WebEdit class. However neither are filled in when I use either the Object Spy or the Recorder.

Note that the page being tested contains a number of these text inputs, each with a blank name but descriptive _id. I'm trying to get the _id into a property of WebEdit so I can refer to a particular text box by Browser("Browser").Page("Page"),WebEdit("_id:=Tenant").

Jim Pfleger
  • 301
  • 3
  • 7

4 Answers4

3

HTML attributes can be obtained by using the .Object.GetAttribute() function. This is particularly useful for obtaining non-standard attributes (i.e., "_id").

The attribute "id" aligns with the Runtime Object Property "html id", so that can be obtained using GetROProperty() or the above method.

An example of using these methods is below:

Dim objUI    
Set objUI = Browser("Browser").Page("Page").WebEdit("WebEdit")
Print objUI.GetROProperty("html id")
Print objUI.Object.GetAttribute("id")
Print objUI.Object.GetAttribute("_id")
Set objUI = Nothing

To use descriptive programming to access an object, you can use the attribute/ notation as well as regular expressions. For example:

Set objUI = Browser("Browser").Page("Page").WebEdit("attribute/_id:=Tenant", "html id:=x-auto-\d*-input")

By default, the .Object methods and properties are not exposed for web elements in the Debug Viewer. It is possible to enhance QTP debugging by registering the Process Debug Manager (PDM) which is included in IE8. This will help you discover additional properties and methods that are available in QTP by using .Object. For more information on enhancing debugging in QTP 11, please see the following article: http://northwaysolutions.com/blog/qtp-11-how-to-enable-enhanced-debugging-features/

BrianJM
  • 301
  • 1
  • 7
  • Thank you for the info Brian. I'll definitely make use of the enhanced debugging. However, I really want to get the _id into a searchable `WebEdit` property, and not just be able to use `GetAttribute`. (I've amended my question to clarify this.) – Jim Pfleger Mar 27 '13 at 21:17
  • @JimPfleger Thanks for clarifying. I have updated the answer to be more complete. – BrianJM Mar 28 '13 at 13:53
2

You can get objects with custom attributes (properties) with the attribute/customAttributeKey:=customAttributeValue identifier. In your case:

Set TenantWebEdit = Browser("Browser").Page("Page").WebEdit("attribute/_id:=Tenant")

This also allows you to put in more identifiers if you want to to make the description unique:

Set TenantWebEdit = Browser("Browser").Page("Page").WebEdit("attribute/_id:=Tenant", "class:=.*x-form-text.*")
AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • With QTP11 the `attribute/_id:=Tenant` syntax did not find any objects. Is there something more that I need to do for this to work? I know it's collecting the attributes because @BrianJM's `objUI.Object.GetAttribute("_id")` gives `Tenant`. – Jim Pfleger Mar 28 '13 at 14:19
  • 1
    @JimPfleger Do you have multiple objects with the attribute "_id" = "Tenant? If so, you will need to further define the descriptive programming to be unique (or use the ChildObjects() method with a description object). – BrianJM Mar 28 '13 at 19:55
  • No `_id` is a unique identifier. I don't know why they didn't just put it in `name`. – Jim Pfleger Mar 28 '13 at 23:45
  • This "attribute/" trick does not work for Java GUI components, right? – TheBlastOne Mar 04 '14 at 17:24
  • @TheBlastOne I suspect it is a feature for Web components, so it would not work for Java I think. – AutomatedChaos Mar 05 '14 at 09:48
0

I don't know if it's the right way to do this, but I was able to hack up what I needed by loading all of the WebEdit objects into a Dictionary keyed on their _id.

Dim WebEdit_desc
Set WebEdit_desc = Description.Create
WebEdit_desc("micClass").value = "WebEdit"

Dim WebEdits
Set WebEdits = CreateObject("Scripting.Dictionary")

Dim WebEdit_list
Set WebEdit_list = Browser("Browse Catalog").Page("Add Tenant").ChildObjects(WebEdit_desc)
Dim i
For i = 0 to WebEdit_list.Count() - 1
    If NOT IsNull(WebEdit_list(i).Object.GetAttribute("_id")) Then
        WebEdits.Add WebEdit_list(i).Object.GetAttribute("_id"), WebEdit_list(i)
    End If
Next
Set WebEdit_list = Nothing

WebEdits.Item("Tenant").Set DataTable("Tenant", dtLocalSheet)
Jim Pfleger
  • 301
  • 3
  • 7
0

To extract the lang property from Browser document

Msgbox browser("creationtime:=0").Page("index:=0").GetROProperty("attribute/lang")

Suresh Karia
  • 17,550
  • 18
  • 67
  • 85
Arvind
  • 36
  • 4
  • 1
    can you explain your answer? – Suresh Karia Dec 14 '15 at 07:58
  • @eirenaios this uses the dom property It checks in the object dom,and returns the lang property value from the dom, similarly you can use .GetroProperty("attribute/id") or .GetroProperty("attribute/UniqueID") to get unique id of the object – Arvind Jan 19 '17 at 07:50
  • 1
    It extracts the lang property from Browser document element..Browser("creationtime:=0").Page("index:=0").Object.documentElement.lang – Arvind Jan 19 '17 at 08:10
  • Pleas edit the question and add this explanation into it so users can understand it better. – Suresh Karia Jan 19 '17 at 19:30