DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Hidden Cost of Flaky Tests in Test Automation
  • Selenium Test Automation Challenges: Common Pain Points and How to Solve Them
  • From Test Automation to Autonomous Quality: Designing AI Agents for Data Validation at Scale
  • End-to-End Test Automation With Playwright, GitHub Page, and Allure Reports

Trending

  • Why SAP S/4HANA Landscape Design Impacts Cloud TCO More Than Compute Costs
  • Building Production-Grade GenAI on GCP with Vertex AI Agent Builder
  • How to Prevent Data Loss in C#
  • Querying Without a Query Language
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Image-Based Test Automation Using Sikuli

Image-Based Test Automation Using Sikuli

This article explains how to interact with web elements using images integrating Selenium and Sikuli APIs.

By 
Remi Andonissamy user avatar
Remi Andonissamy
·
May. 18, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
9.9K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Sikuli is an open source test automation tool for testing web applications. It is similar to Selenium, but it considers all the web elements of the application as images. This tool is capable of recognizing images and interacting with the elements matching the image GUI.

How to Use Sikuli in an Eclipse-Maven Project

Step 1: Create a Maven project called "SikuliProject".

Step 2: Add the Sikuli jar to the pom.xml file in order to load the Sikuli library to our project.

XML
 




x


 
1
    <dependency>
2
        <groupId>com.sikulix</groupId>
3
        <artifactId>sikulixapi</artifactId>
4
        <version>1.1.0</version>
5
    </dependency>


Step 3: Add a Selenium dependency to support loading the browser, opening the application URL, etc.

XML
 




xxxxxxxxxx
1


 
1
    <dependency>
2
        <groupId>org.seleniumhq.selenium</groupId>
3
        <artifactId>selenium-java</artifactId>
4
        <version>3.141.59</version>
5
    </dependency>


Step 4: Create a script using two main classes – "Screen" and "Pattern" – and perform testing of web applications.

Screen Class

This is the main class of the Sikuli API. It contains various built-in methods to perform actions on web elements. 

Examples include: click(), doubleclick(), wait(), and type().

Example:

Java
 




xxxxxxxxxx
1


 
1
Screen screen = new Screen();
2
screen.click(loginlink);


Below are some commonly used Screen() class methods:

  1. click() -> Clicks on an element referred to by the image name.
  2. doubleclick() -> Double clicks on an element referred to by the image name.
  3. delayClick() -> Clicks an element after the given time in milliseconds.
  4. delayType() -> Introduces a delay in milliseconds between each character at next type.
  5. type() -> Type the given text as an argument.

Pattern Class

It is used to uniquely identify the web element based on the image stored in a specific location.

Syntax: Pattern p = new Pattern("Location of image saved");

Example:

Java
 




xxxxxxxxxx
1


 
1
Pattern uname = new Pattern("C:\\Image_screenshots\\userName.png");


Example Demo for Login:

Step 1: Identify the web elements to perform login, capture images, and store them in your hard drive.

Step 2: Images captured for our demo are given below:

  • Login link 
  • Username textbox
  • Password textbox
  • Login Button
  • Logout Link

Selenium-Sikuli Demo:

Java
 




xxxxxxxxxx
1
39


 
1
import org.openqa.selenium.WebDriver;
2
import org.openqa.selenium.firefox.FirefoxDriver;
3
import org.sikuli.script.Pattern;
4
import org.sikuli.script.Screen;
5
import org.testng.annotations.Test;
6

          
7
public class ImageProcessing {
8
    
9
    @Test
10
    public void testImage() throws Exception  {
11
        Screen screen = new Screen();
12
        
13
        Pattern loginlink = new Pattern("C:\\Image_screenshots\\loginLink.png");
14
        Pattern uname = new Pattern("C:\\Image_screenshots\\userName.png");
15
        Pattern pwd = new Pattern("C:\\Image_screenshots\\password.png");
16
        Pattern signin = new Pattern("C:\\Image_screenshots\\loginButton.png");
17
        Pattern logout = new Pattern("C:\\Image_screenshots\\logoutLink.png");
18
        
19
        System.setProperty("webdriver.gecko.driver","C:\\Selenium\\Drivers\\geckodriver.exe");
20
        WebDriver driver = new FirefoxDriver();
21
        driver.manage().window().maximize();
22
        driver.get("http:\\demowebshop.tricentis.com");
23
        
24
        screen.wait(loginlink,10);
25
        screen.click(loginlink);
26
        screen.wait(uname,10);
27
        screen.type(uname,"[email protected]");
28
        screen.type(pwd,"test@1234");
29
        screen.click(signin);
30
        screen.wait(logout,10);
31
        
32
        if (screen.exists("C:\\Image_screenshots\\logoutLink.png")!=null) {
33
            System.out.println("Element is present");
34
            screen.click(logout);
35
        }
36
            
37
    }
38

          
39
}


Conclusion

Sikuli is used widely when Selenium has difficulty recognizing the elements or when there is a need to perform image-based element recognitions. Thanks for reading this article on how to integrate Selenium-Sikuli and perform image-based testing.

Test automation Testing

Opinions expressed by DZone contributors are their own.

Related

  • The Hidden Cost of Flaky Tests in Test Automation
  • Selenium Test Automation Challenges: Common Pain Points and How to Solve Them
  • From Test Automation to Autonomous Quality: Designing AI Agents for Data Validation at Scale
  • End-to-End Test Automation With Playwright, GitHub Page, and Allure Reports

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook