Introduction to Sikuli GUI Automation Tool (Automate Anything You See on Screen) – Sikuli Tutorial #1

As always we try to bring in new things to learn for our readers. Today let’s explore an interesting GUI automation tool – Sikuli.

“Automate anything you see” using Sikuli graphical user interface (GUI) automation tool – Complete beginners guide to quickly set up and start using Sikuli script tool with these in-depth Sikuli Tutorials.

Sikuli automates anything you see on the screen using the image recognition method to identify GUI elements. Sikuli script allows users to automate GUI interaction by using screenshots.

We have divided this series into 2 parts:

Sikuli tutorial #1How It works, How to create a simple Sikuli project.

Sikuli tutorial #2How to create Sikuli maven project, How Sikuli can be used with selenium web driver to automate webpages.

Let’s start with a 1st part in this series.

What You Will Learn:

What is covered in this tutorial:

Introduction

Sikuli is a tool to automate graphical user interfaces (GUI) using “Visual Image Match” method. In Sikuli, all the web elements should be taken as an image and stored inside the project. Sikuli will trigger GUI interactions based on the image visual match, the image which we have passed as the parameter along with all methods.

Sikuli can be very much useful to automate flash objects (which do not have ID or name). It can be useful in the situation, where we have a stable GUI (i.e. GUI components not changing).

Even Window based applications can also be automated using Sikuli. Sikuli provides very friendly Sikuli-script.jar, which can be easily used together with Selenium WebDriver. We can even automate Adobe Video/Audio player, Flash Games on the website using Sikuli. With simple API, it makes coding easier.

Practical Uses

  1. Sikuli can be used to automate Flash Objects / Flash Websites.
  2. It can be useful to automate Window based application. We can automate, what we are seeing on the screen.
  3. It provides, simple API. i.e. all methods can be accessed using screen class object.
  4. It can be easily integrated with Selenium and all other tools.
  5. Using Sikuli we can automate desktop applications.
  6. Most of the automation testing tools will not support flash object automation (E.g. Selenium). Sikuli provides extensive support to automate flash objects.
  7. It uses powerful “Visual Match” mechanism to automate desktop & flash objects.

Benefits

Prerequisites:

Before getting started, we need to download and install the following software:

Steps to Create Sikuli Java Project:

Step #1: Sikuli Download – Download Sikuli from here.

Step #2: Extract the zip file which you’ve downloaded. It will contain the sikuli-script.jar file. Save this extracted file in your local file system.

Step #3: Open Eclipse.

Step #4: Create a java project File -> New  -> Java Project

Step #5:

  1. Right Click on the project
  2. Go to Build Path   -> Configure Build Path
  3. Switch to Libraries tab
  4. Click “Add External Jars” button and Add Sikuli-Script.jar in the Build Path.
  5. Click “Ok”

(Click on image to enlarge)

Sikuli-script.jar will be added to your project build path. You’re done. Now you can start writing Sikuli scripts inside this project.

Some Sikuli Methods:

#1: Creating Object for Screen Class

The screen is a base class provided by Sikuli. We need to create an object for this screen class first, then only we can access all the methods provided by Sikuli.

Syntax:

Screen s=new Screen();

#2: Click On An Element

This method used to Click on the specific image present on the screen.

Syntax:

s.click(“<<image name>>”);

Example:

s.click(“test.png”);

#3: Right Click On An Element

This method used to right click on the specific image present on the screen.

Syntax:

s.rightClick(“<<image name>>”);

Example:

s.rightClick(“test.png”);

#4:  find An Element

This method used to find a specific element present on the screen.

Syntax:

s.find(“<<image name>>”);

Example:

s.find(“test.png”);

 #5: Double Click on An Element

This method used to trigger a double click event on a specific image present on the screen.

Syntax:

s.doubleClick(“<<image name>>”);

Example:

s.doubleClick(“test.png”);

#6: Check whether an Element present on the Screen

This Method is used to check whether the specified element is present on the screen.

Syntax:

s.exists(“<<image name>>”);

Example:

s.exists(“test.png”);

#7: Type a string on a Textbox

This Method is used to enter the specified text on the Text box.

Syntax:

