Sikuli Tutorial for Selenium Automation

โšก Smart Summary

Sikuli automates a graphical interface by recognizing stored screenshots rather than locators, which lets a Selenium test drive the Windows popups, native file dialogs and Flash objects that WebDriver alone cannot reach.

  • ๐Ÿ”˜ Core idea: Every element is stored as a PNG, and Sikuli finds it by matching pixels on screen.
  • โ˜‘๏ธ Setup: Add the SikuliX API JAR to the Java build path beside the Selenium JARs.
  • โœ… Screen class: Provides click, doubleClick, type, hover and find, each taking an image name.
  • ๐Ÿงช Pattern class: Wraps an image path and tunes matching through similar() and exact().
  • ๐Ÿ› ๏ธ Worked example: A Chrome test clicks Choose File, then Sikuli types the path and clicks Open.
  • โš™๏ธ Limitation: A resolution or pixel-size change breaks matching and raises FindFailed.

Sikuli Tutorial for Selenium Automation

What is Sikuli in Selenium?

Sikuli is an open-source GUI based test automation tool. It is mainly used for interacting with elements of web pages and handling windows based popups. Sikuli uses the technique of “Image Recognition” and “Control GUI” to interact with elements of web pages and windows popups. In Sikuli, all the web elements are taken as images and stored inside the project.

โš ๏ธ Naming note: the project is maintained today as SikuliX, published on Maven Central as com.sikulix:sikulixapi. The class names below are unchanged.

How to use Sikuli with Selenium Webdriver

Sikuli can be integrated with Selenium WebDriver using the Sikuli JAR file, in the two steps below.

Step 1) Download the Sikuli JAR file from the below URL, and extract the contents of the ZIP file to a folder.

https://mvnrepository.com/artifact/com.sikulix/sikulixapi/2.0.5

Step 2) Create a new JAVA project in Eclipse and add the JAR file to build path, along with selenium jar files using Right Click on the project -> Build Path -> Configure Build Path, as shown below.

Sikuli JAR file added to the Java build path in Eclipse Configure Build Path

Once you have added the JAR file to project build path, classes provided by Sikuli can be used. A Maven project can declare the same artifact as a dependency instead.

Screen class in Sikuli

Screen class is the base class for all the methods provided by Sikuli. Screen class contains predefined methods for all the commonly performed operations on screen elements such as click, double-click, providing input to a text box, hover, etc. The below is the list of commonly used methods provided by Screen class.

Method Description Syntax
Click This method is used to click on an element on the screen using image name as the parameter.

Screen s = new Screen();

s.click(“QA.png”);

doubleClick This method is used to double click on an element. It accepts image name as the parameter.

Screen s = new Screen();

s.doubleClick(“QA.png”);

Type This method is used to provide input value to an element. It accepts the image name and text to be sent as parameters.

s.type(“QA.png”,”TEXT”);

Hover This method is used to hover over an element. It accepts image name as the parameter.

s.hover(“QA.png”);

Find This method is used to find a specific element on the screen. It accepts image name as the parameter.

s.find(“QA.png”);

Pattern class in Sikuli

Where Screen acts on a plain image name, Pattern class is used to associate the image file with additional attributes to uniquely identify the element. It takes the path of the image as a parameter.

Pattern p = new Pattern(“Path of image”);

The following are the most commonly used methods of Pattern class.

Method Description Syntax
getFileName Returns the file name contained in the Pattern object.

Pattern p = new Pattern(“D:\Demo\QA.png”);

String filename = p.getFileName();

similar This method returns a new Pattern object with similarity set to a specified value. It accepts the similarity value between 0 to 1 as a parameter. Sikuli looks for all elements that fall within the specified similarity range and returns a new pattern object.

Pattern p1 = p.similar(0.7f);

Exact This method returns a new pattern object with similarity set to 1. It looks only for an exact match of the specified element.

Pattern p1 = p.exact();

Code Example for File Upload using Sikuli

Below code explains the use of Sikuli for file upload in Chrome.

package com.sikuli.demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.openqa.selenium.chrome.ChromeDriver;

public class SikuliDemo {

