1. 程式人生 > >QTP之web常用對象

QTP之web常用對象

idt char 依然 solid har 獲取 需要 技術 val

以下為QTP最應掌握的、最常用的功能(以下僅提供菜單入口,其他還有很多入口,但功能都是一樣的)

1、QTP上方菜單欄->Tools->Object Spy(對象探測器)----多個入口

功能:捕獲並查看對象的所有類別、屬性、及支持的方法

2、QTP上方菜單欄->Resours->Object Repository(對象庫)----多個入口

功能:編寫代碼之前對新對象的操作(添加、刪除、更新、高亮、復制、粘貼、輔助對象庫、導出對象庫等等)

3、QTP上方菜單欄->Resours->Object Repository Manager(對象庫管理)---只有這一個入口

功能:多人協作自動化開發組裝時或者維護代碼時對已有對象庫的管理操作(新建對象庫、打開已有對象庫、可用編輯(Enable Editing)、添加、刪除、更新、高亮、復制、粘貼、QC連接、管理庫屬性、對象庫對比、對象庫合並等等)

其中值得一提的是Object Repository Manager的兩個王牌級”輔助工具--對象庫對比和對象庫合並

QTP上方菜單欄->Resours->Object Repository Manager->Tools->Object Repository Comparison Tool(對象庫對比)

技術分享圖片

技術分享圖片

技術分享圖片

QTP上方菜單欄->Resours->Object Repository Manager->Tools->Object Repository Merge Tool(對象庫合並)

技術分享圖片

下邊簡單匯總下web的常用幾類對象:

Browser

Browser對象即瀏覽器對象,例如IE,FF,Chrome。Browser對象是所有web對象的父級對象,是金字塔的頂端,我在Description properties中對它也沒有什麽約束。使用Object Spy查看Browser對象

技術分享圖片 技術分享圖片

以看到瀏覽器的相關屬性,因為我現在測試的系統需要測試IE6的兼容性,大家看到我的IE版本還是6-_-! Operations列出了Browser對象可以使用的方法,下面介紹幾個常用的方法(先將Browser對象加入對象庫)。

1.SystemUtil.Run,打開瀏覽器,具體使用方式可以F1查看。

2.Sync,同步方法,意思是等待瀏覽器完全打開再進行下一步的操作。

3.Navigate,打開URL。

4.Close,關閉瀏覽器。

1

2

3

4

Systemutil.Run "iexplore.exe"

Browser("Browser").Sync

Browser("Browser").Navigate ("http://localhost/qtp/demo-login.php")

Browser("Browser").Close

tips:打開指定網址還可以用Systemutil.Run方法,上述代碼可以簡化為

1

2

Systemutil.Run "iexplore.exe","http://localhost/qtp/demo-login.php"

Browser("Browser").Close

Page

Page對象一般是Browser的子對象,每一個頁面就是一個Page對象,Page對象的Name值是HTML標簽中Title的值。Page對象的重要方法有Exist、Sync、Childobjects等。實際工作中對Browser和Page的操作很少,大多是一些打開關閉和同步的操作。

WebEdit

網頁中的輸入框可以被我識別為WebEdit對象,Operations裏依然有很多方法,大家可以自行查看。

技術分享圖片

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<html>

<head>

<title>web對象演示</title>

<meta http-equiv="Content-type" content="text/html" charset="utf-8">

<style>

.content{

width:260px;

height:30px;

}

.edit{

width:170px;

}

span{

width:70px;

}

</style>

</head>

<body>

<form action="" method="POST">

<div class="content">

<span>text</span><input type="text" name="web" class="edit">

</div>

<div class="content">

<span>password</span><input type="password" name="web" class="edit">

</div>

<div class="content">

<span>textarea</span><textarea rows="5" cols="20" class="edit"></textarea>

</div>

</form>

</body>

</html>

WebEdit中使用較多的set和GetROProperty方法,set用來設置輸入框中的值,GetROProperty獲取運行時對象的值。

1

2

3

4

5

6

7

8

Browser("web對象演示").Page("web對象演示").WebEdit("text").Set "111"

Browser("web對象演示").Page("web對象演示").WebEdit("password").Set "222"

Browser("web對象演示").Page("web對象演示").WebEdit("textarea").Set "333"

a=Browser("web對象演示").Page("web對象演示").WebEdit("text").GetROProperty("value")

b=Browser("web對象演示").Page("web對象演示").WebEdit("password").GetROProperty("value")

c=Browser("web對象演示").Page("web對象演示").WebEdit("textarea").GetROProperty("value")

msgbox "text="+a+";password="+b+";textarea="+c

運行後輸出如下

技術分享圖片

Link

Link是網頁中的鏈接,我們在剛才的網頁中加入如下代碼

