Environment: Win10 Pro 64bit
Screenario:
When a pop up shows up , our tool tries to click the 'Yes' button by Invoke(). However , sometimes , a exception is thrown up .I want to know the root cause and the solution !!!
My workaround:
I wrapped my code with CacheReuest , this helps no exception is thrown , however , the button maybe not be clicked.
Thrown exception:
Abort Automation , reason : Operation is not valid due to the current state of the object. for developers : at MS.Internal.AutomationProxies.Misc.ThrowWin32ExceptionsIfError(Int32 errorCode) at MS.Internal.AutomationProxies.Misc.SendInput(Int32 inputs, INPUT& ki, Int32 size) at MS.Internal.AutomationProxies.Input.SendKeyboardInputVK(Int16 vk, Boolean press) at MS.Internal.AutomationProxies.Misc.SetFocus(IntPtr hwnd) at MS.Internal.AutomationProxies.WindowsButton.Invoke() at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr) at TVSUPluginAutomation.testcases.tvsu_triggerPopup_base.clickPopWin(Int32 timeout) at TVSUPluginAutomation.testcases.tvsu_DownloadAndInstall_multi14_1installedFirst.InstallRemander() at TVSUPluginAutomation.testcases.tvsu_DownloadAndInstall_multi14_1installedFirst.run() at TVSUPluginAutomation.testcases.baseClass.executeTestCase()
My Code:
protected AutomationElement findPopWinX(int timeout = 5000) { using (new IdentityScope("", CommonClass.configXML.SelectSingleNode("/root/LocalServer/User").InnerText, CommonClass.configXML.SelectSingleNode("/root/LocalServer/Pass").InnerText, LogonType.NewCredentials, LogonProvider.WinNT50)) { DateTime now = DateTime.Now; int _timeout = timeout; while (DateTime.Now < now.AddMilliseconds(_timeout)) { List<AutomationElement> all = new List<AutomationElement>(); AutomationElementCollection elementCollection = AutomationElement.RootElement.FindAll(TreeScope.Children, Condition.TrueCondition); AutomationElement ok = null, cancel = null, staticText = null; foreach (AutomationElement child in elementCollection) { try { ok = child.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "1")); cancel = child.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "2")); staticText = child.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "65535")); if (child.Current.ClassName == "#32770" && ok != null && cancel != null && staticText != null) { return child; } } catch (Exception) { } } } } return null; } protected bool clickPopWin(int timeout = 5000) { AutomationElement a = findPopWinX(); if (a==null) { return false; } AutomationElement ok = a.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "1")); if (ok==null) { return false; } InvokePattern pat = ok.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern; pat.Invoke(); return true; }
Marcus.