Part 1 of this series is dedicated to those of you looking to migrate away from the Sikuli IDE and are looking for a friendly guide to get you started. For those of you already familiar with the basics of Sikuli Script in Java, much of this article will rehash what you already know, but go ahead and give it a read and feel free to provide me any useful feedback you might have!
In this post, I will cover:
- Configuring your development environment
- Creating your first Eclipse project
- Your first Java Sikuli Script program
Configuring your development environment:
Before I go any further, I feel obligated to tell you that all of my experience is and will continue to be in Windows 7 x64, using the Eclipse Java IDE, so don’t expect to see any references to Macs or NetBeans in my posts!
If you’ve already successfully installed and played around in the Sikuli IDE, then you are just about where you need to be. For those of you who are starting from scratch, then you will need the following:
- 32 bit Java JDK: Install the most recent version (currently 7) of the x586 Java JDK from Oracle’s website. Once again, do *NOT* install the x64 JDK, as Sikuli looks for the 32-bit version. Try the following link: http://www.oracle.com/technetwork/java/index.html
- Sikuli IDE: The Sikuli IDE includes the JAR file required to use Sikuli Script within your Java program. Try the following link: http://sikuli.org/
- Java IDE: As I mentioned earlier, I recommend Eclipse. Try the following link: http://www.eclipse.org/
Creating your first Eclipse project:
Now that the prereqs are out of the way, its time to fire up Eclipse and create your first Java project!
- The first thing you will need to do is start Eclipse and configure default Workspace. If you are new to Eclipse but have some Visual Studio experience, think of the Workspace as your Solution. For now, lets stick with the default presented with you. At this point, you should be taken to your empty Workspace that is begging for a new project!
- From here, you’ll want to create a new Java Project by navigating to “File > New > Java Project”. This will bring up the new Java Project wizard. In the dialog, type “HelloWorld” in the Project Name field name and click “Next” to continue. This will then take you to the Java Settings dialog. From here, click the Libraries tab and click the “Add External JARs…” button. Browse to the Sikuli install folder (example: C:\Program Files (x86)\Sikuli X) and select “sikuli-script.jar”. You should now see “sikuli-script.jar” in your dialog as seen in the image below. Click “Finish” and you are now ready to use Sikuli Script within your Java project!
- If you want to follow along with my example below, you will need a screenshot of your Windows Start button and trim off any excess pixels as seen in my example below.
- Within Package Explorer, right-click on the “HelloWorld” project folder and click “New > Folder” and name it “img”. Now browse to this folder on your file system and save your Windows 7 Start button image as “Win7StartButton.jpg”. Now refresh your Package Explorer and you will see the image appear as seen below.
- It’s time to create your Java class where your code will reside. Right click on “HelloWorld” and select “New > Class” and set the name to “HelloWorld” and check “public static void main..” and click “Finish”. Now it’s finally time to do some coding!
Your first Java Sikuli Script program
The following HelloWorld program demonstrates what are in my opinion, they most important classes within the Sikuli library; the Screen class and the Pattern class. Take a look at the code below and try to give it a shot! I hope my comments are sufficient and if you have any feedback, please send it to me so that I can update my code examples.
Now that you’ve completed the “HelloWorld” tutorial above, you might be wondering where to go next. The following Java class is an overly simplified example of how my automation framework implements button clicking functionality. The first image is the class itself and the second is a usage example. Once again, these are purely academic in nature and are here to inspire you! In closing, I hope you have enjoyed the first of the Sikuli Script in Java posts and once again, *please* send me any feedback so that I can improve these examples! – Paul Frymire