    public static void main(String[] args) throws FindFailed {

        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        String filepath = "D:\\Guru99Demo\\Files\\";
        String inputFilePath = "D:\\Guru99Demo\\Files\\";
        Screen s = new Screen();
        Pattern fileInputTextBox = new Pattern(filepath + "FileTextBox.PNG");
        Pattern openButton = new Pattern(filepath + "OpenButton.PNG");
        WebDriver driver;

        // Open Chrome browser    
        driver = new ChromeDriver();
        driver.get("https://demo.guru99.com/test/image_upload/index.php");

        // Click on Browse button and handle windows pop up using Sikuli
        driver.findElement(By.xpath(".//*[@id='photoimg']")).click();
        s.wait(fileInputTextBox, 20);
        s.type(fileInputTextBox, inputFilePath + "Test.docx");
        s.click(openButton);

        // Close the browser
        driver.close();
    }
}

Code Explanation

Step 1) The first statement involves setting the driver executable path for chrome.

System.setProperty("webdriver.chrome.driver", "D:\\ chromedriver.exe");

โš ๏ธ Version note: Selenium 4 resolves the driver binary through Selenium Manager, so this line is now optional. It is kept as the source wrote it.

Step 2) Use a screengrab tool such as Snipping Tool to take screenshots of windows popup ‘FileTextBox’ and ‘Open’ button, as below.

Snipping Tool selection over the Windows file input text box on the upload popup

This is how your screenshot should look:

Captured FileTextBox and OpenButton reference images used by the Sikuli script

Images for windows file input text box and open button are stored onto ‘FileTextBox.PNG’ and ‘OpenButton.PNG’.

Example: If you want to automate the operation of opening notepad, then you need to store the image of a desktop icon for notepad onto a PNG file and perform click operation on it.

In our case, it recognizes the file input text box and Open button on Windows popup using the images stored. If the screen resolution changes from image capture to test script execution, the behavior of Sikuli would be inconsistent. Hence it is always advisable to run the test script on the same resolution at which images are captured. Change in pixel size of images will result in Sikuli throwing a FindFailed exception.

Step 3) The next statements include the creation of objects for Screen and Pattern classes. Create a new screen object. Set the path of the file you want to upload as a parameter to the Pattern object.

Screen s = new Screen();
Pattern fileInputTextBox = new Pattern(filepath + "FileTextBox.PNG");
Pattern openButton = new Pattern(filepath + "OpenButton.PNG");

Step 4) The below statements involve opening chrome browser with the URL of a demo file upload application: https://demo.guru99.com/test/image_upload/index.php

driver = new ChromeDriver();
driver.get("https://demo.guru99.com/test/image_upload/index.php");

Step 5) Click the choose file button using below statement

driver.findElement(By.xpath(".//*[@id='photoimg']")).click();

Step 6) Wait for the windows popup to appear. Wait method is used to handle the delay associated with opening windows pop up after clicking on the browse button.

s.wait(fileInputTextBox, 20);

Step 7) Type the file path onto input file text box and click on Open button

s.type(fileInputTextBox, inputFilePath + "Test.docx");
s.click(openButton);

Step 8) Close the browser

driver.close();

Output

Initially, script opens chrome browser

Chrome browser opened on the demo file upload page at the start of the run

Clicks on the ‘Choose File’ button, windows file popup screen will appear. Enters data into File Input textbox and clicks on ‘Open’ button

Windows file popup with the file path typed into the File Input text box

Below screen is displayed once the file upload is complete and closes the browser

Demo page confirming the upload is complete just before the browser closes

Because matching depends on pixels, recapture every reference image whenever the interface is restyled. That fragility is why teams reserve Sikuli for popups plain WebDriver file handling cannot reach.

FAQs

The reference image was not matched on screen. Usual causes are a different resolution, a rescaled image, a restyled theme, or the element not being visible yet. Recapture at run resolution or lower similarity.

Selenium drives the browser DOM through locators. Sikuli drives the screen through screenshots, so it reaches native dialogs the DOM never exposes. The two are used together.

Yes. Declare com.sikulix:sikulixapi as a dependency and Maven pulls the same API, keeping the version in the build file rather than an Eclipse build path.

Yes, SikuliX runs on Windows, Linux and macOS. Reference images are not portable between them, because decorations and fonts differ, so each platform needs its own captures.

Machine learning matchers tolerate colour, scale and theme shifts that pixel comparison cannot, and can find a control by its label. That cuts the recapture work needed after a redesign.

GitHub Copilot drafts Screen and Pattern calls and the WebDriver setup around them. It cannot capture the PNG files, so verify every generated image path.

Matching breaks when the interface is restyled or the resolution changes, runs slower than DOM automation, and reports failures less clearly. It suits interfaces that change rarely.

The original Sikuli research project is not, but its successor SikuliX is and publishes the sikulixapi artifact used here. Articles citing plain sikuli-script JARs describe the earlier line.

Summarize this post with: