2. 客户端调用代码举例
packagesample.demo.AppiumDemo;
importstaticorg.junit.Assert.*;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.net.URL;
importjavax.imageio.ImageIO;
importlibs.Util;
importio.appium.java_client.android.AndroidDriver;
importorg.apache.commons.io.FileUtils;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
importorg.openqa.selenium.By;
importorg.openqa.selenium.OutputType;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.remote.DesiredCapabilities;
publicclassCompareScreenShots{
privateAndroidDriverdriver;
@Before
publicvoidsetUp()throwsException{
DesiredCapabilitiescap=newDesiredCapabilities();
cap.setCapability("deviceName","Android");
cap.setCapability("appPackage","com.example.android.notepad");
cap.setCapability("appActivity",".NotesList");
driver=newAndroidDriver(newURL("http://127.0.0.1:4723/wd/hub"),cap);
}
@After
publicvoidtearDown()throwsException{
driver.quit();
}
@Test
publicvoidcompareScreenAndSubScreen()throwsInterruptedException,IOException{
Thread.sleep(2000);
WebElementel=driver.findElement(By.className("android.widget.ListView")).findElement(By.name("Note1"));
el.click();
Thread.sleep(1000);
Stringp1="C:/1";
Stringp2="C:/2";
Filef2=newFile(p2);
Filef1=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f1,newFile(p1));
BufferedImageimg1=Util.getImageFromFile(f1);
f2=driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(f2,newFile(p2));
BufferedImageimg2=Util.getImageFromFile(f2);
Booleansame=Util.sameAs(img1,img2,0.9);
assertTrue(same);
BufferedImagesubImg1=Util.getSubImage(img1,6,39,474,38);
BufferedImagesubImg2=Util.getSubImage(img1,6,39,474,38);
same=Util.sameAs(subImg1,subImg2,1);
Filef3=newFile("c:/sub-1.png");
ImageIO.write(subImg1,"PNG",f3);
Filef4=newFile("c:/sub-2.png");
ImageIO.write(subImg1,"PNG",f4);
}
}
  也不多解析了,没有什么特别的东西。
  大家用得上的支持下好了...