Wednesday 5 November 2014

Its Time for AutoIT - Parameterizing AutoIT Script through Java

                Many of us are aware that we can use AutoIT for handling windows based popups and Files uploading etc. Now Iam going to discuss about much more advanced usage of this AutoIT. When I was using this AutoIT for files uploading, I got scenario of uploading multiple files in different places. But if I use AutoIT script with the hard coded file path, I cannot use it for other files. Only way I got is to pass parameters to my AutoIT exe file while I run it. AutoIT provides us options for sending parameters from our java code.The below commands from the AutoIT will be used for doing that.

$CmdLineRaw - This command takes the whole text as a single parameter.
Lets see an example with the above command.

Below is the AutoIT script
#include <Constants.au3>
; This displays mesage box with the parameterized text
$text =$CmdLineRaw
MsgBox($MB_SYSTEMMODAL, "My First Script!", $text)

Java code looks like below:

String command="D:/Backup/Project/OwnWorkspace/AutoIT/notepad.exe This is parameterized test";
Runtime.getRuntime().exec(command);

Now the next question is how to multiple parameters.AutoIT provides option for this as well. Below commads are used for doing that.

$CmdLine[0] ; Contains the total number of items in the array.
$CmdLine[1] ; The first parameter.
$CmdLine[2] ; The second parameter.
...
$CmdLine[nth] ; The nth parameter e.g. 10 if the array contains 10 items.

Lets see an example with the above commands:

#include <Constants.au3>
; This displays given text in the notepad
$title =$CmdLine[1]
$text=$CmdLine[2]
Run("C:\Windows\notepad.exe")
Sleep(1000)
WinWaitActive($title)
WinFlash($title)
Sleep(3000)
Send($text)

Java Code:
String command="D:/Backup/Project/OwnWorkspace/AutoIT/notepad.exe \"Untitled - Notepad" \"Hi this is notepad\"";
Runtime.getRuntime().exec(command);

D:/Backup/Project/OwnWorkspace/AutoIT/notepad.exe--it is a path where you stored your .exe file which got converted from au3 file

3 comments:

  1. Hi,

    Tried to use the below command .
    String cmd="C:/AutoITScripts/pdf.exe \"Opening PE_WL_BE_Pricing.pdf \"hi text\""; However, its not getting executed(meaning file gets executed but code
    written in the autoV3 script is not getting executed). But this work fine for the below command.

    String cmd="C:/AutoITScripts/pdf.exe \"Opening PE_WL_BE_Pricing.pdf\"";

    Can you please drop your thoughts on this.

    ReplyDelete
  2. You won't believe me, but I was planning to write a blog very similar to the one you have posted here. Great work!

    Selenium Training In Chennai

    Java Training In Chennai

    ReplyDelete