摘自金阳光
sikuli的官网地址:http://www.sikuli.org
首先下载sikuliX的jar包:https://launchpad.net/sikuli/sikulix/1.1.0
java-doc:http://nightly.sikuli.de/docs/index.html
官网用法介绍:http://www.sikulix.com/quickstart.html
需求:
1.打开百度地图
2.切换城市到郑州
3.使用测距工具
4.测量绿城公园--郑州植物园的距离
前序工作:
1.导入jar包:sikulixsetup-1.1.0.jar
2.在cmd中运行该jar包。java -jar sikulixsetup-1.1.0.jar,则会出现如下图,选择相应选项后,启动会下载sikulixapi.jar。不然会报错:libs to export not found on above classpath: /sikulixlibs/mac/libs64
3.启动成功后会出现如下提示
4.截图,即代码中需要用到的出发位置,终点位置
接下来我们编写代码:
package com.sikuli;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
/**
* @author QiaoJiafei
* @version 创建时间:2016年4月5日 下午6:43:55
* 类说明
*/
public class TestSikuli {
public static void main(String[] args) {
// TODO Auto-generated method stub
testBaiDuMap();
}
/**
* @Title: testBaiDuMap
* @Description: TODO
* @return void
* @throws
*/
private static void testBaiDuMap() {
// TODO Auto-generated method stub
WebDriver dr = new SafariDriver();
dr.manage().window().maximize();
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
dr.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
dr.get("http://map.baidu.com/");
//打开城市选择下拉框
dr.findElement(By.id("tool-container")).findElement(By.id("ui3_city_change")).findElement(By.tagName("a")).click();//.findElement(By.tagName("em")).click();
//选择H省
dr.findElement(By.id("selCityLetterListId")).findElement(By.linkText("H")).click();
//选择郑州
dr.findElement(By.id("selCityPlaceListId")).findElement(By.linkText("郑州")).click();;
//打开工具下拉框
dr.findElement(By.id("tool-container")).findElement(By.xpath("//i[text()=‘工具‘]")).click();
//选择测距
dr.findElement(By.id("boxul")).findElement(By.xpath("//i[text()=‘测距‘]")).click();
Screen screen = new Screen();
String frompng = "/Users/qiaojiafei/Documents/qiaojiafei/jarfile/sikuli/testphoto/from.png";
String topng = "/Users/qiaojiafei/Documents/qiaojiafei/jarfile/sikuli/testphoto/to.png";
Pattern from = new Pattern(frompng);
Pattern to = new Pattern(topng);
try {
if(screen.find(from) != null) {
screen.click(from);
}
if(screen.find(to) != null) {
screen.click(to);
}
screen.doubleClick();
} catch (FindFailed e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
可以在代码中加入:
import org.sikuli.basics.Debug;
Debug.setDebugLevel(3);
查看控制台sikuli的执行过程log