An object model is a collection of objects or classes through which a program can look at and manipulate it to some extent. The objects can be accessed via object references. To invoke a method in an object, the object reference and method name are given, together with any arguments.
Component Object Model in UFT
The Component Object Model in UFT provides a standard way for applications (.exe files) or libraries(.dll files) to make their functionality available to any COM-compliant application or script. For example, consider Microsoft Word Application. You can open a Word document and write any text, create tables and chars, and do many other things. Now, what if you want to do all these tasks through code ( i.e. without manually opening Word document). This is the situation where you would need a Component Object Model in UFT.
To create a Component Object CreateObject method is used.
CreateObject Function: Creates and returns a reference to an ActiveX object.
Syntax is CreateObject(class,[servername])
Part | Description |
class | Required; Variant ( String ). The application name and class of the object to create. |
servername | Optional; Variant ( String ). The name of the network server where the object will be created. If servername is an empty string (“”), the local machine is used. |
The class argument uses the syntax appname.objecttype and has these parts:
Part | Description |
appname | Required; Variant ( String ). The name of the application providing the object. |
objecttype | Required; Variant ( String ). The type or class of object to create. |
Example – Component Object Model in UFT
Set ObjWord = CreateObject (“Word.Document”)
ObjWord.Content = “Hello”
ObjWord.SaveAs “Doc1.docx”
ObjWord.Close
Set ObjWord = Nothing
In this example appname is “Word” and objecttype is “Document”
COM Servers and COM Clients
- Objects that make their functionality available through COM are known as COM Servers.
- Application or scripts that make use if that functionality is referred to as COM clients.
For example, when you write a script that uses WORD, WORD is the COM server and your script is the COM CLIENT
COM Servers Implementation
COM servers can be implemented in one of two ways:
Out-of-process servers: Out-of-process servers are typically implemented in executable files and run in a different process than the script.
For example, when you start a script for creating a Word Object, an instance of Wscipt.exe begins to run. If you next instantiate a Microsoft Word object, you are then working with two processes, Wscipt.exe and Winword.exe
In-process servers: Libraries(.dll files) are known as in-process-servers because they run in the same process as the application or script that called them.
For example, when you call the FileSystemObject from within a script, no new process is created. This is because the FileSystemObject (which is found in the Scrunn.dll) is an in-process server and thus runs in the same process as the script.
In-process servers typically run faster than Out-of-process servers because the operating system does not have to cross boundaries (from the script process to the object process and then back) to access the object methods and properties.
Nothing Keyword: VBScript includes the Nothing keyword, which can be used to disassociate an object reference and an object. After an object variable is set to Nothing, the variable no longer maintains an object reference and thus can not be used to control the object.
Document Object Model in UFT
A Web Page consists of different objects(like textbox, images, web tables, forms, buttons, etc) is organized in a tree structure. The Document Object Model is an API that allows programs to access and update the content, structure, and style of documents.
These objects can be accessed by using scripts for web pages. The Document Object Model is an interface(API) that allows programs and scripts to access and update the content, structure, and style of documents. The DOM applies to HTML as well XML. The page source of a web page can be accessed by using .object notation.
How To Access Document Object Model in UFT?
- .Object.all.tags(“tagname”)
- .Object.getElementsByTagName(“tagname”)
- .Object.getElementById(“html id”)
- .Object.getElementsByName(“name”)
- .Object.links
- .Object.images
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Set ObjInput=Browser("Browser").Page("Page").Object.all.tags("input") Print ObjInput.Length For i = 0 To ObjInput.Length-1 If ObjInput(i).name = "userName" Then ObjInput(i).value="test1234" End If Next |
Example 2: To retrieve all hyperlinks in a page and click on a particular link using .Object.links
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Set ObjLink=Browser("Browser").Page("Page").Object.links Print ObjLink.Length For i = 0 To ObjLink.Length-1 print ObjLink.item(i).innertext If (ObjLink.item(i).innertext)="REGISTER" Then ObjLink.item(i).Click End If Next |
Example 3: Find all objects with input tags and set a text if the name property is userName using .Object.getElementsByTagName(“tagname”)
1 2 3 4 5 6 7 8 9 10 11 |
Set ObjInput=Browser("Browser").Page("Page").Object.getElementsByTagName("input") For i = 0 To ObjInput.Length-1 If ObjInput(i).name = "userName" Then ObjInput(i).value="ABCD123456" End If Next |
Test Object Model in UFT
Each test object class has several properties that can uniquely identify the objects of the particular class and a set of methods to perform specific actions.
- Test Object
- Run-time object.
A run-time object is an actual object in the test application on which methods are performed during the run session.GetROProperty is used to retrieve the actual run time property of an object.
Browser Object Model in UFT
1 |
Browser("Browser").Page("Page").RunScript("alert('Hello World');") |
Setting a value in a text box using a combination of DOM and BOM.
1 2 3 4 5 |
Set objTextBox = Browser("Browser").Page("Page").RunScript("document.getElementsByName('userName');") print objTextBox.length objTextBox(0).value = "TestUser123" |
Setting a value in a text box using a getElementsByName if the index of the object is known. In the following example setting value in a text box that is at 0th index.
1 2 3 |
Set objTextBox = Browser("Browser").Page("Page").RunScript("document.getElementsByName('userName')(0);") objTextBox.value = "TestUser456" |
Recommended Posts
- Automation Object Model in UFT One (AOM)
- GetRoProperty , SetToProperty & GetToProperty in UFT With Example
- Understanding Object Spy in UFT
- Reporter Object in UFT | Important Aspects
- Transactions in UFT with Example
- How to Use Data Driver for Automated Parameterization in UFT
- How to Use RegisterUserFunc in UFT with Examples
- How to Use Function Definition Generator in UFT
- How To Use Dictionary Object in UFT With Examples