Jump to content
Sign in to follow this  
edddy

using autoIt scripts with C# dll

Recommended Posts

edddy

Hello, I am trying to automate a wpf application using the UIA class in c# and am accessing the functions

in the dll from autoIt scripts by making COM objects. I referred this post and am trying to automate the wpf controls by calling functions from within my autoitscripts.. Is this a correct approach or is there another easier way...?????

I am trying to access the c# code using the foll autoit script

$filePath ="C:\DATA\USERS\10156952\Visual Studio 2010\Projects\SampleApp2\SampleApp2\bin\Debug\SampleApp2.exe"
$obj = ObjCreate("Program.Program")

$obj.ProgramStart($filePath)
$obj.FindDesktopElement()
$obj.FindWindow("MainWindow")
$obj.FindControl("Entry")
$obj.FindControl("Click Me")
$obj.FindControl("Exit")

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Automation;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;


namespace FinalSolution
{
    [ComVisible(true)]
    public class Class1
    {
        /// <summary>
        /// Automation Elements for the form elements.
        /// </summary>
         AutomationElement DesktopElement = null;
         AutomationElement WindowElement = null;
         AutomationElement TxtEntryBx = null;
         AutomationElement ClickButton = null;
         AutomationElement ExitButton = null;

        /// <summary>
        /// Control Patterns for the various elements of the form
        /// </summary>
        protected ValuePattern vpTextbox;
        protected InvokePattern IclickButton;
        protected InvokePattern IExitButton;
        
        /// <summary>
        /// Starting the application
        /// </summary>
        /// <param name="filePath"></param>
        public void ProgramStart(String filePath)
        {
            Process.Start(filePath);
        }

        /// <summary>
        /// Find the desktop automationElement(RootElement)
        /// </summary>
        public void FindDesktopElement()
        {
            try
            {
                this.DesktopElement = AutomationElement.RootElement;
            }
            catch (NotImplementedException e) { MessageBox.Show(e.StackTrace); }
            
        }

        /// <summary>
        /// Find our desired window.
        /// </summary>
        /// <param name="WindowName"></param>
        public void FindWindow(string WindowName)
        {          
             this.WindowElement =  this.DesktopElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, WindowName));          
        }

        /// <summary>
        /// Find the desired control within the form
        /// </summary>
        /// <param name="type"></param>
        public void FindControl(string type)
        {          
         //   try
         //   {
                switch (type)
                {
                    case "Entry":                   
                         this.TxtEntryBx =  this.WindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, type));
                        break;

                    case "Click Me":
                         this.ClickButton =  this.WindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, type));
                        break;

                    case "Exit":
                         this.ExitButton =  this.WindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, type));                       
                        break;
                }
        //    }
        //    catch (NotImplementedException E) { Console.WriteLine("Error : " + E.StackTrace); }                    
        }

        /// <summary>
        /// Perform the desired operations.
        /// </summary>
        /// <param name="p"></param>
        /// <param name="value"></param>
        public void PerformOperations(string p, string value)
        {
            switch (p)
            {
                case "Entry":
                     this.vpTextbox = (ValuePattern) this.TxtEntryBx.GetCurrentPattern(ValuePattern.Pattern);
                     this.vpTextbox.SetValue(value);
                    Console.WriteLine("Done Inputting value into the text field");
                    break;

                case "Click Me":
                     this.IclickButton = (InvokePattern) this.ClickButton.GetCurrentPattern(InvokePattern.Pattern);
                     this.IclickButton.Invoke();
                    Console.WriteLine("Done Clicking the \"Click Me\" button");
                    break;

                case "Exit":
                     this.IExitButton = (InvokePattern) this.ExitButton.GetCurrentPattern(InvokePattern.Pattern);
                     this.IExitButton.Invoke();
                    Console.WriteLine("Done Clicking the \"Exit\" button");
                    break;
            }
        }
    }
}

I am really new to autoIt and this may be a really obvious mistake..

Please help me.. Thanx.

Eddy

Edited by edddy

Share this post


Link to post
Share on other sites
edddy

Hello,

If someone could tell me at least if I am in the right direction..

I tried to get the handles of the controls earlier through ControlGetHandle command.. This command works fine with forms.. but I cant seem to get the handle in a wpf application.. Is it something that I am doing wrong..???

Please help me..

Thank you.

Eddy

Share this post


Link to post
Share on other sites
Richard Robertson

WPF uses something completely different. I believe it is actually rendered like a web page instead of standard controls as separate windows.

Also, do you realize you've got a NotImplimentedException being thrown at the end of FindControl?

Share this post


Link to post
Share on other sites
edddy

WPF uses something completely different. I believe it is actually rendered like a web page instead of standard controls as separate windows.

Also, do you realize you've got a NotImplimentedException being thrown at the end of FindControl?

Hello Richard,

Thank you so much for the reply.. yes i had thrown that exception for another case there.. forgot to remove it..

So.. u mean to say there is no way of automating WPF applications from AutoIt ??

Edited by edddy

Share this post


Link to post
Share on other sites
Richard Robertson

I don't believe there is any way to automate WPF at all actually. I've never seen such a thing unless some internal object were exposed specifically for automation (which is the best way to do it).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.