sikuli的官網(wǎng)地址:http://www.sikuli.org
首先下載sikuliX的jar包:https://launchpad.net/sikuli/sikulix/1.1.0
java-doc:http://nightly.sikuli.de/docs/index.html
官網(wǎng)用法介紹:http://www.sikulix.com/quickstart.html
需求:
1.打開百度地圖
2.切換城市到鄭州
3.使用測距工具
4.測量綠城公園--鄭州植物園的距離
前序工作:
1.導(dǎo)入jar包:sikulixsetup-1.1.0.jar
2.在cmd中運行該jar包。java -jar sikulixsetup-1.1.0.jar,則會出現(xiàn)如下圖,選擇相應(yīng)選項后,啟動會下載sikulixapi.jar。不然會報錯:libs to export not found on above classpath: /sikulixlibs/mac/libs64
3.啟動成功后會出現(xiàn)如下提示
4.截圖,即代碼中需要用到的出發(fā)位置,終點位置
接下來我們編寫代碼:
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 創(chuàng)建時間: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的執(zhí)行過程log