Wairhard
Wairhard

Reputation: 19

UI automation .net invoke pattern not working

Good day everyone, have some trouble with UI automation while trying to click a button on Outlook security warning while trying to send mails through My client, when I try to send it it shows the alert prompt to choose if I want to allow e-mail send or not.

This is my code so far it recognizes everything but when it comes to the point of invoking the pattern on the allow button it does nothing, already checked the ispassword property to check if this button is locked but no luck so far.

 aeDesktop = AutomationElement.RootElement
        Dim ipClickOkBtn As InvokePattern
        Dim numwaits As Integer

        Do
            aeOut = aeDesktop.FindFirst(TreeScope.Subtree, New PropertyCondition(AutomationElement.NameProperty, "Microsoft Outlook"))
            numwaits += 1
            Thread.Sleep(100)

        Loop While aeOut Is Nothing AndAlso numwaits < 50

        If Not IsNothing(aeOut) Then


            aePass = aeOut.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, "Allow"))

            Dim isTextPassword As Boolean = CBool(aePass.GetCurrentPropertyValue(AutomationElement.IsPasswordProperty))

        End If
        ipClickOkBtn = DirectCast(aePass.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)
        aePass.SetFocus()

        SendKeys.SendWait(vbCr)
        SendKeys.SendWait("{ENTER}")
        ipClickOkBtn.Invoke()

Any ideas?, thanks a lot for your help.

Upvotes: 1

Views: 1565

Answers (1)

AlbusMPiroglu
AlbusMPiroglu

Reputation: 653

I don't have outlook on my computer, but a few things come to my mind. First of all, you're trying to find a child of the Outlook main window with name property "Allow" and you assume that it's the OK button you're looking for. I suspect that it is not the case. A dialog box (it's a dialog isn't it?) is generally made a direct child of main window, so I assume you're finding the dialog box as aePass, then try to get the invoke pattern of the dialog (but cannot, because a dialog probably doesn't have invoke pattern). I suggest you find the button as an automation element under the dialog first, then get the invoke of that element:

aePass = aeOut.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, "Allow"))
ipClickOkBtn = aePass.FindFirst(TreeScope.Children, New PropertyCondition(AutomationElement.NameProperty, "<put the button name here>"))
clickOkInvoke = DirectCast(ipClickOkBtn.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)

Also, I must add it's not a good idea to search the subtree of the desktop by:

aeOut = aeDesktop.FindFirst(TreeScope.Subtree, 

instead, use the TreeScope.Children to search for the main window (they're always direct child of desktop).

Upvotes: 1

Related Questions