使用Object Repository Automation Objects and Methods操作对象库(转)

上一篇 / 下一篇  2009-05-06 13:39:22 / 个人分类:自动化测试QTP

文章转自(般若菠萝蜜的博客)

 

Dim RepositoryFrom,TOCollection
Set RepositoryFrom = CreateObject("Mercury.ObjectRepositoryUtil")
RepositoryFrom.Load "C:\Login.tsr"
Set TOCollection = RepositoryFrom.GetAllObjectsByClass("WinButton")
Msgbox TOCollection.Count

具体可参照help文档中关于ObjectRepositoryUtil的应用.

'The following example retrieves an object repository's objects and properties,

'looks for specific test objects using several methods, and copies a test object
'to another object repository.
DimImageObj, PageObj, RepositoryFrom, RepositoryTo
SetRepositoryFrom = CreateObject("Mercury.ObjectRepositoryUtil")
SetRepositoryTo = CreateObject("Mercury.ObjectRepositoryUtil")
RepositoryFrom.Load"C:\QuickTest\Tests\Flights.tsr"
RepositoryTo.Load"E:\Temp\Tests\Default.tsr"


 

FunctionEnumerateAllChildProperties(Root)
'The following function recursively enumerates all the test objects directly under
'a specified parent object. For each test object, a message box opens containing the
'test object's name, properties, and property values.
    DimTOCollection, TestObject, PropertiesCollection,Property, Msg
    SetTOCollection = RepositoryFrom.GetChildren(Root)
    Fori = 0ToTOCollection.Count - 1
            SetTestObject = TOCollection.Item(i)
            Msg = RepositoryFrom.GetLogicalName(TestObject) & vbNewLine
            SetPropertiesCollection = TestObject.GetTOProperties()
            Forn = 0ToPropertiesCollection.Count - 1
                SetProperty= PropertiesCollection.Item(n)
                Msg = Msg &Property.Name &"-"&Property.Value & vbNewLine
            Next
            MsgBox Msg
EnumerateAllChildProperties TestObject
    Next
EndFunction


 

FunctionEnumerateAllObjectsProperties(Root)
'The following function enumerates all the test objects under a specified object.
'For each test object, a message box opens containing the test object's name,
'properties, and property values.
 DimTOCollection, TestObject, PropertiesCollection,Property, Msg
    SetTOCollection = RepositoryFrom.GetAllObjects(Root)
    Fori = 0ToTOCollection.Count - 1
        SetTestObject = TOCollection.Item(i)
                    Msg = RepositoryFrom.GetLogicalName(TestObject) & vbNewLine
            SetPropertiesCollection = TestObject.GetTOProperties()
            Forn = 0ToPropertiesCollection.Count - 1
                SetProperty= PropertiesCollection.Item(n)
                Msg =Property.Name &"-"&Property.Value & vbNewLine
            Next
        MsgBox Msg
    Next
EndFunction


 

FunctionRenameAllImages(Root)
'The following function sets a new name for all image test objects under a specified object.
 DimTOCollection, TestObject, PropertiesCollection,Property
    SetTOCollection = RepositoryTo.GetAllObjectsByClass("Image")
    Fori = 0ToTOCollection.Count - 1
            SetTestObject = TOCollection.Item(i)
            RepositoryTo.RenameObject (TestObject,"Image "& i)
            RepositoryTo.UpdateObject TestObject
    Next
EndFunction


 

FunctionRemoveAllLinks(Root)
'The following function recursively enumerates all the test objects under a specified object.
'It looks for all test objects of class Link and removes them from their parent objects.
    DimTOCollection, TestObject, PropertiesCollection,Property
    SetTOCollection = RepositoryFrom.GetChildren(Root)
    Fori = 0ToTOCollection.Count - 1
            SetTestObject = TOCollection.Item(i)
            TOClass = TestObject.GetTOProperty("micclass")
         IfTOClass ="Link"Then
                RepositoryFrom.RemoveObject Root, TestObject
            EndIf
        EnumerateAllChildProperties TestObject
    Next
EndFunction


 

CallEnumerateAllChildProperties(Null)
CallEnumerateAllObjectsProperties(Null)
CallRenameAllImages(Null)
CallRemoveAllLinks(Null)
SetImageObj = RepositoryFrom.GetObject("Browser(""CNN.com"").Page(""CNN.com"").Image(""Remains identified"")")
If(NotIsNull(ImageObj))Then
 MsgBox RepositoryFrom.GetLogicalName(ImageObj)
 ElseMsgBox"null"
EndIf
SetPageObj = RepositoryTo.GetObjectByParent("Browser(""CNN.com"")","Page(""CNN.com"")")
If(NotIsNull(PageObj))Then
 MsgBox RepositoryTo.GetLogicalName(PageObj)
 ElseMsgBox"null"
EndIf
RepositoryTo.AddObject ImageObj, PageObj
RepositoryFrom.Save
RepositoryTo.Save

TAG:

 

评分:0

我来说两句

Open Toolbar