Sikuli-IDE用起来方便,但是用到实际项目中还是有局限性的,Sikuli提供了Sikuli-Script的jar包,在Sikuli-X的安装目录下,这样就可以在Idea中使用JAVA编写代码了,并用Sikuli-Script和selenium完美结合使用。
具体示例代码如下;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.sikuli.script.FindFailed; import org.sikuli.script.Pattern; import org.sikuli.script.Screen; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; /** * @rongrong * Selenium结合sikuliX操作Flash网页案例 */ public class TestSikuli { WebDriver driver; @BeforeClass public void beforeClass() { System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); driver = new ChromeDriver(); } @Test public void testBaiDuMap() { driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); driver.get("http://map.baidu.com/"); //打开城市下拉框 driver.findElement(By.cssSelector(".ui3-city-change-inner")).click(); //选择北京 driver.findElement(By.cssSelector("[citycode='131']")).click();; // 打开工具下拉框 driver.findElement(By.cssSelector("[map-on-click='box'] em")).click(); // 选择测距 driver.findElement(By.cssSelector("[map-on-click='measure'] i")).click(); Screen screen = new Screen(); String start = "D://picture//奥林匹克森林公园.png"; String end = "D://picture//北京南苑机场.png"; Pattern from = new Pattern(start); Pattern to = new Pattern(end); try { if (screen.find(from) != null) { screen.click(from); } if (screen.find(to) != null) { screen.click(to); } screen.doubleClick(); } catch (FindFailed e) { e.printStackTrace(); } } }