Image-Based Test Automation Using Sikuli

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

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

 

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

 

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

 

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

 

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,"remi@abc.com");

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.

Image-Based Test Automation Using Sikuli (dzone.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值