会不会使用工具,是一般QA和高级QA的区别 ---To be crazy
Java就是好,开源框架遍地都是,各种niubility的jar包,各种神器,真是不亦乐乎.
今天研究一下基于图片识别作为对象的niubility的sikuli框架。
MIT的研究人员设计了一种新颖的图形脚本语言Sikuli,计算机用户只须有最基本的编程技能(比如会写print"hello world"),
他不需要去写出一行行代码,而是用屏幕截图的方式,用截出来的图形元素组合出神奇的程序。
简单了解一下:
优点:
1.可以测试不易识别的对象,比如map 和Flash
2.可以验证图片
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的探索结束了,更多更好玩的玩法,会进一步更新