QTP - 19 [HTML DOM] QTP与HTML DOM交互

19 HTML DOM

Concept: Document Object Model for HTML document.(Betterperformance then Test Object, DP)

               The QTPpage Test Object’s “Object” property provides access to the HTML DOM:

               e.g. Set oDocument =Browser(“”).Page(“”).Object

19.1 When use the HTML DOM

               Canaccess object property but QTP not provide. E.g. individual radio buttonelement;

19.2 HTML DOM Objects

--the Document Object: represent entire document and it’s root node.

      

--the HTML Element Object: refer to any particular node in the DOM.

--the HTML Element Collection: a collection of one or more elements.

 HTML:             

<input type="text" value="" class="input">

<input type="text" value="" class="input">

QTP code:

Set txt_Boxes = document.getElementsByName("txt_Name")

For i = 0 to txt_Boxes.Length - 1

               txt_Boxes.item(i).value = "Tarun"

               'Or

               txt_Boxer(i).value =  "Tarun"

Next

 

19.3 How to get Document object:

'Method 1

Set Document = Browser("").Page("").Object

'Method 2

Set Document = Browser("").Object.document

'Method 3

Set Document = Browser("").Page("").Frame("").Object.document

 

19.3 How to get web page Element using the DOM:

HTML:             

<input name=”txt_Name” id=”firstname” type="text" value="Tarun" class="input">

QTP code:

Set txt_Elem = Document.getElementById("firstname")

Set txt_Elem = Document.getElementsByTagName("INPUT").item(0)

Set txt_Elem = Document.getElementsByTagName("INPUT").item("txt_Name")

Set txt_Elem = Document.getElementsByTagName("INPUT").item("firstname")

Set txt_Elem = Document.getElementsByName("txt_Name").item(0)

 

19.4 Other HTML Elements

--Link/Button

Document.Link(“name”).Click

Document.Link(“id”).Click

Document.getElementById("id").Click

Document.getElementsByName("name")(0).Click

--Text Box

Document.getElementById("id").Value

Document.getElementsByName("name")(0). Value

‘Or get all text box collection

Set allElems = document.getElementsByTagName("Input")

For Each elem in allElems

               elem.value = "Tarun"

Next

--Combo or ListBox (所有的选项是个整体)

'HTML:

'<Option value="Actual value1">Displayed Value1 </option>

Set objCombo = Document.getElementsByName("name")(0)

'Get options number of the list

OptionLen = objCombo.Options.Length

'get one of option value

firstOptionVal = objCombo.Options(0).value          'return Actual value1

firstOptionVal = objCombo.Options(0).text                            'return Displayed Value1

'select one option

objCombo.Options(0).Selected = True

'Or

objCombo.value = "Actual value1"

--Checkbox

Document.getElementsByName("name")(0).Checked = Ture

--Radio button(所有的选项都是个体)

‘HTML

<input type="radio" name="Sex" value="male" checked="checked"/>

<input type="radio" name="Sex" value="female" />

‘QTP Code:

Set Document = Browser("Browser").Object.Document

Set objRadio = Document.getElementsByName("Sex").item(1)    ‘item(0) will be male

objRadio.checked =true

'or

'objRadio.value = "female"

--HTML TABLE

      *Cells provide the access to all the cells in table;

      * Rows provide the rows in table. Rows also provide a “cell” collection to access a specific cell present in a row.

Set Document = Browser("Browser").Object.Document

Set objTable = Document.getElementById("myTable")

'Get value of rows(0).cells(0): 1st row and 1st column cell

print objTable.cells(0).outerText      

'Get value of 2nd rows and 1st column cell

print objTable.rows(1).cells(0).outerText  

'rows of the table

print  objTable.rows.length

'get total cells of the table

print objTable.cells.length

'get total cells of spcial row

print objTable.rows(0).cells.length

 

19.5 Converting a DOM object to QTP Test Object

The HTML DOM sourceIndex property is the index value ofobject in DOM tree and can be used to get its corresponding QTP test object.

‘Use DOM to get index value

Set oText = Document.getElementById("myTable")

srcIndex = oText.sourceIndex

 

‘Code with index value and object type

Set Document = Browser("Browser").Object.Document

Set oText = Document.getElementById("firstname")

srcIndex = oText.sourceIndex

Browser("Browser").Page("Page").WebEdit("source_Index:=" & srcIndex ).Set "frank"

‘Or

Browser("Browser").Page("Page").WebEdit("sourceIndex:=" & srcIndex ).Set "frank"

 

 

19.6 Checking appearance using Style sheet:

Access the style sheet using the currentStyle and Styleobjects of every element.

Set oWebElement = Browser("Browser").Page("Page").WebElement("html id:=checkHidden").Object

print  oWebElement.className

Set oWebElement = Browser("Browser").Page("Page").WebElement("html id:=checkHidden").Object

print  oWebElement.currentStyle.dispay = "value"

Note: for more currentStyle object and related method, ref msdn.microsoft.com

19.7 Selecting a Radio button by Text

Radio button in HTML are only associated with valueproperties and not with the text appearing with them.

思路:UsingDOM getAdjacentText method. 不做研究,需要时再说。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值