Sikuli API for Java provides image-based GUI automation functionalities to Java programmers. It is created and will be actively maintained bySikuli Lab. The effort of making this API is a response to the desire of many Sikuli users to use Sikuli Script’s functionalities directly in their Java programs, rather than writing Jython scripts. This new Java library has a re-designed API and includes several new functions that were not available in the original Sikuli Script, such as the abilities to match colors, handle events, and find geometric patterns such as rectangular buttons.
You can also download this eclipse project from :
https://drive.google.com/file/d/0B09BIsDTY_AuYVB4ZjJNM3h0ZlE/view?usp=sharing
Steps:
-
Open Eclipse IDE
-
Create a new Project
-
Download Selenium Bindings
-
Download Sukuli JAR
-
Right click on you project
-
Open New>Class
-
Right click on you project
-
Open Build Path>Configure Build Path
-
Open Libraries Tab
-
Click add External Jars button
-
Add Following Imports
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.api.*;
import org.sikuli.api.robot.Mouse;
import org.sikuli.api.robot.desktop.DesktopMouse;
-
Add Selenium and Java Bindings
-
Paste Following code in Your Class
@BeforeClass
public static void setUp() throws Exception {
wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
public void TestCase1() throws InterruptedException {
}
@After
public void tearDown() {
wd.quit();
}
public static boolean isAlertPresent(FirefoxDriver wd) {
try {
wd.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
-
Open Flash Calculator Link in Browser “http://www.terrence.com/flash/calculator.html “
-
Take small images of required number and operations like 1.png,2.png , equal.png and multiply.png etc. you can use snipping tool utility for this purpose its pre-installed in Win 7 or greater
Like These
-
Now Create function which takes path of image as string and click on that image
Code is:
public void click_Image(String img)
{
s = new DesktopScreenRegion();
target = new ImageTarget(new File(img));
r = s.find(target);
// Create a mouse object
mouse = new DesktopMouse();
// Use the mouse object to click on the center of the target region
mouse.click(r.getCenter());
}
-
Now add Following code in your test case first navigate to URL by web driver and then click by images which you created example code is
@Test
public void register() throws InterruptedException {
click_Image(“IMG\\1.png”);
click_Image(“IMG\.png”);
click_Image(“IMG\\plus.png”);
click_Image(“IMG\\2.png”);
click_Image(“IMG\\equal.png”);
}
Your final code would be:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.api.*;
import org.sikuli.api.robot.Mouse;
import org.sikuli.api.robot.desktop.DesktopMouse;
public class testAutomation {
public static FirefoxDriver wd;
ScreenRegion s;
Target target ;
ScreenRegion r;
// Create a mouse object
Mouse mouse ;
public void click_Image(String img)
{
s = new DesktopScreenRegion();
target = new ImageTarget(new File(img));
r = s.find(target);
// Create a mouse object
mouse = new DesktopMouse();
// Use the mouse object to click on the center of the target region
mouse.click(r.getCenter());
}
@BeforeClass
public static void setUp() throws Exception {
wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
public void register() throws InterruptedException {
click_Image(“IMG\\1.png”);
click_Image(“IMG\.png”);
click_Image(“IMG\\plus.png”);
click_Image(“IMG\\2.png”);
click_Image(“IMG\\equal.png”);
}
@After
public void tearDown() {
wd.quit();
}
public static boolean isAlertPresent(FirefoxDriver wd) {
try {
wd.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
}