s.type(“<<image name>>”,”String to be typed”);

Example:

s.type(“test.png”,”HI!!”);

#8: Wheeling on a particular image

This method is used to perform wheeling action on the element image.

Syntax:

s.wheel(“<<image name>>”,<<int position>>,<<int direction>>);

Example:

s.wheel(“test.png”,25,0);

 #9: Drag and Drop a Image/Element

This method is used to drag and drop a specified image from source position to target position.

Syntax:  

s.dragDrop(“<<source image name>>”,”<<target image name>>”);

Example:      

s.dragDrop(“test.png”,”test1.png”);



#10: Roll Hover on a particular image

This method is used to perform roll hover event on the specified image.

Syntax:

s.hover(“<<image name>>”);

Example:

s.hover(“test.png”);

#11: Paste Copied String

This method used to paste text on the specified textbox.

Syntax:

s.paste(“<<image name>>”,”test”);

Example:

s.paste(“test.png”,”test”);

Sikuli Example #1: YouTube video – Pause and Play a video

Step #1:

Open a YouTube video link and Capture play and pause element images using screen capture tool.

Pause button (note: file name is pause.png)

Play button (note: file name is play.png)

Copy these images inside the project.

Step #2:

Create a package inside the Sikuli java project created and within that create a class named “Youtube”.

Step #3:

Type the following code inside that class.

 package com.test;

import org.sikuli.script.FindFailed;
 import org.sikuli.script.Screen;

public class Youtube {

public static void main(String[] args) throws FindFailed, InterruptedException {
 // TODO Auto-generated method stub

Screen s=new Screen();
 s.find("pause.png"); //identify pause button
 s.click("pause.png"); //click pause button
 System.out.println("pause button clicked");

s.find("play.png"); //identify play button
 s.click("play.png"); //click play button
 }

} 

Step #4:

Right click on the class select Run As -> Java Application.

Sikuli Example #2: Open Notepad and type some text

Step #1:

Capture notepad icon on the desktop on the screen.

 

notepad_icon.png

notepad.png                                              

Step #2:

Copy these images inside your project.

Step #3:

Create a class named “NotepadExample” inside your project and type the following code.

 package com.test;

import org.sikuli.script.FindFailed;
 import org.sikuli.script.Screen;

public class NotepadExample {

public static void main(String[] args) throws FindFailed {
 // TODO Auto-generated method stub

Screen s=new Screen();
 s.click("notepad_icon.png");
 s.find("notepad.png");
 s.type("notepad.png","This is Nice Sikuli Tutorial!!!!");
 }

} 

Step #4:

Open the screen to be tested before executing the code.

Execute this file by Right click Run As  -> Java Application.

Sikuli Example #3: Drag and drop

Step #1:

Take the screen shot of required items on the screen, and put it them inside your sikuli project.



[Note: here, downloads icon is “source.png” and flower image is “destination.png”]

Step #2:

Put these pictures inside your project.

Step #3:

Create a class with name “DragAndDrop” and write the following code.

 package com.test;

import org.sikuli.script.FindFailed;
 import org.sikuli.script.Screen;

public class DragAndDrop {

public static void main(String[] args) throws FindFailed, InterruptedException {
 // TODO Auto-generated method stub
 Screen s=new Screen();
 s.find("source.png");
 System.out.println("Source image found");
 s.find("target.png");
 System.out.println("target image found");
 s.dragDrop("source.png", "target.png");
 }

} 

Step #4:

Execute this script by right click Run As  -> Java Application.

After execution of this script, downloads icon will be dragged and dropped on the image, indicated as a target.

Before Execution:

(Click on images to enlarge)

After Execution:

Drawbacks of this tool

More resources:

Conclusion

Sikuli is very much useful in automating flash objects. It can be used to automate window based applications. It is a great tool to play with elements on a screen, based on their visual.

About the author: This is a guest post by Anitha Eswari. She is currently working as a senior test engineer having sound knowledge on manual and automation testing and various test management tools.

Next Tutorial: In next part of this series let’s have a deep look at creating Sikuli maven project and how to integrate Selenium with Sikuli.

Already using this tool? Please share your experience and tips. If you want to get started but have queries let us know.