Oct
05
2008
--

Identifying controls using labels - Approach 1

Identifying objects correctly and robustly in an application is very important in Automation. In case you have automated any CRM application you might have noticed objects are recognized by a dynamic name

Browser("Browser").Page("Page").WebEdit("TS_EXTRA_FEILD40").Set "AAA"

Now after a build the code or object might change to

Browser("Browser").Page("Page").WebEdit("TS_EXTRA_FEILD42").Set "AAA"

These object changes are unpredictable and cause scripts to fail. Let’s assume the field name to be ‘Product Name:’ in the CRM application. Digging into the HTML code we can see the HTML code specific to the product code label and text box would look like below

<LABEL for="TS_EXTRA_FEILD40">Product Name:</LABEL>
<INPUT id="TS_EXTRA_FEILD40" name="TS_EXTRA_FEILD40" >

The HTML preview of the same is shown below

Product Name:

Using the general way of recording will produce a high maintenance effort scripts, as the object repository would need to be updated every time. Code below demonstrates how to get object using it’s label and not the actual HTML name

'----------------------------------------------------------------
'Author: Tarun Lalwani
'Website: http://KnowledgeInbox.com
'Paremeters -
'@ParentObject - The parent object for which the control needs
'            to be retrieved
'@LabelName - Label for which you need to get the control
'@MicClass - Type of object (WebEdit, WebList etc...)
'----------------------------------------------------------------
Function ObjFromLabel(ByVal ParentObject, ByVal LabelName, ByVal MicClass)
   'Return Nothing in case of issue
  Set ObjFromLabel = Nothing
 
   Dim objLabel
   Set objLabel = ParentObject.WebElement("html tag:=LABEL","outertext:=" & LabelName)
 
    'Check if the label object exists
   If objLabel.Exist(0) Then
      Dim controlHTMLid
 
      'get the property specified in FOR atrribute of the tag
      'Test. Below works for IE
      controlHTMLid = objLabel.GetROProperty("attribute/htmlfor")
 
      'In case of firefox
      if controlHTMLid ="" then controlHTMLid = objLabel.GetROProperty("attribute/for")
 
      If controlHTMLid <> "" Then
         Execute "Set ObjFromLabel = ParentObject." & MicClass & "(""html id:=" & controlHTMLid & """)"
      End If
   End If
End Function
 
Dim oPg, oEdit
 
'The page object
Set oPg = Browser("Identifying controls using").Page("Identifying controls using")
 
'Get the webedit for 'Product Name:'
Set oEdit = ObjFromLabel(oPg ,"Product Name:","WebEdit")
 
'Set the value
oEdit.Set "Tarun Lalwani"

The approach discussed above uses descriptive programming to check for the label first and then gets the html id of the specific control mapped to it.

Rating: 9.3/10 (11 votes cast)