QTP & DOM (转)

上一篇 / 下一篇  2011-05-13 17:04:48 / 个人分类:QTP

要想用QTP做好WEB自动化测试,需要熟悉DOM 。

通过 DOM ( Document Object Model ,文档对象模型),可以操纵页面的每个 HTML 元素。

 

常用 DOM 属性和方法
Every HTML element in a Web page is a scriptable object in the object model, with its own set of properties, methods, and events. To enable access to these objects, Internet Explorer creates a top-level document object for each HTML document it displays. When you use the .Object property on a Web page object in your test or component, you actually get a reference to this DOM object. This document object represents the entire page. From this document object, you can access the rest of the object hierarchy by using properties and collections.

Following are the most useful document properties and methods available through the Web .Object property: (通过.Object 可以访问以下属性、集合和方法)

Properties 属性
·   activeElement Property - Retrieves the object that has the focus when the parent document has focus.

·   cookie Property - Sets or retrieves the string value of a cookie.

·   documentElement Property - Retrieves a reference to the root node of the document.

·   readyState Property - Retrieves a value that indicates the current state of the object.

·   URL Property - Sets or retrieves the URL for the current document.

·   URLUnencoded Property - Retrieves the URL for the document, stripped of any character encoding.

Collections 集合
·   all - Returns a reference to the collection of elements contained by the object.

·   frames - Retrieves a collection of all window objects defined by the given document or defined by the document associated with the given window.

·   images - Retrieves a collection, in source order, of img objects in the document.

·   links - Retrieves a collection of all objects that specify the HREF property and all area objects in the document.

Methods 方法
·   getElementById Method - Returns a reference to the first object with the specified value of the ID attribute.

·   getElementsByName Method - Retrieves a collection of objects based on the value of the NAME attribute.

·   getElementsByTagName Method - Retrieves a collection of objects based on the specified element name.

Note that some of these properties are also provided by QuickTest Test Objects. For example, it is possible to access the cookies set by a Web page both through the cookie property in the DOM, and through the GetCookies method provided by the Browser Test Object.

 

activeElement
Retrieves the object that has the focus when the parent document has focus.
You can get the source index of the active element (object that has focus) on the page by doing the following:

CurrentSourceInd = Browser("Browser"). Page("Page").Object.activeElement.sourceIndex

Now you can query the source index of any object on the page by doing the following:

SrcIdx = Browser("Browser").Page("Page").WebEdit("q").GetROProperty("source_index")

You can then compare the two values to see if the WebEdit is the active element on the page that has focus. If the source index of the WebEdit is the same as the source index of the activeElement , then the WebEdit is the object that has focus.

 

activeElement 代表当前获得焦点的对象

 

例子:

' 当前获得焦点的页面元素的 sourceIndex

CurrentSourceInd = Browser("Web Tours").Page("Web Tours").Object.activeElement.sourceIndex

Print CurrentSourceInd

 

' 获取 username 输入框的 sourceIndex 属性

SrcIdx = Browser("Web Tours").Page("Web Tours").Frame("navbar").WebEdit("username").GetROProperty("source_index")

Print SrcIdx

 

If CurrentSourceInd<>SrcIdx Then

       Reporter.ReportEvent micFail," 默认焦点错误 "," 默认焦点未置于 UserName 输入框上! "

End If

 

' 把焦点置于 username 输入框上

Browser("Web Tours").Page("Web Tours").Frame("navbar").WebEdit("username").Object.focus

SrcIdx = Browser("Web Tours").Page("Web Tours").Frame("navbar").WebEdit("username").GetROProperty("source_index")

Print SrcIdx

 

cookie
A cookie is a small piece of information stored by the browser that applies to a Web page. Each cookie can store up to 20 name=value; pairs called crumbs, and the cookie is always returned as a string of all the cookies that apply to the page. This means that you must parse the string returned to find the values of individual cookies.

