基于Excel的TestComplete关键字框架
TestComplete在TC7之后逐渐支持关键字脚本开发,但是在之前的版本是没有关键字视图的。
用Excel存储关键字是很多关键字框架流行的做法,在TestComplete中也可以实现。
首先在Excel中存储关键字:
Object_Name |
Object_Reference |
Parent_Object |
Description |
Notepad |
Sys.Process("Notepad") |
|
Notepad Application |
Editor |
Window("Notepad", "*", 1).Window("Edit", "", 1) |
Notepad |
Editor for Notepad |
ChangedDialog |
Window("#32770", "Notepad", 1) |
Notepad |
Do you want to save the changes dialog |
NoButton |
Window("Button", "&No", 2) |
ChangedDialog |
No Button on the save changes dialog |
然后在TC脚本中编写关键字驱动脚本,读取Excel数据。
下面是完整的框架脚本(VBScript):
Dim ObjectMapStorage
Class ObjectMap
Private m_TestObject
Private m_ObjectReference
Private m_ParentObject
Private m_ObjectInfo
Public Property Get TestObject
TestObject = m_TestObject
End Property
Public Property Get ObjectReference
ObjectReference = m_ObjectReference
End Property
Public Property Get ParentObject
ParentObject = m_ParentObject
End Property
Public Property Get ObjectInfo
ObjectInfo = m_ObjectInfo
End Property
Public Property Let TestObject(Value)
if (Value <> m_TestObject) Then
if ObjectMapStorage.Exists(m_TestObject) Then
ObjectMapStorage.Remove(m_TestObject)
end if
if ObjectMapStorage.Exists(Value) Then
ObjectMapStorage.Remove(Value)
end if
m_TestObject = Value
Call ObjectMapStorage.Add(m_TestObject,me)
End if
End Property
Public Property Let ObjectReference(Value)
m_ObjectReference = Value
End Property
Public Property Let ParentObject(Value)
m_ParentObject = Value
End Property
Public Property Let ObjectInfo(Value)
m_ObjectInfo = Value
End Property
Function GetTestCompleteObject
Dim S
if (VarType(ParentObject) = varNull) Then
S = ObjectReference
Else
If not ObjectMapStorage.Exists(ParentObject) then
Log.Error("Unable to find parent [" & ParentObject & "] of object [" & TestObject & "]")
Set GetTestCompleteObject = Utils.CreateStubObject
Exit Function
End If
Dim ParentObjMapItem
Set ParentObjMapItem = ObjectMapStorage.Item(ParentObject)
Dim ParentObj
Set ParentObj = ParentObjMapItem.GetTestCompleteObject
S = ParentObj.FullName + "." + ObjectReference
end if
Set GetTestCompleteObject = Eval(S)
End Function
End Class
Function GetTestCompleteObject(ObjectName)
if ObjectMapStorage.Exists(ObjectName) Then
Dim Obj
Set Obj = ObjectMapStorage.Item(ObjectName)
Set GetTestCompleteObject = Obj.GetTestCompleteObject
Else
Log.Error("Object Does not exist [" & ObjectName & "]")
Set GetTestCompleteObject = Utils.CreateStubObject
End if
End Function
Sub ReadObjectMap(ObjectMapExcelFile, Sheet)
Set ObjectMapStorage = Sys.OleObject("scripting.dictionary")
Set DDTDriver = DDT.ExcelDriver(ObjectMapExcelFile, Sheet)
While not DDTDriver.EOF
Dim Obj
Set Obj = new ObjectMap
Obj.TestObject = DDTDriver.Value("Object_Name")
Obj.ObjectReference = DDTDriver.Value("Object_Reference")
Obj.ParentObject = DDTDriver.Value("Parent_Object")
Obj.ObjectInfo = DDTDriver.Value("Description")
DDTDriver.Next
WEND
End Sub
Sub TestObjectMap
Set ObjectMapStorage = Sys.OleObject("scripting.dictionary")
Dim Obj
Set Obj = new ObjectMap
Obj.ObjectReference = "Sys.Process(""Notepad"")"
Obj.TestObject = "Notepad"
Set Obj1 = new ObjectMap
Obj1.ObjectReference = "Window(""Notepad"", ""*"", 1)"
Obj1.TestObject = "Notepad_Main_Window"
Obj1.ParentObject = "Notepad"
Dim ObjChild
Set ObjChild = new ObjectMap
ObjChild.ObjectReference = "Window(""Edit"")"
ObjChild.TestObject = "Notepad_Edit"
ObjChild.ParentObject = "Notepad_Main_Window"
Set Temp = ObjChild.GetTestCompleteObject
End Sub
Sub TestReadObjectMap
Call ReadObjectMap(Files.FileNameByName("ObjectMap.xls"),"Sheet1")
GetTestCompleteObject("Editor").Keys("TestComplete")
GetTestCompleteObject("Dummy")
End Sub
参考:
《Simple Keyword Framework with TestComplete - Part 1 - The object map》
http://blog.csdn.net/Testing_is_believing/archive/2008/06/27/2593103.aspx