Link to home
Start Free TrialLog in
Avatar of itnifl
itniflFlag for Norway

asked on

Pressing cancel button with C# UI Automation

var task = Task.Factory.StartNew(() => {
                  AutomationElement aeDesktop = AutomationElement.RootElement;
                  AutomationElement aeFormatMessage;
                  AutomationElement aeCancelBtn1;
                  string NameProperty = "Microsoft Windows";                  

                  int timesClicked = 0;
                  while(timesClicked < cancelButtonMaxClick) { 
                     // Were we already canceled?
                     m_cancellationToken.ThrowIfCancellationRequested();
                     // Poll on this property if you have to do
                     // other cleanup before throwing.                     
                     if (m_cancellationToken.IsCancellationRequested) {
                        // Clean up here, then...
                        m_cancellationToken.ThrowIfCancellationRequested();
                     }
                     aeFormatMessage = aeDesktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, NameProperty));

                     if (aeFormatMessage != null) {
                        string btnCancelhexID = "0000000000000000";
                        string btnCanceldecimalID = Convert.ToInt32(btnCancelhexID, 16).ToString();
                        aeCancelBtn1 = aeFormatMessage.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, btnCanceldecimalID));

                        InvokePattern ipClickCancelBtn = (InvokePattern)aeCancelBtn1.GetCurrentPattern(InvokePattern.Pattern);
                        Console.WriteLine("Clicking cancel button");
                        ipClickCancelBtn.Invoke();
                        timesClicked++;
                     }                     
                     Thread.Sleep(1);
                  }
               }, m_cancellationToken).ContinueWith((Task ourTask) => {
                  AggregateException exceptions = ourTask.Exception;                  
               }, TaskContinuationOptions.OnlyOnFaulted);

Open in new window


I format a USB disk using diskpart via C#. This will create popups saying "you need to format this drive." I want to automatically press cancel when this popup appears. I have used Spy++ to get the Control ID of the button. It seems to be 0000000000000000, which I assume must be wrong. The code discovers the window when it appears, but aeCancelBtn1 ends up as null, since I guess the button is never found. This I cannot use the code to press it. What am I doing wrong?

Adding here a picture of the dialogue I want to automate and the Spy++ info that is displayed regarding the cancel button.
User generated image
Avatar of Ammar Iqbal
Ammar Iqbal
Flag of Norway image

Have  looked  at this following link regarding Invoke a Control Using UI Automation. Hope this helps you
https://msdn.microsoft.com/en-us/library/ms747211(v=vs.110).aspx
ASKER CERTIFIED SOLUTION
Avatar of itnifl
itnifl
Flag of Norway image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itnifl

ASKER

Thanks for comments. Accepting my own comment as the solution, since I seemed to get at it first.