會不會使用工具,是通常QA和高級QA的區別 ---To be crazyhtml
Java就是好,開源框架遍地都是,各類niubility的jar包,各類神器,真是不亦樂乎.web
今天研究一下基於圖片識別做爲對象的niubility的sikuli框架。chrome
MIT的研究人員設計了一種新穎的圖形腳本語言Sikuli,計算機用戶只須有最基本的編程技能(好比會寫print"hello world"),編程
他不須要去寫出一行行代碼,而是用屏幕截圖的方式,用截出來的圖形元素組合出神奇的程序。 框架
簡單瞭解一下:ide
優勢:工具
1.能夠測試不易識別的對象,好比map 和Flash測試
2.能夠驗證圖片spa
3.通俗易懂,容易維護設計
缺點:
待續
1. 只能識別當前屏幕的內容,若是操做對象被最小化,是沒法識別的
2. 第一次運行須要重啓系統 ,雖然不make sense
下載:http://pan.baidu.com/s/1hq2CaHm
實例1.
打開chrome,輸入http://www.cnblogs.com/tobecrazy/ 並轉到
首先你得分別準備截圖
建立工程,把sikuli-script.jar添加進去
代碼以下:
import org.openqa.selenium.Keys; import org.sikuli.basics.Debug; import org.sikuli.script.FindFailed; import org.sikuli.script.Key; import org.sikuli.script.Pattern; import org.sikuli.script.Screen; public class NavigateToMyBlog { private static String URL="http://www.cnblogs.com/tobecrazy/"; public static void main(String[] args) throws FindFailed, Exception { Debug.setDebugLevel(3); Screen screen=new Screen(); Pattern chromeLogo=new Pattern("D:\\MyBlog\\chromeICO.png"); screen.doubleClick(chromeLogo); Pattern maxButton=new Pattern("D:\\MyBlog\\maxButton.png"); screen.click(maxButton); Pattern addressBar=new Pattern("D:\\MyBlog\\adressBar.png"); screen.type(addressBar, URL); String keys=Keys.ENTER.toString(); screen.keyDown(Key.ENTER);; } }
實例二, 配合selenium webdriver 測試高德地圖
1.打開地圖
2.選擇北京
3.打開地圖的工具,選擇測距
3.選擇天安門
4.選擇到這裏去
5.選擇輸入並搜索,火車站
precondition:
截圖呀:
Debug.setDebugLevel(3); Screen s = new Screen(); String URL = "http://www.amap.com/"; WebDriver driver = DriverFactory.getFirefoxDriver(); Actions actions = new Actions(driver); driver.get(URL); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); WebElement chooseCityElement = driver.findElement(By .xpath("//div/span[@class='icon_expand cursor ml5']")); chooseCityElement.click(); WebElement city_iframe = driver.findElement(By .xpath("//iframe[@id='city_iframe']")); driver.switchTo().frame(city_iframe); //選擇北京 WebElement BJ = driver.findElement(By.xpath("//div/a[text()='北京']")); BJ.click(); //等待頁面加載 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.switchTo().defaultContent(); //打開工具 選擇測距 WebElement tools=driver.findElement(By.xpath("//div/span[text()='工具']")); actions.moveToElement(tools).click().perform(); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); Pattern cj=new Pattern("D:\\MyBlog\\CJ.png"); if(s.find(cj) != null) { s.click(cj); } Pattern bjbz=new Pattern("D:\\MyBlog\\BJBZ.png"); s.click(bjbz); Pattern bjnz=new Pattern("D:\\MyBlog\\BJNZ.png"); s.click(bjnz); s.rightClick(); WebElement inputBoxElement = driver.findElement(By .xpath("//input[@id='keywordTxt']")); WebElement searchButton = driver.findElement(By .xpath("//input[starts-with(@class,'magnifier_button')]")); inputBoxElement.clear(); inputBoxElement.sendKeys("天安門"); searchButton.submit(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); WebElement tiAnMenElement = driver.findElement(By .xpath("//div[@title='天安門']")); tiAnMenElement.click(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); WebElement comeToHereElement = driver.findElement(By .xpath("//div[@class='route']//li[text()='到這裏去']")); comeToHereElement.click(); WebElement setStartLocationElement = driver.findElement(By .xpath("//div/input[@class='route-input srh-ipt']")); setStartLocationElement.sendKeys("火車站"); WebElement routeByBus = driver.findElement(By .xpath("//div[@id='rout-by-bus']")); routeByBus.click(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); WebElement startFrom = driver .findElement(By .xpath("//div[@class='start_end_item_title' and contains(text(),'北京站')]")); actions.moveToElement(startFrom) .click(driver.findElement(By .xpath("//a[contains(text(),'設爲起點') and @data-name='北京站']"))) .perform(); driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS); driver.findElement(By.xpath("//div[@class='amap-zoom-plus']")).click();
到此爲止,selenium配合sikuli的探索結束了,更多更好玩的玩法,會進一步更新