QTP - GUI 物件操作



在指令碼執行過程中,QTP 與各種 GUI 物件進行互動。因此,瞭解關鍵 GUI 物件的基本方法非常重要,使用這些方法我們可以有效地進行操作。

文字框操作

以下是我們在執行時訪問文字框使用的方法:

  • Set - 幫助測試人員在文字框中設定值

  • Click - 點選文字框

  • SetSecure - 用於安全地設定密碼框中的文字

  • WaitProperty - 等待屬性值變為真

  • Exist - 檢查文字框是否存在

  • GetROProperty("text") - 獲取文字框的值

  • GetROProperty("Visible") - 如果可見,則返回布林值

示例

Browser("Math Calculator").Sync
Set Obj = Browser("Math Calculator").Page("SQR Calc").WebEdit("n")

'Clicks on the Text Box
Obj.Click

'Verify if the Object Exist - Returns Boolean value
a = obj.Exist
print a

'Set the value
obj.Set "10000" : wait(2)

'Get the Runtime Object Property - Value of the Text Box
val = obj.GetROProperty("value")
print val

'Get the Run Time Object Property - Visiblility - Returns Boolean Value
x = Obj.GetROProperty("visible")
print x

複選框操作

以下是一些可以使用的方法來操作複選框:

  • Set - 幫助測試人員將複選框值設定為“ON”或“OFF”

  • Click - 點選複選框。即使選中或取消選中,但使用者無法確定狀態

  • WaitProperty - 等待屬性值變為真

  • Exist - 檢查複選框是否存在

  • GetROProperty("name") - 獲取複選框的名稱

  • GetROProperty("Visible") - 如果可見,則返回布林值

示例

'To Check the Check Box
Set Obj = Browser("Calculator").Page("Gmail").WebCheckBox("PersistentCookie")
Obj.Set "ON"

'To UnCheck the Check Box
Obj.Set "OFF"

'Verifies the Existance of the Check box and returns Boolean Value
val = Obj.Exist
print val

'Fetches the Name of the CheckBox
a = Obj.GetROProperty("name")
print a

'Verifies the visible property and returns the boolean value.
x = Obj.GetROProperty("visible")
print x

單選按鈕操作

以下是一些可以使用的方法來操作單選按鈕:

  • Select(RadioButtonName) - 幫助測試人員將單選按鈕設定為“ON”

  • Click - 點選單選按鈕。即使選中或取消選中,但測試人員無法確定狀態

  • WaitProperty - 等待屬性值變為真

  • Exist - 檢查單選按鈕是否存在

  • GetROProperty("name") - 獲取單選按鈕的名稱

  • GetROProperty("Visible") - 如果可見,則返回布林值

示例

'Select the Radio Button by name "YES"
Set Obj = Browser("Calculator").Page("Forms").WebRadioGroup("group1")
Obj.Select("Yes")

'Verifies the Existance of the Radio Button and returns Boolean Value
val = Obj.Exist
print val

'Returns the Outerhtml of the Radio Button
txt = Obj.GetROProperty("outerhtml")
print text

'Returns the boolean value if Radio button is Visible.
vis = Obj.GetROProperty("visible")
print vis

組合框操作

以下是一些可以使用的方法來操作組合框:

  • Select(Value) - 幫助測試人員從組合框中選擇值

  • Click - 點選物件

  • WaitProperty - 等待屬性值變為真

  • Exist - 檢查組合框是否存在

  • GetROProperty("Text") - 獲取組合框中選定的值

  • GetROProperty("all items") - 返回組合框中的所有項

  • GetROProperty("items count") - 返回組合框中專案的數量

示例

'Get the List of all the Items from the ComboBox
Set ObjList = Browser("Math Calculator").Page("Statistics").WebList("class")
x = ObjList.GetROProperty("all items")
print x

'Get the Number of Items from the Combo Box
y = ObjList.GetROProperty("items count")
print y

'Get the text value of the Selected Item
z = ObjList.GetROProperty("text")
print z

按鈕操作

以下是一些可以使用的方法來操作按鈕:

  • Click - 點選按鈕

  • WaitProperty - 等待屬性值變為真

  • Exist - 檢查按鈕是否存在

  • GetROProperty("Name") - 獲取按鈕的名稱

  • GetROProperty("Disabled") - 如果啟用/停用,則返回布林值

示例

'To Perform a Click on the Button
Set obj_Button = Browser("Math Calculator").Page("SQR").WebButton("Calc")
obj_Button.Click

'To Perform a Middle Click on the Button
obj_Button.MiddleClick

'To check if the button is enabled or disabled.Returns Boolean Value
x = obj_Button.GetROProperty("disabled")
print x

'To fetch the Name of the Button
y = obj_Button.GetROProperty("name")
print y

Web 表格操作

在當今的基於 Web 的應用程式中,Web 表格變得非常普遍,測試人員需要了解 Web 表格的工作原理以及如何在 Web 表格上執行操作。本主題將幫助您有效地操作 Web 表格。

序號 語句和描述
1

if 語句

if 語句由一個布林表示式後跟一個或多個語句組成。

2

if...else 語句

if else 語句由一個布林表示式後跟一個或多個語句組成。如果條件為真,則執行if語句下的語句。如果條件為假,則執行指令碼的else部分。

3

if..elseif...else 語句

一個 if 語句後跟一個或多個Elseif語句,這些語句包含布林表示式,然後後跟一個可選的else語句,當所有條件都變為假時執行該語句。

4

巢狀 if 語句

一個 if 或elseif語句在另一個ifelseif語句中。

5

switch 語句

switch語句允許將變數針對列表中的值進行相等性測試。

  • html id - 如果表格具有 id 標籤,則最好使用此屬性。

  • innerText - 表格的標題。

  • sourceIndex - 獲取表格的源索引

  • ChildItemCount - 獲取指定行中存在的子項數

  • RowCount - 獲取表格中的行數

  • ColumnCount - 獲取表格中的列數

  • GetcellData - 根據列和行索引獲取單元格的值

示例

Browser("Tutorials Point").Sync
' WebTable 
Obj = Browser("Tutorials Point").Page("VBScript Decisions").WebTable("Statement")
' Fetch RowCount
x = Obj.RowCount
print x

' Fetch ColumnCount
y = Obj.ColumnCount(1)
print y

' Print the Cell Data of the Table
For i = 1 To x Step 1
   
   For j = 1 To y Step 1
      z = Obj.GetCellData(i,j)
      print "Row ID : " & i & " Column ID : " & j & " Value : " & z
   Next
Next

'Fetch the Child Item count of Type Link in a particular Cell
z = Obj.ChildItemCount(2,1,"Link")
print z
廣告