Desktop Automation using AutoIT in C# Visual Studio

January 18, 2024

What is AutoIT?

AutoIt is a scripting language and window-based application automation tool.

Pros:

  • Windows Automation: AutoIt is great for automating Windows applications. It allows your C# application to do things like clicking buttons and typing text in other programs.
  • Easy to Learn: AutoIt’s scripting language is easy to understand. It’s good for people who want to automate Windows tasks without learning complex programming languages.
  • Useful Functions: AutoIt has built-in functions for many tasks, including working with GUIs (Graphical User Interfaces), files, and the clipboard.
  • Script Recording: AutoIt can record your actions and turn them into automation scripts, which is helpful for beginners.

Cons:

  • Windows Only: AutoIt works only on Windows. You can’t use it for automation tasks on other operating systems like Linux or macOS.
  • Limited for Web: While you can use AutoIt with web browsers, it’s not the best choice for web automation tasks compared to other tools.
  • Script Compilation: To run AutoIt scripts, you need to compile them into executable files. This adds an extra step when you want to deploy your automation.
  • Security Concerns: Some security software might mistakenly flag compiled AutoIt scripts as harmful, causing false alarms.
  • Learning Curve: Though AutoIt is easy to get started with, mastering it for complex tasks might take some time.

Installation Steps:

  1. Step 1 : Download & Install Finder Tool from Link
    File Type: .zip
    Use: To Find Window Elements
  2. Step 2:Download & Install Script Editor i.e SciTe Tool from Link
    File Type: .exe
    Use: To write Script for Window  Automation
  3. Step 3:You can see AutoIT Folder appears under ‘All Apps’
  4. Step 4:Click on AutoIT Finder Tool (According to your system property i.e. x64 / x84)
  5. Step 5: The AutoIT Finder Tool will open
    .
  6. Step 6: Access the start menu again, open the AutoIT folder, and launch SciTE Script Editor (According to your system property i.e. x64 / x84) .
  7. Step 7: SciTE Script Editor will open.
  8. Step 8:Open Chrome & load Web Application URL i.e. Register (automationtesting.in)
  9. Step 9: Click on ‘Choose file’ – a window will appear.
  10. Step 10: Open SciTE Script Editor and write basic code:
  11. Step 11: Now Open SciTE Script Editor & write Basic Code
    // To focus on opened window
    ControlFocus("", "", "Edit1")
    
    // To set text on Element
    ControlSetText("", "", "Edit1", "")
    
    // To click on Element
    ControlClick("", "", "")
    
    
    
  12. Step 12: Now Open Web Application & AutoIT Finder tool
  13. Step 13: Now move this pointer tool to other element
  14. Step 14: Now I moved the pointer to ‘File Name’ Element & we can see Finder tool got the details. So now we copy value from ‘ClassnameNN’ field
  15. Step 15: Now paste the copied value to SciTE Scripting Tool
  16. Step 16: Now Open Web Application & AutoIT Finder tool
  17. Step 17: Now copy the ‘Title’ value i.e ‘Open’ & paste it in SciTE Tool
  18. Step 18: Now Open Web Application & AutoIT Finder tool
  19. Step 19: Now right click on the file which you want to upload& copy file path
  20. Step 20: Now paste the Value in SciTE Tool
  21. Step 21: Now open Web Application & AutoIT Finder tool then Locate another Element2 i.e ‘Open’ button. Copy the Element value ‘Button1’ from finder tool
  22. Step 22: Now paste the Value in SciTE Tool
  23. Step 23: Now click on save icon to Save this File
  24. Step 24: Now give file name & file type will be saved as ‘.au3’ then click on save button
  25. Step 25: Now open the folder location where ‘.au3’ file is saved
  26. Step 26: Now Right click on Upload file & click on Compile Script (You Can choose x64 / x84 bit according to your system)
  27. Step 27: We can see .exe file is generated (We need this .exe file path in our code)
  1. Creating Project in Visual Studio:
  1. Step 1: Launch Visual Studio or download and install it if you haven’t already.
  2. Step 2: Open Visual Studio
  3. Step 3: Click on ‘Create a new project’
  4. Step 4: Below window will open
  5. Step 5: Choose Project Template “Console App” for C# & click on Next button
  6. Step 6: Give your project a name and specify the location where you want to save it.
  7. Step 7: Click the “Next” button
  8. Step 8: Choose “Framework” to create your new C# project and click on “Create” button
  9. Step 9: Project Screen will appear
  10. Step 10: Right-click on Your Project
  11. Step 11: Click the “Browse” section and search for “Selenium.WebDriver”
  12. Step 12: Install latest “Selenium.WebDriver”
  13. Step 13: Open “Program.cs” file & paste below code in itusing OpenQA.Selenium;using OpenQA.Selenium.Chrome;using OpenQA.Selenium.Interactions;using System.Diagnostics;public class Program{public static void Main()

    {

    // To initialize & launch the driver

    IWebDriver driver = new ChromeDriver();

    // To call action class methods

    Actions a = new Actions(driver);

    // Load the URL

    driver.Navigate().GoToUrl(“http://demo.automationtesting.in/Register.html”);

    // Maximize the Browser Window

    driver.Manage().Window.Maximize();

    // Stop the execution for 5 seconds (5000 miliseconds)

    Thread.Sleep(5000);

    // To click on “Choose File” button

    a.MoveToElement(driver.FindElement(By.XPath(“//input[@id=’imagesrc’]”))).Click().Build().Perform();

    // Wait for some time (5000 milliseconds)

    Thread.Sleep(7000);

    // Use Process.Start to run the AutoIt script for file upload

    string executablePath = @”E:\Demo\Upload2.exe”; // Paste actual path of .exe file which we have created

    // To start AutoIt program execution

    Process.Start(executablePath);

    // Wait for some time (5000 milliseconds)

    Thread.Sleep(5000);

    // Close the WebDriver

    //driver.Quit();

    }

    }

  14. Step 14: Build the code & Run It!