How to create description object model at runtime

2019-07-25 21:47发布

问题:

thank you for taking a look on this question. Just wondering if there is a best approach to create description object model at runtime. My code fails
Object doesn't support this property or method: 'Browser(...).page(...).WebButton'

FunctionCreateDescObjAt_RunTime(StrBrowserNme,StrBrwsrTitle,StrObject,StrPgeNme,StrPgtitle,StrObjectName,index)`

    'create a description object for Browser & Page`

    Set WebBrwsrDesc= Description.Create
        WebBrwsrDesc("application version").value= "Internet Explorer.*"
        If StrBrowser<>"" Then
            WebBrwsrDesc("name").value=StrBrowserNme
            WebBrwsrDesc("title").value=StrBrwsrTitle
        End If

    Set WebPageDesc= Description.Create
        WebPageDesc("name").value=StrPgeNme
        WebPageDesc("title").value=StrPgtitle

'   'Based on the type of object, execute the condition`

    Select Case StrObject`

        Case "WebButton"
            Set WebBtnDes= Description.Create
            WebBtnDes("html tag").value="INPUT"
            WebBtnDes("name").value=StrObjectName   
            WebBtnDes("micclass").value="button"
            WebBtnDes("index").value=index
            'Browser("title:=.*","name:=.*").page("title:=.*","name:=.*").WebButton(WebBtnDes).fnWebButtonClick
            Browser(WebBrwsrDesc).page(WebPageDesc).WebButton(WebBtnDes).click

    end select

End Function

I am making a call from action CreateDescObjAt_RunTime "Account Login","Your Store", "WebButton", "", "Account Login", "Login", "" And this is failing. However if I un comment this line & comment problem line, it works Browser("title:=.*","name:=.*").page("title:=.*","name:=.*").WebButton(WebBtnDes).fnWebButtonClick

Could you please help me with the right approach? thanks

回答1:

If you want to set a generic browser and page you can simply use a statement similar to the line you have commented:

Dim objPage : Set objPage = Browser("class:=browser").Page("title:=.*")

The line above will create a page object that you can work with.

Check the parameters being passed to your function to make sure you are correctly identifying your browser and page.

For the part of your actual object, which you want to create at runtime, you need to create a Description object, then look for the ChildObjects of your main object (in this case, your page) and store it to a collection. After that you can check whether or not your object is found. So your Select Case part would be something like this:

Select Case StrObject

    Case "WebButton"
        ' This is just a description of your object, not your actual object
        Dim descButton : Set descButton = Description.Create
            descButton("html tag").value="INPUT"
            descButton("name").value=StrObjectName   
            descButton("micclass").value="button"
            descButton("index").value=index

        ' In the following statement you are looking for all child objects
        ' of your page that matches with your description, and storing it
        ' into the collButton collection
        Dim collButton : Set collButton = Browser("class:=browser").Page("title:=.*").ChildObjects(descButton)

        If collButton.count > 0 Then ' Now you are checking if any object was found
            ' There are many ways to get the button object that you want.
            ' Here I'm just assuming you want the first one, but you could iterate
            ' into the collection to make sure you have the right one
            Dim objButton : Set objButton = collButton(0) ' I'm getting the first item, which is in index 0 of your collection
            objButton(0).Click ' This object already have the whole Browser().Page().WebButton() identified, so no need to use it
        Else
            MsgBox "No WebButton found. Please check your Description object"
        End If

    ' Your other cases...

    End Select


回答2:

MicClass of a Webbutton Can't be button. It should be WebButton

' You are using following

WebBtnDes("micclass").value="button"

It Should be : WebButton

'Anyway Describing Description Object

Set ObjButton=Description.Create
ObjButton("MiCClass").value="WebButton"
ObjButton("name").value=strButtonName
ObjButton("htmlid").value=strHtmlId
Set ObjButton= Browser().page().ChildObject(ObjButton)