The following is an example which retrieves the value of the cookie for a specified Web page.

strCookie = Browser("Browser").Page("Page").Object.cookie

Note: You can also use the Browser Test Object's GetCookies method to retrieve cookies for a specific URL. For example:

msgbox Browser("Yahoo").GetCookies("http://finance.yahoo.com")

 

使用 Object.cookie 来获取指定页面上的 Cookie

例子:

strCookie = Browser("Web Tours").Page("Web Tours").Object.cookie

Print strCookie

 

' 也可这样获取 Cookie

Print Browser("Web Tours").GetCookies("http://127.0.0.1:1080/WebTours/")

 

 

documentElement
The documentElement property is a reference to the root node of the document. It is read-only, and has no default value. The root node of a typical HTML document is the html object.

documentElement 属性指向DOM 的根节点,通常就是HTML 节点对象

This example uses the documentElement property to retrieve the innerHTML property of the entire document.

msgbox Browser("Browser").Page("Page").Object.documentElement.innerHTML

 

可以通过 Object.documentElement 访问 innerHTML 属性取得整个文档的 HTML 文本

例子:

Print Browser("Web Tours").Page("Web Tours").Object.documentElement.innerHTML

 

取到页面的 HTML 文本如下:

<HEAD><TITLE>Web Tours</TITLE></HEAD><FRAMESET border=1 frameSpacing=4 borderColor=#e0e7f1 rows=65,* frameBorder=0><FRAME. name=header marginWidth=2 marginHeight=2 src="header.html" noResize scrolling=no><FRAME. name=body marginWidth=2 marginHeight=2 src="welcome.pl?signOff=true" noResize></FRAMESET>

 

 

readyState
The readyState property is a string value a string value that indicates the current state of the object.

用readyState 属性来标识指定对象当前的状态,这个属性对于所有DOM 元素都适用。

Note that the readyState property is applicable to any DOM element. Both examples below are equally valid, and both will display the readyState of the object to which they belong

msgbox Browser("Browser").Page("Page").Object.readyState

and

msgbox Browser("Browser").Page("Page").Link("Link").Object.readyState

An object's state is initially set to uninitialized, and then to loading. When data loading is complete, the state of the link object passes through the loaded and interactive states to reach the complete state.

 

对象状态的一般变化过程:

uninitialized -> loading -> loaded -> interactive -> complete

 

 

URL
The URL property sets or retrieves the URL for the current document. This property is valid for Page and Frame. objects.

The following example displays the URL for the Web page that is currently open in the browser:

msgbox Browser("Browser").Page("Page").Object.URL

 

URL 属性对于 Page 和 Frame. 对象都适用

 

例子:

Print  Browser("Web Tours").Page("Web Tours").Object.URL

Print  Browser("Web Tours").Page("Web Tours").Frame("body").Object.URL

Print  Browser("Web Tours").Page("Web Tours").Frame("header").Object.URL

Print  Browser("Web Tours").Page("Web Tours").Frame("info").Object.URL

Print  Browser("Web Tours").Page("Web Tours").Frame("navbar").Object.URL

 

输出页面对象以及各个 Frame. 对象的 URL 属性:

http://127.0.0.1:1080/WebTours/

http://127.0.0.1:1080/WebTours/welcome.pl?signOff=true

http://127.0.0.1:1080/WebTours/header.html

http://127.0.0.1:1080/WebTours/home.html

http://127.0.0.1:1080/WebTours/nav.pl?in=home

 

 

URLUnencoded
The only difference between the URL property and the URLUnencoded property is that the URLUnencoded property strips the URL of any character codings (such as %20 for spaces, and so forth.)

For more information about URL character encoding issues, see RFC-1738 on Uniform. Resource Locators (URL) .

 

URL 与 URLUnencoded 的区别在于 URLUnencoded 属性去掉了 URL 中的格式编码,例如代表空格的 %20

 