1

2

3

<div class="content">

<span>link</span><a href="http://www.baidu.com">點這裏跳轉到百度</a>

</div>

技術分享圖片

對Link對象的操作主要有Click、CheckProperty。CheckProperty方法是檢查Link對象的url屬性是否正確,click就是點擊操作,運行下方代碼,頁面會自動跳轉至百度。

1

2

3

4

5

6

7

‘檢查url屬性是否正確

Browser("web對象演示").Page("web對象演示").Link("點這裏跳轉到百度")_

.CheckProperty "url","http://www.baidu.com/"

‘如果正確執行Click方法

If Reporter.RunStatus=Pass Then

Browser("web對象演示").Page("web對象演示").Link("點這裏跳轉到百度").Click

End If

WebButton

WebButton對象就是頁面中各式各樣的按鈕啦,主要操作就是Click:)

技術分享圖片

WebElement

WebElement主要是頁面中的div,span,p等標簽包涵的內容,一般用來驗證數據的正確性,例如上面例子中的span標簽。

技術分享圖片

1

2

3

4

If Browser("web對象演示").Page("web對象演示")_

.WebElement("password").GetROProperty("innerhtml")="password" then

msgbox "ok"

end if

WebList

我將頁面中的下拉框識別為WebList對象,在我們的演示網頁中加入以下代碼。

1

2

3

4

5

6

7

8

9

<div class="content">

<span>select</span>

<select>

<option value ="php">php</option>

<option value ="java">java</option>

<option value="vbs">vbs</option>

<option value="python">python</option>

</select>

</div>

技術分享圖片

註意Properties中的all items屬性,它包括了list中的所有選項,選擇WebList中的選項用的是Select方法,用法和set方法一樣。

WebRadioGroup && WebCheckBox

WebRadioGroup單選框對象,WebCheckBox復選框對象,加入如下代碼。

1

2

3

4

5

6

7

8

9

10

11

<div class="content">

<span>radio</span>

<input type="radio" name="sex" value="boy" checked="checked">男

<input type="radio" name="sex" value="girl">女

</div>

<div class="content">

<span>checkbox</span>

<input type="checkbox" name="swim" value="swim" id="swim">遊泳

<input type="checkbox" name="game" value="game" id="game">遊戲

<input type="checkbox" name="read" value="read" id="read">閱讀

</div>

技術分享圖片 技術分享圖片

選擇單選框和復選框的代碼如下

1

2

3

Browser("web對象演示").Page("web對象演示").WebRadioGroup("sex").Select "girl"

Browser("web對象演示").Page("web對象演示").WebCheckBox("read").Set "ON"

Browser("web對象演示").Page("web對象演示").WebCheckBox("swim").Set "ON"

WebTable

WebTable對象是網頁控件中的重點與難點,在網頁布局中table一般用於數據的展示,這也是我們測試的重點所在。老樣子,現在網頁中創建table控件,加入以下代碼。

1

2

3

4

5

<table>

<tr><td>text1</td><td>textarea1</td><td>sex1</td><td>hobby1</td></tr>

<tr><td>text2</td><td>textarea2</td><td>sex2</td><td>hobby2</td></tr>

<tr><td>text3</td><td>textarea3</td><td>sex3</td><td>hobby3</td></tr>

</table>

並在style標

1

2

3

4

5

6

7

8

9

10

11

12

table{

border: 1px solid black;

padding:0;

margin:0 auto;

border-collapse: collapse;

}

td{

border: 1px solid black;

font-size:12px;

padding: 3px 3px 3px 8px;

color: black;

}


技術分享圖片

從圖中可以看到,我將td中識別為WebElement對象,將td的父級元素即table識別為WebTable對象。WebTable對象的方法有很多,這裏列舉幾個常用的方法,參考下面的代碼。

1

2

3

4

5

6

7

8

9

10

11

12

‘獲取列數

col=Browser("web對象演示").Page("web對象演示").WebTable("table").ColumnCount(1)

‘獲取行數

row=Browser("web對象演示").Page("web對象演示").WebTable("table").RowCount

‘獲取指定單元格的值

info=Browser("web對象演示").Page("web對象演示").WebTable("table").GetCellData(1,1)

‘得到指定單元格內的測試對象的數目

Dim obj

obj=Browser("web對象演示").Page("web對象演示").WebTable("table").ChildItemCount(1,1,"WebEdit")

msgbox "列數="+CStr(col)+" 行數="+CStr(row)+" 第一行第一列="+info+_

"有"+CStr(obj)+"個WebEdit對象"

運行結果如下:

技術分享圖片

對於WebTable對象大部分情況下使用描述性編程,

QTP之web常用對象