Monday, 8 September 2014

Executing Sample Program using Sikuli.

Click link for Sikuli Installation.

Below program demonstrates, click Window XP Start button using Java and Sikuli

To click the Window XP start button, first its required to capture the "Start" button image and this can be done by two way's.

Option 1: Capturing "Start" button image using Sikuli IDE.
Option 2: Capturing "Start" button image using keyboard "Print Screen" button and crop the image(i.e. removing the unwanted portion of the image).

The current page illustrates capturing image using option 2.


Crop the image and save.


After the image is cropped and saved(i.e. "Start1.PNG").

Sikuli Installation folder:

Set the Build path for Sikuli jar.


Sample Program:
package pack1;
import org.sikuli.script.Screen;

public class SilkuliExample1 {
public static void main(String[] args) {
Screen s = new Screen();
try {
s.click("D:\\Eclipse Workspace\\Sikuli Proj\\img\\Start1.PNG");
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
}


When program is executed, Sikuli click's the Windows XP Start button.


Sikuli Troubleshooting:
        Issue with the below program is captured image is not proper(i.e. Sikuli unable to match the image provided in the program with the screen), in that case it is required to recapture the image and pass it to the program. 

Cropped image("Start.png"):

Sample Program:
package pack1;

import org.sikuli.script.Screen;

public class SilkuliExample1 {

public static void main(String[] args) {
Screen s = new Screen();
try {
s.click("D:\\Eclipse Workspace\\Sikuli Proj\\img\\Start.PNG");
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output:

FindFailed: can not find D:\Eclipse Workspace\Sikuli Proj\img\Start.PNG on the screen.
  Line 1574, in file Region.java

at org.sikuli.script.Region.handleFindFailed(Region.java:1574)
at org.sikuli.script.Region.wait(Region.java:1682)
at org.sikuli.script.Region.find(Region.java:1590)
at org.sikuli.script.Region.getLocationFromTarget(Region.java:1991)
at org.sikuli.script.Region.click(Region.java:2197)
at org.sikuli.script.Region.click(Region.java:2181)
at pack1.SilkuliExample1.main(SilkuliExample1.java:10)

Note: Above program is demonstrated using Windows XP environment and program doesn't work with other Windows Operating Systems, to execute above program on other operating systems(i.e. Windows 7, Windows Vista, etc) re-capture the image and add the image path in the program.

No comments:

Post a Comment