all, frames, images, 和 links 集合
Return a reference to the collection of elements contained by the object.

The all collection contains all the elements in the specified object. 指定对象中的所有元素。

The frames property contains objects of type window (frame).

Frame. 类型的对象中的所有元素。

The images property contains objects of type of img (image).

Image 类型的对象中的所有元素。

The links collections contains objects of types HREF and area.

HREF 、area 类型的对象中的所有元素。

length property - Sets or retrieves the number of objects in a collection.
item method - Retrieves an object from the all collection or various other collections.
namedItem method - Retrieves an object or a collection from the specified collection.
There are additional methods available for accessing some of the collections. For example, the images collection provides the tags method, which can be used to retrieve a collection of objects with a specified tag name. For more information, see http://msdn.microsoft.com/en-us/library/ms535862(VS.85).aspx .

The following example loops through all the INPUT tags, and displays a message indicating the number of tags checked:

Set inputs = Browser("Browser").Page("Page").Object.all.tags("INPUT")
'Loop over all inputs in this collection and get the 'checked' value:
       For Each inputBtn In inputs
              If inputBtn.checked Then
                     checkedCount = checkedCount + 1
              End If
       Next

MsgBox checkedCount & " checkboxes are checked."

The following example displays a message indicating the number of frames in the specified Web page:

msgbox Browser("Yahoo").Page("Yahoo!").Object.frames.length

 

 

从 all 、 frames 、 images 、 links 等集合中可以方便地获取到同一类型的所有对象。

 

例子:

 

' 取得 Frame. 的个数

Print  Browser("Web Tours").Page("Web Tours").Object.frames.length

 

Browser("Web Tours").Page("Web Tours").Frame("navbar").WebEdit("username").Set "jojo"

Browser("Web Tours").Page("Web Tours").Frame("navbar").WebEdit("password").SetSecure "4b4735162198024e11994f954660"

Browser("Web Tours").Page("Web Tours").Frame("navbar").Image("Login").Click 55,14

Browser("Web Tours").Page("Web Tours").Frame("navbar").Image("Itinerary Button").Click

 

Browser("Web Tours").Page("Web Tours").Frame("info").WebCheckBox("1").Set "ON"

Browser("Web Tours").Page("Web Tours").Frame("info").WebCheckBox("2").Set "ON"

 

' 取得标签为 INPUT 的所有对象

Set inputs = Browser("Web Tours").Page("Web Tours").Frame("info").Object.all.tags("INPUT")

For Each inputBtn In inputs

       If inputBtn.checked Then

              checkedCount = checkedCount + 1

    End If

Next

Print checkedCount & " checkboxes are checked."

 

 

getElementById
The getElementById method returns a reference to the first object with the specified value of the ID attribute.
The following example sets the value of obj to the first element in the page that has an ID of "mark":

Set bj = Browser("Browser").Page("Yahoo!").Object.getElementByID("mark")

 

通过对象的 ID 来查找对象

 

例子:

Set bj = Browser("Google").Page("Google").Object.getElementById("tb")

msgbox obj.innerText

 

getElementsByName 和 getElementsByTagName
The getElementsByName and getElementsByTagName methods can be used just like getElementById except that they return a collection of objects (as opposed to getElementById , which returns only the first matching object it finds.)

For example, the following script. line will display a message indicating the number of images on the page - image objects have an "IMG" tag.

msgbox Browser("Yahoo").Page("Yahoo!").Object.getElementsByTagName "IMG").length

getElementById 只查找并返回第一个匹配的对象,而 getElementsByName 和 getElementsByTagName 则返回一个包含匹配对象的集合。

 

例:

set imags = Browser("Google").Page("Google").Object.getElementsByTagName("IMG")

For i=0 to imags.length -1

       Print imags(i).src

Next

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/testing_is_believing/archive/2010/01/08/5161739.aspx


TAG:

 

评分:0

我来说两句

Open Toolbar